#include #define rep(i,n) for(int i=0;i prime, mip(5000000 + 1); mip[0] = mip[1] = -1; for(int i = 2; i < mip.size(); i++) { if(mip[i] == 0) { mip[i] = i; prime.push_back(i); } for(int j = 0; j < prime.size() and prime[j] <= mip[i] and i * prime[j] <= mip.size(); j++) { mip[i * prime[j]] = prime[j]; } } auto pfact = [&](int x) { std::vector> vec; while(x > 1) { if(vec.empty() or vec.back().first != mip[x]) { vec.push_back({mip[x], 1}); } else { vec.back().second++; } x /= mip[x]; } return vec; }; int T;cin >> T; while (T--) { ll a;int p; cin >> a >> p; auto fac=pfact(p); if(fac.size()==1&&fac[0].first==p){ cout << (a%p==0?0:1) << "\n"; } else{ cout << -1 << "\n"; } } return 0; }