#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define LI list<int>
#define vct vector
#define vi vct<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vvi vct<vi>
#define vll vct<ll>
#define vvll vct<vll>
#define vpii vct<pii>
#define vpll vct<pll>
#define vb vct<bool>
#define vs vector<string>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define ft cout<<"for test"<<endl;
#define read(v,a,n) for(int i=a; i<n; i++)cin>>v[i];
#define print(v) for(auto it:v)cout<<it<<' '; cout<<endl;
#define PI acos(-1.0)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define t_c int test,cs=1;cin>>test;while(test--)
#define casep cout<<"Case "<<cs++<<": "
///#define endl '\n'
///................function.....................///
#define D(a) (double)(a)
#define siz(s) (int)(s.size())
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define mod 1000000007
#define dbg(a) cout << #a << " = " << a << " ";
int main()
{
    FIO;
    ///freopen("output.txt","r",stdin);
    ///freopen("in.txt","w",stdout);
    t_c
    {
        ll a,b;
        cin>>a>>b;
        if(a%b)
        {
            cout<<a<<endl;
            continue;
        }
        vi prm;
        int tmp=b;
        ll i,j,ans=1LL;
        for(i=2; i*i<=b; i++)
        {
            if(tmp%i==0)
            {
                prm.pb(i);
                while(tmp%i==0)
                    tmp/=i;
            }
        }
        if(tmp>1)
            prm.pb(tmp);
        for(auto it:prm)
        {
            tmp=b;
            int cnt=0;
            ll res=1;
            while(tmp%it==0)
            {
                tmp/=it;
                cnt++;
            }
            cnt--;
            tmp=a;
            while(tmp%it==0)
            {
                tmp/=it;
                if(cnt)
                {
                    cnt--;
                    res*=it;
                }
            }
            ans = max(ans, res*tmp);
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define LI list<int>
#define vct vector
#define vi vct<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vvi vct<vi>
#define vll vct<ll>
#define vvll vct<vll>
#define vpii vct<pii>
#define vpll vct<pll>
#define vb vct<bool>
#define vs vector<string>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define ft cout<<"for test"<<endl;
#define read(v,a,n) for(int i=a; i<n; i++)cin>>v[i];
#define print(v) for(auto it:v)cout<<it<<' '; cout<<endl;
#define PI acos(-1.0)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define t_c int test,cs=1;cin>>test;while(test--)
#define casep cout<<"Case "<<cs++<<": "
///#define endl '\n'
///................function.....................///
#define D(a) (double)(a)
#define siz(s) (int)(s.size())
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define mod 1000000007
#define dbg(a) cout << #a << " = " << a << " ";
int main()
{
    FIO;
    ///freopen("output.txt","r",stdin);
    ///freopen("in.txt","w",stdout);
    t_c
    {
        ll a,b;
        cin>>a>>b;
        if(a%b)
        {
            cout<<a<<endl;
            continue;
        }
        vll prm;
        ll tmp=b;
        for(ll i=2; i*i<=tmp; i++)
        {
            if(tmp%i==0)
            {
                prm.pb(i);
                while(tmp%i==0)tmp/=i;
            }
        }
        if(tmp>1)
            prm.pb(tmp);
        ll ans=1;
        for(auto it:prm)
        {
            tmp = b;
            int cnt=0;
            ll res=1;
            while(tmp%it==0)
            {
                tmp/=it;
                cnt++;
            }
            cnt--;
            tmp=a;
            while(tmp%it==0)
            {
                tmp/=it;
                if(cnt)
                {
                    cnt--;
                    res*=it;
                }
            }
            ans = max(ans, tmp*res);
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define LI list<int>
#define vct vector
#define vi vct<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vvi vct<vi>
#define vll vct<ll>
#define vvll vct<vll>
#define vpii vct<pii>
#define vpll vct<pll>
#define vb vct<bool>
#define vs vector<string>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define ft cout<<"for test"<<endl;
#define read(v,a,n) for(int i=a; i<n; i++)cin>>v[i];
#define print(v) for(auto it:v)cout<<it<<' '; cout<<endl;
#define PI acos(-1.0)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define t_c int test,cs=1;cin>>test;while(test--)
#define casep cout<<"Case "<<cs++<<": "
///#define endl '\n'
///................function.....................///
#define D(a) (double)(a)
#define siz(s) (int)(s.size())
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define mod 1000000007
#define dbg(a) cout << #a << " = " << a << " ";
int main()
{
    FIO;
    ///freopen("output.txt","r",stdin);
    ///freopen("in.txt","w",stdout);
    t_c
    {
        ll a,b;
        cin>>a>>b;
        if(a%b)
        {
            cout<<a<<endl;
            continue;
        }
        vi prm;
        ll i,j,ans=1LL;
        for(i=2; i*i<=a; i++)
        {
            if(b%i==0)
            {
                prm.pb(i);
                ll tmp=a,res=1;
                while(b%i==0)
                {
                    b/=i;
                    tmp/=i;
                    res *= i;
                }
                while(tmp%i==0)
                    tmp/=i;
                res/=i;
                ans = max(ans,res*tmp);
            }
        }
        cout<<ans<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define LI list<int>
#define vct vector
#define vi vct<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vvi vct<vi>
#define vll vct<ll>
#define vvll vct<vll>
#define vpii vct<pii>
#define vpll vct<pll>
#define vb vct<bool>
#define vs vector<string>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define ft cout<<"for test"<<endl;
#define read(v,a,n) for(int i=a; i<n; i++)cin>>v[i];
#define print(v) for(auto it:v)cout<<it<<' '; cout<<endl;
#define PI acos(-1.0)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define t_c int test,cs=1;cin>>test;while(test--)
#define casep cout<<"Case "<<cs++<<": "
///#define endl '\n'
///................function.....................///
#define D(a) (double)(a)
#define siz(s) (int)(s.size())
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define mod 1000000007
#define dbg(a) cout << #a << " = " << a << " ";
int main()
{
    FIO;
    ///freopen("output.txt","r",stdin);
    ///freopen("in.txt","w",stdout);
    t_c
    {
        ll a,b;
        cin>>a>>b;
        if(a%b)
        {
            cout<<a<<endl;
            continue;
        }
        vi prm;
        ll i,j,ans=1LL;
        for(i=2; i*i<=b; i++)
        {
            if(b%i==0)
            {
                prm.pb(i);
                ll tmp=a,res=1;
                while(b%i==0)
                {
                    b/=i;
                    tmp/=i;
                    res *= i;
                }
                while(tmp%i==0)
                    tmp/=i;
                res/=i;
                ans = max(ans,res*tmp);
            }
        }
        if(b>1)
        {
            ans = max(ans, a/b);
        }
        cout<<ans<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define LI list<int>
#define vct vector
#define vi vct<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vvi vct<vi>
#define vll vct<ll>
#define vvll vct<vll>
#define vpii vct<pii>
#define vpll vct<pll>
#define vb vct<bool>
#define vs vector<string>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define ft cout<<"for test"<<endl;
#define read(v,a,n) for(int i=a; i<n; i++)cin>>v[i];
#define print(v) for(auto it:v)cout<<it<<' '; cout<<endl;
#define PI acos(-1.0)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define t_c int test,cs=1;cin>>test;while(test--)
#define casep cout<<"Case "<<cs++<<": "
///#define endl '\n'
///................function.....................///
#define D(a) (double)(a)
#define siz(s) (int)(s.size())
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define mod 1000000007
#define dbg(a) cout << #a << " = " << a << " ";
int main()
{
    FIO;
    ///freopen("output.txt","r",stdin);
    ///freopen("in.txt","w",stdout);
    t_c
    {
        ll a,b;
        cin>>a>>b;
        if(a%b)
        {
            cout<<a<<endl;
            continue;
        }
        vll prm;
        ll tmp=b,tt;
        ll ans=1;
        for(ll i=2; i*i<=tmp; i++)
        {
            if(tmp%i==0)
            {
                prm.pb(i);
                int cnt=0;
                while(tmp%i==0)
                {
                    tmp/=i;
                    cnt++;
                }
                tt=a;
                ll res=1;
                cnt--;
                while(tt%i==0)
                {
                    tt/=i;
                    if(cnt)
                    {
                        res*=i;
                        cnt--;
                    }
                }
                ans = max(ans, tt*res);
            }
        }
        if(tmp>1)
            ans = max(ans, a/tmp);
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define LI list<int>
#define vct vector
#define vi vct<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vvi vct<vi>
#define vll vct<ll>
#define vvll vct<vll>
#define vpii vct<pii>
#define vpll vct<pll>
#define vb vct<bool>
#define vs vector<string>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())
#define ft cout<<"for test"<<endl;
#define read(v,a,n) for(int i=a; i<n; i++)cin>>v[i];
#define print(v) for(auto it:v)cout<<it<<' '; cout<<endl;
#define PI acos(-1.0)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define t_c int test,cs=1;cin>>test;while(test--)
#define casep cout<<"Case "<<cs++<<": "
///#define endl '\n'
///................function.....................///
#define D(a) (double)(a)
#define siz(s) (int)(s.size())
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define mod 1000000007
#define dbg(a) cout << #a << " = " << a << " ";
int main()
{
    FIO;
    ///freopen("output.txt","r",stdin);
    ///freopen("in.txt","w",stdout);
    t_c
    {
        ll a,b;
        cin>>a>>b;
        if(a%b)
        {
            cout<<a<<endl;
            continue;
        }
        vi prm;
        int tmp=b;
        ll i,j,ans=1LL;
        for(i=2; i*i<=b; i++)
        {
            if(tmp%i==0)
            {
                prm.pb(i);
                while(tmp%i==0)
                    tmp/=i;
            }
        }
        if(b>1)
            prm.pb(b);
        ll ans=1;
        for(auto it:prm)
        {
            tmp=b;
            int cnt=0;
            ll res=1;
            while(tmp%it==0)
            {
                tmp/=it;
                cnt++;
            }
            cnt--;
            tmp=a;
            while(tmp%it==0)
            {
                tmp/=it;
                if(cnt)
                {
                    cnt--;
                    res*=it;
                }
            }
            ans = max(ans, res*tmp);
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int, long long> fac(int n)
{
	map<int, long long> Q;
	for(int i = 2 ; i * i <= n ; i++)
	{
		if(n % i) continue ;
		while(n % i == 0)
		{
			n /= i ;
			Q[i]++;
		}
	}
	if(n != 1) Q[n]++ ;
	return Q ;
}
void solve()
{
	long long p, q; cin >> p >> q ;
	if(p % q)
	cout << p << "\n" ;
	else
	{
		map<int, long long > Q = fac(q);
		long long ans = 1 ;
		for(auto x : Q)
		{
			long long P = p ;
			int n = x.first ;
			long long m = x.second - 1;
			while(p % n == 0) p /= n ;
			while(m--) p *= n ;
			ans = max(ans, p) ;
			p = P ;
		}
		cout << ans << "\n" ;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int t = 1 ; cin >> t;
	while(t--) solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int, long long> fac(int n)
{
	map<int, long long> Q;
	int i = 2 ;
	while(n != 1)
	{
		while(n % i == 0)
		{
			n /= i ;
			Q[i]++;
		}
		if(i == 2) i++;
		else i += 2 ;
	}
	return Q ;
}
long long exp(long long a, long long b)
{
    if(b == 1) return a ;
    if(b == 0) return 1 ;
    if(a == 0) return 0 ;
    if(a == 1) return 1 ;
    if(b & 1)
    return (exp((a * a), (b / 2)) * a);
    return exp((a * a), (b / 2));
}
void solve()
{
	long long p, q; cin >> p >> q ;
	if(p % q)
	cout << p << "\n" ;
	else
	{
		map<int, long long > Q = fac(q);
		long long ans = 1 ;
		for(auto x : Q)
		{
			long long P = p ;
			int n = x.first ;
			long long m = x.second - 1;
			while(p % n == 0) p /= n ;
			while(m--) p *= n ;
			ans = max(ans, p) ;
			p = P ;
		}
		cout << ans << "\n" ;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int t = 1 ; cin >> t;
	while(t--) solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> fac(int n)
{
	vector<int> Q;
	for(int i = 2 ; i * i <= n ; i++)
	{
		if(n % i == 0)
		{
			Q.push_back(i) ;
			Q.push_back(n / i) ;
		}
	}
	return Q ;
}
void solve()
{
	long long p, q; cin >> p >> q ;
	if(p % q)
	cout << p << "\n" ;
	else
	{
		vector<int> Q = fac(q);
		long long ans = LLONG_MIN ;
		for(auto x : Q)
		{
			long long P = p ;
			if(p % x == 0)
			{
				p /= x ;
				if(p % q)
				ans = max(ans, p);
			}
			p = P ;
		}
		cout << ans << "\n" ;
 	}

}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int t = 1 ; cin >> t;
	while(t--) solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int, int> fac(int n)
{
	map<int, int> Q;
	int i = 2 ;
	while(n != 1)
	{
		while(n % i == 0)
		{
			n /= i ;
			Q[i]++;
		}
		i++;
	}
	return Q ;
}
void solve()
{
	long long p, q; cin >> p >> q ;
	if(p % q)
	cout << p << "\n" ;
	else
	{
		map<int, int> Q = fac(q);
		long long ans = 1;
		auto x = *(Q.begin()) ;
		if(1)
		{
			long long P = p ;
			while(P % (x.first) == 0) P /= x.first ;
			int s = x.second - 1;
			while(s--) P *= x.first ;
			ans = max(ans, P);
		}
		cout << ans << "\n" ;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int t = 1 ; cin >> t;
	while(t--) solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int, long long> fac(int n)
{
	map<int, long long> Q;
	int i = 2 ;
	while(n != 1)
	{
		while(n % i == 0)
		{
			n /= i ;
			Q[i]++;
		}
		if(i == 2) i++;
		else i += 2 ;
	}
	return Q ;
}
long long exp(long long a, long long b)
{
    if(b == 1) return a ;
    if(b == 0) return 1 ;
    if(a == 0) return 0 ;
    if(a == 1) return 1 ;
    if(b & 1)
    return (exp((a * a), (b / 2)) * a);
    return exp((a * a), (b / 2));
}
void solve()
{
	long long p, q; cin >> p >> q ;
	if(p % q)
	cout << p << "\n" ;
	else
	{
		map<int, long long > Q = fac(q);
		long long ans = 1 ;
		for(auto x : Q)
		{
			int n = x.first ;
			long long m = x.second - 1;
			while(p % n == 0) p /= n ;
			while(m--) p *= n ;
			ans = max(ans, p) ;
			p *= n ;
		}
		cout << ans << "\n" ;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int t = 1 ; cin >> t;
	while(t--) solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> fac(int n)
{
	vector<int> Q;
	for(int i = 2 ; i * i <= n ; i++)
	{
		if(n % i == 0)
		{
			Q.push_back(i) ;
			Q.push_back(n / i) ;
		}
	}
	return Q ;
}
void solve()
{
	long long p, q; cin >> p >> q ;
	if(p % q)
	cout << p << "\n" ;
	else
	{
		vector<int> Q = fac(q);
		long long ans = LLONG_MIN ;
		for(auto x : Q)
		{
			long long P = p ;
			while(p % x == 0) p /= x ;
			if(p % q == 0) p = P ;
			else
			{
				ans = max(ans, p) ;
				p = P;
			}
		}
		cout << ans << "\n" ;
 	}

}
int main()
{
	// ios_base::sync_with_stdio(false);
	// cin.tie(NULL); cout.tie(NULL);
	int t = 1 ; cin >> t;
	while(t--) solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<int> fac(int n)
{
	vector<int> Q;
	for(int i = 2 ; i * i <= n ; i++)
	{
		if(n % i == 0)
		{
			Q.push_back(i) ;
			Q.push_back(n / i) ;
		}
	}
	return Q ;
}
void solve()
{
	long long p, q; cin >> p >> q ;
	if(p % q)
	cout << p << "\n" ;
	else
	{
		vector<int> Q = fac(q);
		long long ans = 1 ;
		for(auto x : Q)
		{
			long long P = p ;
			if(p % x == 0)
			{
				p /= x ;
				if(p % q)
				ans = max(ans, p);
			}
			p = P ;
		}
		cout << ans << "\n" ;
 	}

}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int t = 1 ; cin >> t;
	while(t--) solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int, long long> fac(int n)
{
	map<int, long long> Q;
	for(int i = 2 ; i * i <= n ; i++)
	{
		if(n % i) continue ;
		while(n % i == 0)
		{
			n /= i ;
			Q[i]++;
		}
	}
	return Q ;
}
void solve()
{
	long long p, q; cin >> p >> q ;
	if(p % q)
	cout << p << "\n" ;
	else
	{
		map<int, long long > Q = fac(q);
		long long ans = 1 ;
		for(auto x : Q)
		{
			long long P = p ;
			int n = x.first ;
			long long m = x.second - 1;
			while(p % n == 0) p /= n ;
			while(m--) p *= n ;
			ans = max(ans, p) ;
			p = P ;
		}
		cout << ans << "\n" ;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int t = 1 ; cin >> t;
	while(t--) solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int, long long> fac(int n)
{
	map<int, long long> Q;
	int i = 2 ;
	while(n != 1)
	{
		while(n % i == 0)
		{
			n /= i ;
			Q[i]++;
		}
		if(i == 2) i++;
		else i += 2 ;
	}
	return Q ;
}
long long exp(long long a, long long b)
{
    if(b == 1) return a ;
    if(b == 0) return 1 ;
    if(a == 0) return 0 ;
    if(a == 1) return 1 ;
    if(b & 1)
    return (exp((a * a), (b / 2)) * a);
    return exp((a * a), (b / 2));
}
void solve()
{
	long long p, q; cin >> p >> q ;
	if(p % q)
	cout << p << "\n" ;
	else
	{
		map<int, long long > Q = fac(q);
		map<int, long long > P = fac(p);
		long long ans = 1;
		map<int, long long > PP = P ;
		for(auto &x : PP)
		x.second = exp(x.first, x.second);
		for(auto x : P)
		{
			int n = x.first ;
			long long m = x.second ;
			if(Q.find(x.first) != Q.end())
			m = min(Q[x.first] - 1, m) ;
			long long cans = p ;
			cans /= PP[x.first] ;
			if(Q.find(x.first) != Q.end())
			cans *= exp(x.first, m) ;
			ans = max(ans, cans) ;
		}
		cout << ans << "\n" ;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int t = 1 ; cin >> t;
	while(t--) solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int, int> fac(int n)
{
	map<int, int> Q;
	int i = 2 ;
	while(n != 1)
	{
		while(n % i == 0)
		{
			n /= i ;
			Q[i]++;
		}
		i++;
	}
	return Q ;
}
void solve()
{
	long long p, q; cin >> p >> q ;
	if(p % q)
	cout << p << "\n" ;
	else
	{
		map<int, int> Q = fac(q);
		long long ans = 1;
		for(auto x : Q)
		{
			long long P = p ;
			while(P % (x.first) == 0) P /= x.first ;
			int s = x.second - 1;
			while(s--) P *= x.first ;
			ans = max(ans, P);
		}
		cout << ans << "\n" ;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	int t = 1 ; cin >> t;
	while(t--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+5;

long long remove(long long p, long long q){
    while(p%q==0)p/=q;
    return p;
}

int main()
{
    //Division round 680
    bool seive[MAX];
    for(int i=2; i<MAX; i++){
        if(seive[i])continue;
        for(int j=i+i; j<MAX; j+=i){
            seive[j]=1;
        }
    }
    vector<int> primes;
    for(int i=2; i<MAX; i++){
        if(!seive[i])primes.push_back(i);
    }
    int t; cin >> t;
    while(t--){
        long long p,q,ans=1; cin >> p >> q;
        if(p%q==0){
            for(int i=0; i<primes.size(); i++){
                if(q%primes[i]==0){
                    ans=max(ans,remove(p,primes[i]));
                }
            }
            cout << ans << '\n';
        }else{
            cout << p << '\n';
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+6;

long long removeP(long long p, long long q, long long num){
    while(p%q==0)p/=num;
    return p;
}

int main()
{
    //Division round 680
    int t; cin >> t;
    while(t--){
        long long p,q; cin >> p >> q;
        long long ans = removeP(p,q,q);
        for(int i=2; i*i<=q; i++){
            if(q%i==0)
                ans=max(ans,max(removeP(p,q,i),removeP(p,q,q/i)));
        }
        cout << ans << '\n';
    }
}


// Author : Mohamed Sameh
#include <bits/stdc++.h>
using namespace std ;
long long p, q;
int mx = 1e5;
long long removeP(int prime)
{
  long long cur = p;
  while(cur % q == 0)
    cur /= prime;
  return cur;
}
int main()
{
  int t;
  cin >> t;
  while(t--)
  {
    cin >> p >> q;
    long long ans = removeP(q);
    for (int i = 2; i <= mx; i++)
    {
      if ( q % i)continue;
      ans = max(ans, removeP(i));
    }
    cout << ans << '\n';
  }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+6;

long long removeP(long long p, long long q, long long num){
    while(p%q==0)p/=num;
    return p;
}

int main()
{
    //Division round 680
    bool seive[MAX]={};
    for(long long i=2; i<MAX; i++){
        if(seive[i])continue;
        for(long long j=i*i; j<MAX; j+=i){
            seive[j]=1;
        }
    }
    vector<int> primes;
    for(int i=2; i<MAX; i++){
        if(!seive[i])primes.push_back(i);
    }
    int t; cin >> t;
    while(t--){
        long long p,q,ans=removeP(p,q,q); cin >> p >> q;
        if(p%q==0){
            for(int i=0; i<primes.size(); i++){
                if(q%primes[i]==0){
                    ans=max(ans,removeP(p,q,primes[i]));
                }
            }
            cout << ans << '\n';
        }else{
            cout << p << '\n';
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e7+6;

long long removeP(long long p, long long q, long long num){
    while(p%q==0)p/=num;
    return p;
}

int main()
{
    //Division round 680
    bool seive[MAX]={};
    for(long long i=2; i<MAX; i++){
        if(seive[i])continue;
        for(long long j=i*i; j<MAX; j+=i){
            seive[j]=1;
        }
    }
    vector<int> primes;
    for(int i=2; i<MAX; i++){
        if(!seive[i])primes.push_back(i);
    }
    int t; cin >> t;
    while(t--){
        long long p,q; cin >> p >> q;
        long long ans=removeP(p,q,q);
        for(int i=0; i<primes.size(); i++){
            if(q%primes[i]==0){
                ans=max(ans,removeP(p,q,primes[i]));
            }
        }
        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+6;

long long removeP(long long p, long long q, long long num){
    while(p%q==0)p/=num;
    return p;
}

int main()
{
    //Division round 680
    bool seive[MAX]={};
    for(long long i=2; i<MAX; i++){
        if(seive[i])continue;
        for(long long j=i*i; j<MAX; j+=i){
            seive[j]=1;
        }
    }
    vector<int> primes;
    for(int i=2; i<MAX; i++){
        if(!seive[i])primes.push_back(i);
    }
    int t; cin >> t;
    while(t--){
        long long p,q; cin >> p >> q;
        long long ans=removeP(p,q,q);
        if(p%q==0){
            for(int i=0; i<primes.size(); i++){
                if(q%primes[i]==0){
                    ans=max(ans,removeP(p,q,primes[i]));
                }
            }
            cout << ans << '\n';
        }else{
            cout << p << '\n';
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+6;

long long removeP(long long p, long long q, long long num){
    while(p%q==0)p/=num;
    return p;
}

int main()
{
    //Division round 680
    int t; cin >> t;
    while(t--){
        long long p,q; cin >> p >> q;
        long long ans = 1;
        for(int i=2; i*i<=q; i++){
            if(q%i==0)
                ans=max(ans,max(removeP(p,q,i),removeP(p,q,q/i)));
        }
        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+6;

long long removeP(long long p, long long q, long long num){
    while(p%q==0)p/=num;
    return p;
}

int main()
{
    //Division round 680
    bool seive[MAX]={};
    for(long long i=2; i<MAX; i++){
        if(seive[i])continue;
        for(long long j=i*i; j<MAX; j+=i){
            seive[j]=1;
        }
    }
    vector<int> primes;
    for(int i=2; i<MAX; i++){
        if(!seive[i])primes.push_back(i);
    }
    int t; cin >> t;
    while(t--){
        long long p,q,ans=1; cin >> p >> q;
        if(p%q==0){
            for(int i=0; i<primes.size(); i++){
                if(q%primes[i]==0){
                    ans=max(ans,removeP(p,q,primes[i]));
                }
            }
            cout << ans << '\n';
        }else{
            cout << p << '\n';
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+5;

long long remove(long long p, long long q){
    while(p%q==0)p/=q;
    return p;
}

int main()
{
    //Division round 680
    bool seive[MAX]={};
    for(int i=2; i<MAX; i++){
        if(seive[i])continue;
        for(int j=i+i; j<MAX; j+=i){
            seive[j]=1;
        }
    }
    vector<int> primes;
    for(int i=2; i<MAX; i++){
        if(!seive[i])primes.push_back(i);
    }
    int t; cin >> t;
    while(t--){
        long long p,q,ans=1; cin >> p >> q;
        if(p%q==0){
            for(int i=0; i<primes.size(); i++){
                if(q%primes[i]==0){
                    ans=max(ans,remove(p,primes[i]));
                }
            }
            cout << ans << '\n';
        }else{
            cout << p << '\n';
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+6;

long long removeP(long long p, long long q, long long num){
    while(p%q==0)p/=num;
    return p;
}

int main()
{
    //Division round 680
    bool seive[MAX]={};
    for(long long i=2; i<MAX; i++){
        if(seive[i])continue;
        for(long long j=i*i; j<MAX; j+=i){
            seive[j]=1;
        }
    }
    vector<int> primes;
    for(int i=2; i<MAX; i++){
        if(!seive[i])primes.push_back(i);
    }
    int t; cin >> t;
    while(t--){
        long long p,q,ans=removeP(p,q,primes[i]); cin >> p >> q;
        if(p%q==0){
            for(int i=0; i<primes.size(); i++){
                if(q%primes[i]==0){
                    ans=max(ans,removeP(p,q,primes[i]));
                }
            }
            cout << ans << '\n';
        }else{
            cout << p << '\n';
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5+6;

long long removeP(long long p, long long q, long long num){
    while(p%q==0)p/=num;
    return p;
}

int main()
{
    //Division round 680
    int t; cin >> t;
    while(t--){
        long long p,q; cin >> p >> q;
        long long ans = removeP(p,q,q); // <-- A must here
        for(int i=2; i*i<=q; i++){
            if(q%i==0)
                ans=max(ans,max(removeP(p,q,i),removeP(p,q,q/i)));
        }
        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e8;

long long remove(long long p, long long q){
    while(p%q==0)p/=q;
    return p;
}

int main()
{
    //Division round 680
    bool seive[MAX]={};
    for(int i=2; i<MAX; i++){
        if(seive[i])continue;
        for(int j=i+i; j<MAX; j+=i){
            seive[j]=1;
        }
    }
    vector<int> primes;
    for(int i=2; i<MAX; i++){
        if(!seive[i])primes.push_back(i);
    }
    int t; cin >> t;
    while(t--){
        long long p,q,ans=1; cin >> p >> q;
        if(p%q==0){
            for(int i=0; i<primes.size(); i++){
                if(q%primes[i]==0){
                    ans=max(ans,remove(p,primes[i]));
                }
            }
            cout << ans << '\n';
        }else{
            cout << p << '\n';
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e7;

long long remove(long long p, long long q){
    while(p%q==0)p/=q;
    return p;
}

int main()
{
    //Division round 680
    bool seive[MAX]={};
    for(int i=2; i<MAX; i++){
        if(seive[i])continue;
        for(int j=i+i; j<MAX; j+=i){
            seive[j]=1;
        }
    }
    vector<int> primes;
    for(int i=2; i<MAX; i++){
        if(!seive[i])primes.push_back(i);
    }
    int t; cin >> t;
    while(t--){
        long long p,q,ans=1; cin >> p >> q;
        if(p%q==0){
            for(int i=0; i<primes.size(); i++){
                if(q%primes[i]==0){
                    ans=max(ans,remove(p,primes[i]));
                }
            }
            cout << ans << '\n';
        }else{
            cout << p << '\n';
        }
    }
}


/* ICoder : Hakesh D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define ld long double
#define ar array

#define vt vector
#define append push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back

#define SUM accumulate
#define MAX *max_element
#define MIN *min_element
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define GCD __gcd

#define FOR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define for1(e) FOR(i, 0, e, 1)
#define for2(i, e) FOR(i, 0, e, 1)
#define for3(b, e) FOR(i, b, e, 1)
#define for4(i, b, e) FOR(i, b, e, 1)
#define EACH(x, a) for (auto& x: a)

#define maxn1 100005
#define maxn2 200005
#define mod 1000000007
#define inf INT_MAX
#define iinf 1e9
#define linf 1e12
#define llinf 1e18

void read(vt<int> a,int n){int temp;for1(n){cin >> temp;a.append(temp);}}
void write(vt<int> a){for(auto x : a) {cout << x << ' ';} ; cout << endl;}
void print(vt<int> a,int i,int f){for(int x=i;x<=f;x++) {cout << a[x] << ' ';} ; cout << endl;}
/***********************************************************************************************/

void Isolve(){
    ll a,b; cin >> a >> b;
    //power of x in n;
    auto iget = [&](ll n,ll x){
        ll ans = 0;
        while(n%x==0){ans++;n /= x;}
        return ans;
    };

    vt<pair<ll,ll>> prime;
    ll n = b,i = 2;
    while(i <= sqrt(b)+2){
        if(n%i==0){
            ll pwr = 0;
            while(n%i==0){pwr++;n /= i;}
            prime.append(mp(i,pwr));
        }
        i++;
    }

    /*
    for(auto x : prime){
        cout << x.first << " " << x .second << endl;
    }
    */

    ll ans = 1;
    for(auto x : prime){
        ll fact = x.first;
        ll pwr = x.second;
        ll apwr = iget(a,fact);
        if(pwr > apwr){cout << a << endl; return;}
        //apwr - pwr + 1
        ll tmp = a;
        for(ll i = 0;i < apwr-pwr+1 ;i++){tmp /= fact;}
        //cout << tmp << endl;
        ans = max(ans,tmp);

    }
    cout << ans << endl;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int it = 1;
    cin >> it;
    while (it--) {
        Isolve();
    }
    return 0;
}
/***********************************************************************************************/


/* ICoder : Hakesh D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define ld long double
#define ar array

#define vt vector
#define append push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back

#define SUM accumulate
#define MAX *max_element
#define MIN *min_element
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define GCD __gcd

#define FOR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define for1(e) FOR(i, 0, e, 1)
#define for2(i, e) FOR(i, 0, e, 1)
#define for3(b, e) FOR(i, b, e, 1)
#define for4(i, b, e) FOR(i, b, e, 1)
#define EACH(x, a) for (auto& x: a)

#define maxn1 100005
#define maxn2 200005
#define mod 1000000007
#define inf INT_MAX
#define iinf 1e9
#define linf 1e12
#define llinf 1e18

void read(vt<int> a,int n){int temp;for1(n){cin >> temp;a.append(temp);}}
void write(vt<int> a){for(auto x : a) {cout << x << ' ';} ; cout << endl;}
void print(vt<int> a,int i,int f){for(int x=i;x<=f;x++) {cout << a[x] << ' ';} ; cout << endl;}
/***********************************************************************************************/

void Isolve(){
    ll a,b; cin >> a >> b;
    //power of x in n;
    auto iget = [&](ll n,ll x){
        ll ans = 0;
        while(n%x==0){ans++;n /= x;}
        return ans;
    };

    vt<pair<ll,ll>> prime;
    ll n = b,i = 2;
    while(i <= sqrt(b)+2){
        if(n%i==0){
            ll pwr = 0;
            while(n%i==0){pwr++;n /= i;}
            prime.append(mp(i,pwr));
        }
        i++;
    }
    if(sz(prime) == 0){
        prime.append(mp(b,1));
    }
    /*
    for(auto x : prime){
        cout << x.first << " " << x .second << endl;
    }
    */

    ll ans = 1;
    for(auto x : prime){
        ll fact = x.first;
        ll pwr = x.second;
        ll apwr = iget(a,fact);
        if(pwr > apwr){cout << a << endl; return;}
        //apwr - pwr + 1
        ll tmp = a;
        for(ll i = 0;i < apwr-pwr+1 ;i++){tmp /= fact;}
        //cout << tmp << endl;
        ans = max(ans,tmp);

    }
    cout << ans << endl;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int it = 1;
    cin >> it;
    while (it--) {
        Isolve();
    }
    return 0;
}
/***********************************************************************************************/


/* ICoder : Hakesh D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define ld long double
#define ar array

#define vt vector
#define append push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back

#define SUM accumulate
#define MAX *max_element
#define MIN *min_element
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define GCD __gcd

#define FOR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define for1(e) FOR(i, 0, e, 1)
#define for2(i, e) FOR(i, 0, e, 1)
#define for3(b, e) FOR(i, b, e, 1)
#define for4(i, b, e) FOR(i, b, e, 1)
#define EACH(x, a) for (auto& x: a)

#define maxn1 100005
#define maxn2 200005
#define mod 1000000007
#define inf INT_MAX
#define iinf 1e9
#define linf 1e12
#define llinf 1e18

void read(vt<int> a,int n){int temp;for1(n){cin >> temp;a.append(temp);}}
void write(vt<int> a){for(auto x : a) {cout << x << ' ';} ; cout << endl;}
void print(vt<int> a,int i,int f){for(int x=i;x<=f;x++) {cout << a[x] << ' ';} ; cout << endl;}
/***********************************************************************************************/

void Isolve(){
    ll a,b; cin >> a >> b;
    ll gd = GCD(a,b);

    if(gd != b){cout << a << endl; return ;}
    //power of x in n;
    auto iget = [&](ll n,ll x){
        ll ans = 0;
        while(n%x==0){ans++;n /= x;}
        return ans;
    };

    vt<pair<ll,ll>> prime;
    ll n = b,i = 2;
    while(i <= sqrt(n)+2){
        if(n%i==0){
            ll pwr = iget(n,i);
            prime.append(mp(i,pwr));
            n = n/(pow(i,pwr));
        }
        i++;
    }

    /*
    for(auto x : prime){
        cout << x.first << " " << x .second << endl;
    }
    */

    vt<ll> ans;
    for(auto x : prime){
        ll fact = x.first;
        ll pwr = x.second;
        ll apwr = iget(a,fact);
        ll tmp = pow(fact,apwr-pwr+1);
        ans.append(tmp);
    }
    cout << a/MIN(all(ans)) << endl;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int it = 1;
    cin >> it;
    while (it--) {
        Isolve();
    }
    return 0;
}
/***********************************************************************************************/


/* ICoder : Hakesh D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define ld long double
#define ar array

#define vt vector
#define append push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back

#define SUM accumulate
#define MAX *max_element
#define MIN *min_element
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define GCD __gcd

#define FOR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define for1(e) FOR(i, 0, e, 1)
#define for2(i, e) FOR(i, 0, e, 1)
#define for3(b, e) FOR(i, b, e, 1)
#define for4(i, b, e) FOR(i, b, e, 1)
#define EACH(x, a) for (auto& x: a)

#define maxn1 100005
#define maxn2 200005
#define mod 1000000007
#define inf INT_MAX
#define iinf 1e9
#define linf 1e12
#define llinf 1e18

void read(vt<int> a,int n){int temp;for1(n){cin >> temp;a.append(temp);}}
void write(vt<int> a){for(auto x : a) {cout << x << ' ';} ; cout << endl;}
void print(vt<int> a,int i,int f){for(int x=i;x<=f;x++) {cout << a[x] << ' ';} ; cout << endl;}
/***********************************************************************************************/

void Isolve(){
    ll a,b; cin >> a >> b;
    //power of x in n;
    auto iget = [&](ll n,ll x){
        ll ans = 0;
        while(n%x==0){ans++;n /= x;}
        return ans;
    };

    vt<pair<ll,ll>> prime;
    ll n = b,i = 2;
    while(i <= sqrt(n)+2){
        if(n%i==0){
            ll pwr = 0;
            while(n%i==0){pwr++;n /= i;}
            prime.append(mp(i,pwr));
        }
        i++;
    }

    /*
    for(auto x : prime){
        cout << x.first << " " << x .second << endl;
    }
    */

    ll ans = 0;
    for(auto x : prime){
        ll fact = x.first;
        ll pwr = x.second;
        ll apwr = iget(a,fact);
        if(pwr > apwr){cout << a << endl; return;}
        //apwr - pwr + 1
        ll tmp = a;
        for(ll i = 0;i < apwr-pwr+1 ;i++){tmp /= fact;}
        //cout << tmp << endl;
        ans = max(ans,tmp);

    }
    cout << ans << endl;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int it = 1;
    cin >> it;
    while (it--) {
        Isolve();
    }
    return 0;
}
/***********************************************************************************************/


/* ICoder : Hakesh D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define ld long double
#define ar array

#define vt vector
#define append push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back

#define SUM accumulate
#define MAX *max_element
#define MIN *min_element
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define GCD __gcd

#define FOR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define for1(e) FOR(i, 0, e, 1)
#define for2(i, e) FOR(i, 0, e, 1)
#define for3(b, e) FOR(i, b, e, 1)
#define for4(i, b, e) FOR(i, b, e, 1)
#define EACH(x, a) for (auto& x: a)

#define maxn1 100005
#define maxn2 200005
#define mod 1000000007
#define inf INT_MAX
#define iinf 1e9
#define linf 1e12
#define llinf 1e18

void read(vt<int> a,int n){int temp;for1(n){cin >> temp;a.append(temp);}}
void write(vt<int> a){for(auto x : a) {cout << x << ' ';} ; cout << endl;}
void print(vt<int> a,int i,int f){for(int x=i;x<=f;x++) {cout << a[x] << ' ';} ; cout << endl;}
/***********************************************************************************************/

int prime[500000];

void primeMaker(int prime[]){
    for(int i = 2;i < 500000;i++){
    if(prime[i] == 1){
        int j = 2*i;
        while(j < 500000){
            prime[j] = 0;
            j += i;
        }
    }
}
}

void Isolve(){
    ll a,b; cin >> a >> b;
    //power of x in n;
    auto iget = [&](ll n,ll x){
        ll ans = 0;
        while(n%x==0){ans++;n /= x;}
        return ans;
    };

    vt<pair<ll,ll>> factors;
    ll n = b,i = 2;
    while(i <= sqrt(b)+2){
        if(b%i==0){
            if(prime[i]){
                ll pwr = iget(b,i);
                factors.append(mp(i,pwr));
            }
            if(prime[b/i]){
                ll pwr = iget(b,b/i);
                factors.append(mp(b/i,pwr));
            }

        }
        i++;
    }
    if(sz(factors) == 0){
        factors.append(mp(b,1));
    }
    /*
    for(auto x : factors){
        cout << x.first << " " << x .second << endl;
    }
    */

    ll ans = 1;
    for(auto x : factors){
        ll fact = x.first;
        ll pwr = x.second;
        ll apwr = iget(a,fact);
        if(pwr > apwr){cout << a << endl; return;}
        //apwr - pwr + 1
        ll tmp = a;
        for(ll i = 0;i < apwr-pwr+1 ;i++){tmp /= fact;}
        //cout << tmp << endl;
        ans = max(ans,tmp);

    }
    cout << ans << endl;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    fill(prime,prime+500000,1);
    prime[1] = 0;

    primeMaker(prime);
    int it = 1;
    cin >> it;
    while (it--) {
        Isolve();
    }
    return 0;
}
/***********************************************************************************************/


/* ICoder : Hakesh D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define ld long double
#define ar array

#define vt vector
#define append push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back

#define SUM accumulate
#define MAX *max_element
#define MIN *min_element
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define GCD __gcd

#define FOR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define for1(e) FOR(i, 0, e, 1)
#define for2(i, e) FOR(i, 0, e, 1)
#define for3(b, e) FOR(i, b, e, 1)
#define for4(i, b, e) FOR(i, b, e, 1)
#define EACH(x, a) for (auto& x: a)

#define maxn1 100005
#define maxn2 200005
#define mod 1000000007
#define inf INT_MAX
#define iinf 1e9
#define linf 1e12
#define llinf 1e18

void read(vt<int> a,int n){int temp;for1(n){cin >> temp;a.append(temp);}}
void write(vt<int> a){for(auto x : a) {cout << x << ' ';} ; cout << endl;}
void print(vt<int> a,int i,int f){for(int x=i;x<=f;x++) {cout << a[x] << ' ';} ; cout << endl;}
/***********************************************************************************************/

void Isolve(){
    ll a,b; cin >> a >> b;
    //power of x in n;
    auto iget = [&](ll n,ll x){
        ll ans = 0;
        while(n%x==0){ans++;n /= x;}
        return ans;
    };

    vt<pair<ll,ll>> prime;
    ll n = b,i = 2;
    while(i <= sqrt(n)+2){
        if(n%i==0){
            ll pwr = 0;
            while(n%i==0){pwr++;n /= i;}
            prime.append(mp(i,pwr));
        }
        i++;
    }

    /*
    for(auto x : prime){
        cout << x.first << " " << x .second << endl;
    }
    */

    ll ans = 1;
    for(auto x : prime){
        ll fact = x.first;
        ll pwr = x.second;
        ll apwr = iget(a,fact);
        if(pwr > apwr){cout << a << endl; return;}
        //apwr - pwr + 1
        ll tmp = a;
        for(ll i = 0;i < apwr-pwr+1 ;i++){tmp /= fact;}
        //cout << tmp << endl;
        ans = max(ans,tmp);

    }
    cout << ans << endl;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int it = 1;
    cin >> it;
    while (it--) {
        Isolve();
    }
    return 0;
}
/***********************************************************************************************/


/* ICoder : Hakesh D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define ld long double
#define ar array

#define vt vector
#define append push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back

#define SUM accumulate
#define MAX *max_element
#define MIN *min_element
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define GCD __gcd

#define FOR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define for1(e) FOR(i, 0, e, 1)
#define for2(i, e) FOR(i, 0, e, 1)
#define for3(b, e) FOR(i, b, e, 1)
#define for4(i, b, e) FOR(i, b, e, 1)
#define EACH(x, a) for (auto& x: a)

#define maxn1 100005
#define maxn2 200005
#define mod 1000000007
#define inf INT_MAX
#define iinf 1e9
#define linf 1e12
#define llinf 1e18

void read(vt<int> a,int n){int temp;for1(n){cin >> temp;a.append(temp);}}
void write(vt<int> a){for(auto x : a) {cout << x << ' ';} ; cout << endl;}
void print(vt<int> a,int i,int f){for(int x=i;x<=f;x++) {cout << a[x] << ' ';} ; cout << endl;}
/***********************************************************************************************/

int prime[500000];

void primeMaker(int prime[]){
    for(int i = 2;i < 500000;i++){
    if(prime[i] == 1){
        int j = 2*i;
        while(j < 500000){
            prime[j] = 0;
            j += i;
        }
    }
}
}

void Isolve(){
    ll a,b; cin >> a >> b;
    //power of x in n;
    auto iget = [&](ll n,ll x){
        ll ans = 0;
        while(n%x==0){ans++;n /= x;}
        return ans;
    };

    vt<pair<ll,ll>> factors;
    ll n = b,i = 2;
    while(i <= sqrt(b)){
        if(n%i==0){
            if(prime[i]){
                ll pwr = iget(b,i);
                factors.append(mp(i,pwr));
                while(n%i==0) n/=i;
            }

        }
        i++;
    }
    if(n != 1){
        factors.append(mp(n,1));
    }
    /*
    for(auto x : factors){
        cout << x.first << " " << x .second << endl;
    }
    */

    ll ans = 1;
    for(auto x : factors){
        ll fact = x.first;
        ll pwr = x.second;
        ll apwr = iget(a,fact);
        if(pwr > apwr){cout << a << endl; return;}
        //apwr - pwr + 1
        ll tmp = a;
        for(ll i = 0;i < apwr-pwr+1 ;i++){tmp /= fact;}
        //cout << tmp << endl;
        ans = max(ans,tmp);

    }
    cout << ans << endl;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    fill(prime,prime+500000,1);
    prime[1] = 0;

    primeMaker(prime);
    int it = 1;
    cin >> it;
    while (it--) {
        Isolve();
    }
    return 0;
}
/***********************************************************************************************/


/* ICoder : Hakesh D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define ld long double
#define ar array

#define vt vector
#define append push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back

#define SUM accumulate
#define MAX *max_element
#define MIN *min_element
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define GCD __gcd

#define FOR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define for1(e) FOR(i, 0, e, 1)
#define for2(i, e) FOR(i, 0, e, 1)
#define for3(b, e) FOR(i, b, e, 1)
#define for4(i, b, e) FOR(i, b, e, 1)
#define EACH(x, a) for (auto& x: a)

#define maxn1 100005
#define maxn2 200005
#define mod 1000000007
#define inf INT_MAX
#define iinf 1e9
#define linf 1e12
#define llinf 1e18

void read(vt<int> a,int n){int temp;for1(n){cin >> temp;a.append(temp);}}
void write(vt<int> a){for(auto x : a) {cout << x << ' ';} ; cout << endl;}
void print(vt<int> a,int i,int f){for(int x=i;x<=f;x++) {cout << a[x] << ' ';} ; cout << endl;}
/***********************************************************************************************/

void Isolve(){
    ll a,b; cin >> a >> b;
    vt<ll> ans;
    for(ll i = 1;i <=sqrt(a);i++){
        if(a%i == 0){
            if((a/i)%b != 0) {cout << a/i << endl; return;}
            if((i)%b != 0) {ans.append(i);}
        }

    }
    cout << MAX(all(ans)) << endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int it = 1;
    cin >> it;
    while (it--) {
        Isolve();
    }
    return 0;
}
/***********************************************************************************************/


/* ICoder : Hakesh D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define ld long double
#define ar array

#define vt vector
#define append push_back
#define mp make_pair
#define popf pop_front
#define popb pop_back

#define SUM accumulate
#define MAX *max_element
#define MIN *min_element
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define GCD __gcd

#define FOR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define for1(e) FOR(i, 0, e, 1)
#define for2(i, e) FOR(i, 0, e, 1)
#define for3(b, e) FOR(i, b, e, 1)
#define for4(i, b, e) FOR(i, b, e, 1)
#define EACH(x, a) for (auto& x: a)

#define maxn1 100005
#define maxn2 200005
#define mod 1000000007
#define inf INT_MAX
#define iinf 1e9
#define linf 1e12
#define llinf 1e18

void read(vt<int> a,int n){int temp;for1(n){cin >> temp;a.append(temp);}}
void write(vt<int> a){for(auto x : a) {cout << x << ' ';} ; cout << endl;}
void print(vt<int> a,int i,int f){for(int x=i;x<=f;x++) {cout << a[x] << ' ';} ; cout << endl;}
/***********************************************************************************************/

void Isolve(){
    ll a,b; cin >> a >> b;
    ll gd = GCD(a,b);

    if(gd != b){cout << a << endl; return ;}
    //power of x in n;
    auto iget = [&](ll n,ll x){
        ll ans = 0;
        while(n%x==0){ans++;n /= x;}
        return ans;
    };

    vt<pair<ll,ll>> prime;
    ll n = b,i = 2;
    while(i <= sqrt(b)+1){
        if(n%i==0){
            ll pwr = iget(n,i);
            prime.append(mp(i,pwr));
            n = n/(pow(i,pwr));
        }
        i++;
    }

    /*
    for(auto x : prime){
        cout << x.first << " " << x .second << endl;
    }
    */

    vt<ll> ans;
    for(auto x : prime){
        ll fact = x.first;
        ll pwr = x.second;
        ll apwr = iget(a,fact);
        ll tmp = pow(fact,apwr-pwr+1);
        ans.append(tmp);
    }
    cout << a/MIN(all(ans)) << endl;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int it = 1;
    cin >> it;
    while (it--) {
        Isolve();
    }
    return 0;
}
/***********************************************************************************************/


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int maxValid(int x, int a, int b) {
    int res = 0;
    if(a % x == 0 && x % b != 0) res = max(res, x);
    if(a % x == 0 && (a / x) % b != 0) res = max(res, a / x);
    return res;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // handle different cases
        if(p < q) {
            cout << p << '\n';
        }

        else if (p == q) {
            int ans = 1;
            for(int i = 2; i * i <= q; ++i){
                if((q % i) == 0){
                    ans = max(ans, i);
                    ans = max(ans, q / i);
                }
            }
            cout << ans << '\n';
        }

        else {
            int ans = 1;
            for(int i = 1; i * i <= q; ++i){
                if((q % i) == 0){
                    ans = max(ans, maxValid(i, p, q));
                    ans = max(ans, maxValid((q / i), p, q));
                }
            }
            cout << ans << '\n';
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; (i * i) <= x; ++i) {
        if(x % i == 0){
            //int p = i;
            int c = 0;
            while(x % i == 0) {x /= i; c++;}
            fact.push_back({i, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;
        int ans = 0;
        vpii div = factorization(q);
        for(auto divisor : div) {
            int x = divisor.first;
            //int cnt = divisor.second;
            int P = p;
            while((P % q) == 0) {P /= x;}
            ans = max(ans, P);
        }
        cout << ans << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // find largest x such that x | p
        // and q doesn't divide x
        int g = __gcd(p, q);
        if((p % q) != 0 || (p < q)) {
            cout << p << "\n";
        } else {
            int ans = 1;
            int i; // for iterating over numbers
            vector<int> div;
            div.push_back(1);

            for(i = 2; i * i <= q; ++i) {
                if(q % i == 0) {
                    div.push_back(i);
                    div.push_back(q / i);
                }
            }

            if(q % i == 0) div.push_back(i);

            sortall(div);
            int x = (p / q);
            for(int i = (int)div.size() - 1; i >= 0; --i) {
                if((q % (x * div[i])) != 0) {
                    ans = (x * div[i]);
                    break;
                }
            }

            cout << ans << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // find largest x such that x | p
        // and q doesn't divide x
        int g = __gcd(p, q);
        if((p % q) != 0) {
            cout << p << "\n";
        } else {
            int ans = 1;
            vpii div = factorization(q);
            for(auto divisor : div) {
                int x = divisor.first;
                int cnt = divisor.second;
                int P = p;
                while((P % x) == 0) {P /= x;}
                ans = max(ans, P);
            }

            cout << ans << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // find largest x such that x | p
        // and q doesn't divide x
        int g = __gcd(p, q);
        if((p % q) != 0 || (p < q)) {
            cout << p << "\n";
        } else {
            int ans = 1;
            int i; // for iterating over numbers
            vector<int> div;
            div.push_back(1);

            for(i = 2; i * i <= q; ++i) {
                if(q % i == 0) {
                    div.push_back(i);
                    div.push_back(q / i);
                }
            }

            sortall(div);
            int x = (p / q);
            for(int i = (int)div.size() - 1; i >= 0; --i) {
                if((q % (x * div[i])) != 0) {
                    ans = (x * div[i]);
                    break;
                }
            }

            cout << ans << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 1; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // find largest x such that x | p
        // and q doesn't divide x
        int g = __gcd(p, q);
        if((p % q) != 0) {
            cout << p << "\n";
        } else {
            int ans = 0;
            vpii div = factorization(q);
            for(auto divisor : div) {
                int x = divisor.first;
                int cnt = divisor.second;
                int P = p;
                while((P % x) == 0) {P /= x;}
                ans = max(ans, P);
            }

            cout << ans << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int maxValid(int x, int a, int b) {
    int res = 0;
    if(a % x == 0 && x % b != 0) res = max(res, x);
    if(a % x == 0 && (a / x) % b != 0) res = max(res, a / x);
    return res;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // handle different cases
        if(p % q != 0 || p < q) {
            cout << p << '\n';
        }

        else if (p == q) {
            int ans = 1;
            for(int i = 2; i * i <= q; ++i){
                if((q % i) == 0){
                    ans = max(ans, i);
                    ans = max(ans, q / i);
                }
            }
            cout << ans << '\n';
        }

        else {
            int ans = 1;
            for(int i = 1; i * i <= q; ++i){
                if((q % i) == 0){
                    ans = max(ans, maxValid(i, p, q));
                    ans = max(ans, maxValid((q / i), p, q));
                }
            }
            cout << ans << '\n';
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // find largest x such that x | p
        // and q doesn't divide x
        int g = __gcd(p, q);
        if((p % q) != 0) {
            cout << p << "\n";
        } else {
            int ans = 0;
            vpii div = factorization(q);
            for(auto divisor : div) {
                int x = divisor.first;
                int cnt = divisor.second;
                int P = p;
                while((P % x) == 0) {P /= x;}
                ans = max(ans, P);
            }

            cout << ans << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // find largest x such that x | p
        // and q doesn't divide x
        int g = __gcd(p, q);
        if((p % q) != 0 || (p < q)) {
            cout << p << "\n";
        } else {

            int ans = -1;
            int i = 2;
            while(1) {
                if(p % i == 0) {
                    int x = p / i;
                    if(x % q != 0) {
                        ans = x;
                        break;
                    }
                }
                ++i;
            }

            if(ans != -1) cout << ans << '\n';
            else cout << "1\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        if(p > q && p % q != 0) {
            cout << p << '\n';
            continue;
        }

        if(p > q && p % q == 0) {
            int x = (p / q);
            int ans;
            for(int i = x;  i >= 2; --i) {
                if(p % (x * i) == 0 && ((x * i) % q) != 0) {
                    ans = (x * i);
                    break;
                }
            }

            cout << ans << '\n';
            continue;
        }

        if(p < q)
            cout << p << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // find largest x such that x | p
        // and q doesn't divide x
        int g = __gcd(p, q);
        if((p % q) != 0 || (p < q)) {
            cout << p << "\n";
        } else {
            int ans = -1;
            int i; // for iterating over numbers
            vector<int> div;
            div.push_back(1);

            for(i = 2; i * i <= q; ++i) {
                if(q % i == 0) {
                    div.push_back(i);
                    div.push_back(q / i);
                }
            }

            if(q % i == 0) div.push_back(i);

            sortall(div);
            int x = (p / q);
            for(int i = (int)div.size() - 1; i >= 0; --i) {
                if(((x * div[i]) % q) != 0) {
                    ans = (x * div[i]);
                    break;
                }
            }

            if(ans != -1) cout << ans << "\n";
            else cout << "1\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // find largest x such that x | p
        // and q doesn't divide x
        int g = __gcd(p, q);
        if((p % q) != 0) {
            cout << p << "\n";
        } else {
            int ans = 1;
            vpii div = factorization(q);
            for(auto divisor : div) {
                int x = divisor.first;
                int cnt = divisor.second;
                int c = 0;
                while((p % x) == 0) {p /= x; c++;}
                if(c > cnt) {
                    ans *= pow(x, c);
                }
            }

            cout << ans << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long int
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF (1ll << 60l)
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp);
void ipgraph(int m);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const int mod = 1e9 + 7;
//=========================================

vector<pii> factorization(int x) {
    vector<pair<int, int> > fact;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0){
            int p = i;
            int c = 0;
            while(x % p == 0) {x /= p; c++;}
            fact.push_back({p, c});
        }
    }

    if(x > 1) {
        fact.push_back({x, 1});
    }

    return fact;
}

int32_t main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("inputf.in", "r", stdin);
    freopen("outputf.in", "w", stdout);
    #endif

    int t;
    cin >> t;
    while(t--) {
        int p, q;
        cin >> p >> q;

        // find largest x such that x | p
        // and q doesn't divide x
        int g = __gcd(p, q);
        if((p % q) != 0 || (p < q)) {
            cout << p << "\n";
        } else {

            int ans = -1;
            int i = 2;
            for(i = 2; i * i <= p; ++i) {
                if(p % i == 0) {
                    int x = p / i;
                    if(x % q != 0) {
                        ans = x;
                        break;
                    }
                }
                //++i;
            }

            if(ans != -1) cout << ans << '\n';
            else cout << "1\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	// CODE goes here
	ll t;
	cin >> t;
	while(t--){
		ll p,q;
		cin >> p >> q;
		ll maxi=LLONG_MIN;
		if(p%q){
			cout << p << "\n";
			continue;
		}
		ll temp=q,divv=2;
		vector<ll> pf;
		while(temp>1 && divv<sqrt(q)+1){
			while(temp%divv==0){
				temp/=divv;
				pf.push_back(divv);
			}
			divv++;
		}
		if(temp>1) pf.push_back(temp);
		sort(pf.begin(),pf.end());
		vector<ll> x;
		for(ll i=0;i<pf.size();i++){
			ll tempx=p;
			ll count=0;
			for(ll j=0;j<pf.size();j++) if(pf[j]==pf[i]) count++;
			while(tempx%pf[i]==0) tempx/=pf[i];
			for(ll j=0;j<count-1;j++) tempx*=pf[i];
			x.push_back(tempx);
		}
		sort(x.begin(),x.end());
		cout << x[x.size()-1] << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	// CODE goes here
	ll t;
	cin >> t;
	while(t--){
		ll p,q;
		cin >> p >> q;
		ll maxi=LLONG_MIN;
		if(p%q){
			cout << p << "\n";
			continue;
		}
		ll temp=q,div=2;
		vector<ll> pf;
		while(temp>1 && div<sqrt(q)+1){
			while(temp%div==0){
				temp/=div;
				pf.push_back(div);
			}
			div++;
		}
		ll ans=1;
		if(temp>1) pf.push_back(temp);
		sort(pf.begin(),pf.end());
		//for(ll i=0;i<pf.size();i++) cout << pf[i] << " ";
		for(ll i=0;i<pf.size();i++){
			if(p%pf[i]==0){
			    if((p/pf[i])%q){
				    ans=p/pf[i];
				    break;
			    }
			}
		}
		cout << ans << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	// CODE goes here
	ll t;
	cin >> t;
	while(t--){
		ll p,q;
		cin >> p >> q;
		ll maxi=LLONG_MIN;
		if(p%q){
			cout << p << "\n";
			continue;
		}
		ll temp=q,div=2;
		vector<ll> pf;
		while(temp>1 && div<sqrt(q)+1){
			while(temp%div==0){
				temp/=div;
				pf.push_back(div);
			}
			div++;
		}
		if(temp>1) pf.push_back(temp);
		sort(pf.begin(),pf.end());
		vector<ll> x;
		for(ll i=0;i<pf.size();i++){
			ll tempx=p;
			while(tempx%pf[i]==0) tempx/=pf[i];
			x.push_back(tempx);
		}
		sort(x.begin(),x.end());
		cout << x[x.size()-1] << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	// CODE goes here
	ll t;
	cin >> t;
	while(t--){
		ll p,q;
		cin >> p >> q;
		ll maxi=LLONG_MIN;
		if(p%q){
			cout << p << "\n";
			continue;
		}
		ll temp=q,div=2;
		vector<ll> pf;
		while(temp>0 && div<sqrt(q)+1){
			while(temp%div==0){
				temp/=div;
				pf.push_back(div);
			}
			div++;
		}
		ll ans=-1;
		if(temp>1) pf.push_back(temp);
		sort(pf.begin(),pf.end());
		//for(ll i=0;i<pf.size();i++) cout << pf[i] << " ";
		for(ll i=0;i<pf.size();i++){
			if((p/pf[i])%q){
				ans=p/pf[i];
				break;
			}
		}
		cout << ans << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	// CODE goes here
	ll t;
	cin >> t;
	while(t--){
		ll p,q;
		cin >> p >> q;
		ll maxi=LLONG_MIN;
		if(p%q){
			cout << p << "\n";
			continue;
		}
		ll temp=q,div=2;
		vector<ll> pf;
		while(temp>1 && div<sqrt(q)+1){
			while(temp%div==0){
				temp/=div;
				pf.push_back(div);
			}
			div++;
		}
		ll ans=1;
		if(temp>1) pf.push_back(temp);
		sort(pf.begin(),pf.end());
		//for(ll i=0;i<pf.size();i++) cout << pf[i] << " ";
		for(ll i=0;i<pf.size();i++){
			if(p%pf[i]==0){
			    if((p/pf[i])%q){
				    ans=p/pf[i];
				    break;
			    }
			}
		}
		if(ans==1){
		    if(pf.size()>1) for(ll i=1;i<pf.size();i++) ans*=pf[i];
		}
		cout << ans << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	// CODE goes here
	ll t;
	cin >> t;
	while(t--){
		ll p,q;
		cin >> p >> q;
		ll maxi=LLONG_MIN;
		if(p%q){
			cout << p << "\n";
			continue;
		}
		ll temp=q,div=2;
		vector<ll> pf;
		while(temp>1 && div<sqrt(q)+1){
			while(temp%div==0){
				temp/=div;
				pf.push_back(div);
			}
			div++;
		}
		if(temp>1) pf.push_back(temp);
		sort(pf.begin(),pf.end());
		vector<ll> x;
		for(ll i=0;i<pf.size();i++){
			ll tempx=p;
			ll count=0;
			for(ll j=0;j<pf.size();j++) if(pf[j]==pf[i]) count++;
			while(tempx%pf[i]==0) tempx/=pf[i];
			for(ll j=0;j<count-1;j++) tempx*=pf[i];
			x.push_back(tempx);
		}
		sort(x.begin(),x.end());
		cout << max(x[x.size()-1],p/q) << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	// CODE goes here
	ll t;
	cin >> t;
	while(t--){
		ll p,q;
		cin >> p >> q;
		ll maxi=LLONG_MIN;
		for(ll i=1;i<sqrt(p)+1;i++){
			if(p%i) continue;
			if(i%q && (p/i)%q){
			    maxi=max({maxi,i,p/i});
			    break;
			}
			else if(i%q) maxi=max(maxi,i);
			else if((p/i)%q){
			    maxi=max(maxi,p/i);
			    break;
			}
		}
		cout << maxi << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	// CODE goes here
	ll t;
	cin >> t;
	while(t--){
		ll p,q;
		cin >> p >> q;
		ll maxi=LLONG_MIN;
		if(p%q){
			cout << p << "\n";
			continue;
		}
		ll temp=q,div=2;
		vector<ll> pf;
		while(temp>1 && div<sqrt(q)+1){
			while(temp%div==0){
				temp/=div;
				pf.push_back(div);
			}
			div++;
		}
		ll ans=1;
		if(temp>1) pf.push_back(temp);
		sort(pf.begin(),pf.end());
		//for(ll i=0;i<pf.size();i++) cout << pf[i] << " ";
		for(ll i=0;i<pf.size();i++){
			if((p/pf[i])%q){
				ans=p/pf[i];
				break;
			}
		}
		cout << ans << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	// CODE goes here
	ll t;
	cin >> t;
	while(t--){
		ll p,q;
		cin >> p >> q;
		ll maxi=LLONG_MIN;
		if(p%q){
			cout << p << "\n";
			continue;
		}
		for(ll i=1;i<sqrt(p)+1;i++){
			if(p%i) continue;
			if(i%q && (p/i)%q){
			    maxi=max({maxi,i,p/i});
			    break;
			}
			else if(i%q) maxi=max(maxi,i);
			else if((p/i)%q){
			    maxi=max(maxi,p/i);
			    break;
			}
		}
		cout << maxi << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	// CODE goes here
	ll t;
	cin >> t;
	while(t--){
		ll p,q;
		cin >> p >> q;
		ll maxi=LLONG_MIN;
		if(p%q){
			cout << p << "\n";
			continue;
		}
		ll temp=q,div=2;
		vector<ll> pf;
		while(temp>1 && div<sqrt(q)+1){
			while(temp%div==0){
				temp/=div;
				pf.push_back(div);
			}
			div++;
		}
		if(temp>1) pf.push_back(temp);
		sort(pf.begin(),pf.end());
		vector<ll> x;
		for(ll i=0;i<pf.size();i++){
			ll tempx=p;
			while(tempx%pf[i]==0) tempx/=pf[i];
			x.push_back(tempx);
		}
		sort(x.begin(),x.end());
		cout << max(x[x.size()-1],p/q) << "\n";
	}
	return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        ll tmp = p;
        ll qq = q;
        ll now = 1;
        ll m1 = p;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                if (now1 >= now2) {
                    now *= now1;
                    m1 = min(m1, now1 / now2 * prime[i]);
                }
                else {
                    m1 = 1;
                }
            }
        }
        if (q > 1 && tmp % q == 0) {
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p;
            while (q % p == 0) q /= p, now2 *= p;
            if (now1 >= now2) {
                now *= now1;
                m1 = min(m1, now1 / now2 * p);
            }
            else {
                m1 = 1;
            }
        }
        if (tmp > 1) {
            cout << p << endl;
            continue;
        }
        cout << p / m1 << endl;

    }

    return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        if (p % q != 0) {
            cout << p << endl;
            continue;
        }
        ll ans = 1;
        ll now = p;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll tmp = p;
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i], now /= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                ans = max(ans, tmp * now2 / prime[i]);
            }
        }
        if (q > 1) {
            ll tmp = p;
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p, now /= p;
            while (q % p == 0) q /= p, now2 *= p;
            ans = max(ans, tmp * now2 / p);
        }
        cout << ans << endl;

    }

    return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        if (p % q != 0) {
            cout << p << endl;
            continue;
        }
        ll ans = 1;
        ll now = p;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll tmp = p;
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i], now /= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                if (now1 >= now2 / prime[i])
                    ans = max(ans, tmp * now2 / prime[i]);
                else ans = p;
            }
        }
        if (q > 1 && p % q == 0) {
            ll tmp = p;
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p, now /= p;
            while (q % p == 0) q /= p, now2 *= p;
            if (now1 >= now2 / p)
                ans = max(ans, tmp * now2 / p);
            else
                ans = p;
        }
        cout << ans << endl;

    }

    return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        ll ans = 1;
        ll now = p;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll tmp = p;
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i], now /= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                if (now1 >= now2 / prime[i])
                    ans = max(ans, tmp * now2 / prime[i]);
                else ans = p;
            }
        }
        if (q > 1 && p % q == 0) {
            ll tmp = p;
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p, now /= p;
            while (q % p == 0) q /= p, now2 *= p;
            if (now1 >= now2 / p)
                ans = max(ans, tmp * now2 / p);
            else
                ans = p;
        }
        cout << ans << endl;

    }

    return 0;
}


//
// Created by ttw on 2020/11/1.
//

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;

int prime[maxn], tot;
bool vis[maxn];
void init () {
    for (int i = 2; i < maxn; i++) {
        if (!vis[i]) {
            prime[tot++] = i;
        }
        for (int j = 0; j < tot && i * prime[j] < maxn; j++) {
            vis[i * prime[j]] = 1;
            if (i % prime[j] == 0)  break;
        }
    }
}

int main () {
    init();
    int T;
    cin >> T;
    while (T--) {
        ll p, q;
        cin >> p >> q;
        ll tmp = p;
        ll qq = q;
        ll now = 1;
        if (p == q) {
        }
        ll m1 = p;
        for (int i = 0; i < tot && prime[i] <= q; i++) {
            if (q % prime[i] == 0) {
                ll now1 = 1, now2 = 1;
                while (tmp % prime[i] == 0) tmp /= prime[i], now1 *= prime[i];
                while (q % prime[i] == 0) q /= prime[i], now2 *= prime[i];
                if (now1 >= now2) {
                    now *= now1;
                    m1 = min(m1, now1 / now2 * prime[i]);
                }
            }
        }
        if (q > 1 && tmp % q == 0) {
            ll now1 = 1, now2 = 1;
            while (tmp % p == 0) tmp /= p, now1 *= p;
            while (q % p == 0) q /= p, now2 *= p;
            if (now1 >= now2) {
                now *= now1;
                m1 = min(m1, now1 / now2 * p);
            }
        }
        if (tmp > 1) {
            cout << p << endl;
            continue;
        }
        cout << p / m1 << endl;

    }

    return 0;
}