#include using namespace std; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i) #define rep2(i, x, n) for(ll i = x, i##_len = (n); i < i##_len; ++i) #define all(n) begin(n), end(n) using ll = long long; using P = pair; using vi = vector; using vl = vector; using vs = vector; using vc = vector; using vb = vector; using vd = vector; vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1}; 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; } }; int main() { ll t; cin >> t; Sieve si(6000000); rep(_, t) { ll a, p; cin >> a >> p; if(si.isPrime(p)) cout << int(a % p > 0) << '\n'; else cout << -1 << '\n'; } }