#include using namespace std; using ll = long long; struct Sieve { vector isprime; int n; Sieve(int n_) : isprime(n_ + 1, true), n(n_) {}; void build() { isprime[0] = isprime[1] = false; for(int i = 2; i <= n; i++) { if(isprime[i]) { for(int j = i + i; j <= n; j += i) { isprime[j] = false; } } } } bool isp(int i) { return isprime[i]; } }; const int mxN = 5e6; int main() { Sieve P(mxN); P.build(); int t; cin >> t; while(t--) { ll a; int p; cin >> a >> p; if(!P.isp(p)) { printf("%d\n", -1); }else{ if(a % p == 0) printf("%d\n", 0); else printf("%d\n", 1); } } }