#include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; #define MAX_PRIME 6000000 bool isprime[MAX_PRIME+1]; void init_prime(){ fill(isprime, isprime+MAX_PRIME+1, true); isprime[0] = isprime[1] = false; for(int i = 2; i <= MAX_PRIME; i++) { if (isprime[i]) { for(int j = i*2; j <= MAX_PRIME; j += i) isprime[j] = false; } } } int main(){ init_prime(); int T; cin >> T; rep(t,T){ ll A, P; cin >> A >> P; if(!isprime[P]) cout << -1 << endl; else{ if(A==0) cout << 0 << endl; else cout << 1 << endl; } } return 0; }