#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, n) for(int(i) = 0; (i) < (n); (i)++) #define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++) #define All(v) (v).begin(), (v).end() #define pb push_back #define MP(a, b) make_pair((a), (b)) using ll = long long; using pii = pair; using pll = pair; const int INF = 1 << 30; const ll LINF = 1LL << 60; const int MOD = 1e9 + 7; vector sieve(int x) { // range...[0,x] vector flags(x + 1, true); flags[0] = false, flags[1] = false; for(int i = 2; i * i <= x; i++) { if(!flags[i]) continue; for(int mult = i * i; mult <= x; mult += i) flags[mult] = false; } return flags; } int main() { int T; ios::sync_with_stdio(false); cin.tie(nullptr); cin >> T; auto primes = sieve(5000000); while(T--) { ll A, P; cin >> A >> P; if(primes[P]) { A = A % P; if(A == 0) { cout << 0 << endl; } else { cout << 1 << endl; } } else { cout << -1 << endl; } } return 0; }