#include <bits/stdc++.h>
#define ll long long int
#define endl "\n"
#define pb push_back
#define pf push_front
#define mp make_pair
#define eb emplace_back
#define popb pop_back
#define popf pop_front
#define ff first
#define ss second
#define here cout<<"HERE !!!"<<endl;
#define sz(v) (ll)v.size()
#define vi vector<ll>
#define print(v) for (auto it : v) cout << it << " "; cout << endl;
#define all(v) v.begin(), v.end()
#define i128 __int128
#define read(v) for(auto &it:v) cin>>it;
#define Faster ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
const long double pi = acos(-1.0l);
const ll inf = 4e18;
const int mod = 998244353, mx = 1e5 + 100;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll my_rand(ll l, ll r)
{
return uniform_int_distribution<ll>(l, r)(rng);
}
//rotate(v.begin(), v.begin() + d, v.end());
//order_of_key(k): number of element less than k
//find_by_order(k): k-th element in set start from 0
const int32_t sz = 2e6 + 2;
vector<bool> prime(sz + 1, true);
void sieve() {
for (int i = 4; i < sz; i += 2) prime[i] = false;
for (int i = 3; i * i <= sz; i += 2) {
if (prime[i]) {
for (int j = i * i; j <= sz; j += 2 * i)
prime[j] = false;
}
} prime[0] = prime[1] = false;
}
void ravana(ll idx)
{
ll n, k; cin >> n >> k;
bool f = true;
vi v;
for (ll i = 2; i * i <= n; i ++)
{
if (n % i == 0)
{
v.pb(i);
if (n / i != i)
v.pb(n / i);
f = false;
}
}
if (f)
{
k %= 2;
if (k == 1)
{
cout << n*n << endl;
}
else
{
cout << n << endl;
}
return;
}
vi temp;
for (auto it : v)
{
bool f = true;
if (it < sz)
{
if (prime[it])
{
temp.pb(it);
}
continue;
}
if (it % 2 == 0 and it > 2)
continue;
for (ll i = 3; i * i <= it; i += 2)
{
if (it % i == 0)
{
f = false;
break;
}
}
if (f)
temp.pb(it);
}
v = temp;
sort(all(v));
ll x = 1;
while (sz(v) > 1)
{
if (x)
{
n *= v[0];
}
else
{
if (n % v.back() == 0)
{
n /= v.back();
}
else
{
v.popb();
n /= v.back();
}
}
k--;
x ^= 1;
if (k == 0)
{
cout << n << endl;
return;
}
}
k %= 2;
if (k == 1)
{
cout << n*v[0] << endl;
}
else
{
cout << n << endl;
}
}
signed main()
{
auto begin = std::chrono::high_resolution_clock::now();
Faster;
sieve();
ll t = 1;
cin >> t;
for (ll i = 1; i <= t; ++i)
{
ravana(i);
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}