#include<bits/stdc++.h> #define x first #define y second #define PB push_back #define mst(x,a) memset(x,a,sizeof(x)) #define all(a) begin(a),end(a) #define rep(x,l,u) for(ll x=l;x<u;x++) #define rrep(x,l,u) for(ll x=l;x>=u;x--) #define sz(x) x.size() #define IOS ios::sync_with_stdio(false);cin.tie(0); using namespace std; typedef unsigned long long ull; typedef pair<int,int> PII; typedef pair<char,char> PCC; typedef long long ll; typedef pair<ll,ll> PLL; const int N=1e5+10; const int M=1e6+10; const int INF=0x3f3f3f3f; const int mod=1e9+7; void solve(){ ll p,q;cin>>p>>q; if(p%q!=0) {cout<<p<<endl;return;} ll x=q; ll ans=1; rep(i,2,x/i+1){ if(x%i==0){ while(x%i==0) x/=i; ll ss=p; while(ss%q==0) ss/=i; ans=max(ans,ss); } } if(x>1){ ll ss=p; while(ss%q==0) ss/=x; ans=max(ans,ss); } cout<<ans<<endl; } int main(){ IOS; //freopen("test.txt", "r", stdin); //freopen("test.txt", "w", stdout); int t;cin>>t; while(t--) solve(); return 0; }
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句