#include #define rep(i,n) for(int i=0;i<(int)n;++i) #define rrep(i,n) for(int i=(int)n-1;i>=0;--i) using namespace std; using ll = long long; template inline bool chmax(T& a,T b){if(a inline bool chmin(T& a,T b){if(b vector make_vec(size_t a){return vector(a);} template auto make_vec(size_t a,Ts... ts){return vector(ts...))>(a,make_vec(ts...));} template typename enable_if::value>::type fill_v(U& u,const V... v){u=U(v...);} template typename enable_if::value>::type fill_v(U& u,const V... v){for(auto& e:u)fill_v(e,v...);} struct Sieve { int n; vector f, primes; Sieve(int n=1) : n(n), f(n+1) { f[0] = f[1] = -1; for (long long i = 2; i <= n; ++i) { if (f[i]) continue; primes.push_back(i); f[i] = i; for (long long 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, pair(fl[0], 0)); for (int p : fl) { if (res.back().first == p) { res.back().second++; } else { res.emplace_back(p, 1); } } return res; } } sieve(5e6); void solve() { ll a, p; cin >> a >> p; if (!sieve.isPrime(p)) { cout << -1 << endl; } else { if (a % p == 0) { cout << 0 << endl; } else { cout << 1 << endl; } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) solve(); }