#include #include using mint = atcoder::static_modint<998244353>; // using mint = atcoder::static_modint<1000000007>; using namespace std; using namespace atcoder; using ld = long double; using ll = long long; #define mp(a,b) make_pair(a,b) #define rep(i,s,n) for(int i=s; i dx{1,0,-1,0},dy{0,1,0,-1}; bool is_prime(ll x){ // miller rabin auto pow_mod=[&](__int128_t a,ll k,__int128_t mod){ __int128_t output=1; a%=mod; while(k){ if(k&1)output=(output*a)%mod; a=(a*a)%mod; k>>=1; } return output; }; if(x<=1)return false; vector test; if(x<4759123141)test={2,7,61}; else test={2,325,9375,28178,450775,9780504,1795265022}; ll K=x-1; while(!(K&1))K>>=1; for(ll a:test){ if(a>=x)return true; bool ok=false; ll k=K; __int128_t c=pow_mod(a,k,x); if(c==1)ok=true; else{ while(k<=x-1){ if(c==x-1){ ok=true; break; } c=c*c%x; k<<=1; } } if(!ok)return false; } return true; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T;cin >> T; rep(i,0,T){ ll a,p;cin >> a >> p; a%=p; if(is_prime(p)){ if(a)cout << 1 << "\n"; else cout << 0 << "\n"; } else cout << -1 << "\n"; } }