#include using namespace std; using ll=long long; using P= pair; // Sieve of Eratosthenes // https://youtu.be/UTVg7wzMWQc?t=2774 struct Sieve { int n; vector f, primes; Sieve(int n=1):n(n), f(n+1) { f[0] = f[1] = -1; for (ll i = 2; i <= n; ++i) { if (f[i]) continue; primes.push_back(i); f[i] = i; for (ll j = i*i; j <= n; j += i) { if (!f[j]) f[j] = i; } } } bool isPrime(int x) { return f[x] == x;} vector factorList(int x) { vector res; while (x != 1) { res.push_back(f[x]); x /= f[x]; } return res; } vector

factor(int x) { vector fl = factorList(x); if (fl.size() == 0) return {}; vector

res(1, P(fl[0], 0)); for (int p : fl) { if (res.back().first == p) { res.back().second++; } else { res.emplace_back(p, 1); } } return res; } vector> factor(ll x) { vector> res; for (int p : primes) { int y = 0; while (x%p == 0) x /= p, ++y; if (y != 0) res.emplace_back(p,y); } if (x != 1) res.emplace_back(x,1); return res; } }; int main() { ll T; cin >> T; Sieve sieve = Sieve(int(1e6)*5+5); for(int i=0;i> A >> P; if(sieve.isPrime(P)){ if(A%P ==0){ cout << 0 << "\n"; }else{ cout << 1 <<"\n"; } }else{ cout << -1 << "\n"; } } return 0; }