#includeusing namespace std;typedeflonglong ll;intmain(){int t;cin >> t;while(t --){ll p;int q;cin >> p >> q;if(p < q || p % q !=0) cout << p << endl;else{ll ans =0;int t = q;for(int i =2; i * i <= q; i ++){int f =0;while(q % i ==0){q /= i;f ++;}if(f){f = f -1;ll t = p;if(t % i ==0){while(t % i ==0) t /= i;while(f --) t *= i;ans =max(t,ans);}}}if(q >1){while(p % q ==0) p /= q;ans =max(p,ans);}cout << ans << endl;}}}