#include using namespace std; typedef long long ll; #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++) #define NMAX 5000100 bool primes[NMAX]; ll mod_pow(ll a, ll n, ll mod){ ll ret = 1; while(n > 0){ if(n & 1) ret = (ret*(a % mod))%mod; a = ((a%mod)*(a%mod)) % mod; n = n >> 1; } return ret; } ll gcd(ll a, ll b){ if(a < b) return gcd(b, a); ll r = a % b; while(r != 0){ a = b; b = r; r = a % b; } return b; } ll lcm(ll a, ll b){ ll ret = a / gcd(a,b) * b; return ret; } int main(){ rep(i, 2, NMAX) primes[i] = true; rep(i, 2, NMAX) { for(ll j = i * 2; j <= NMAX; j += i) primes[j] = false; } ll T = 0; cin >> T; rep(test, 0, T){ ll a, p; cin >> a >> p; if(!primes[p]) { cout << -1 << endl; continue; } if(gcd(a, p) == 1) cout << 1 << endl; else cout << 0 << endl; } return 0; }