#include using namespace std; typedef long long int ll; typedef pair P; typedef vector VI; typedef vector VVI; const ll MOD = 1000000007; const ll INF = 1e18; #define REP(i, n) for(int i = 0; i < n; i++) #define ALL(v) v.begin(), v.end() ll power(ll x, ll y, ll p) { if (y==0) return 1; else if (y==1) return x%p; else if (y%2==0) { ll pow=power(x,y/2,p); return (pow*pow)%p; } else { ll pow=power(x,y/2,p); return ((pow*pow)%p)*x%p; } } int main() { int t; cin >> t; vector pr(5000001,1); for(int i=2;i<=5000000;i++){ if(pr[i]){ for(int j=i*2;j<=5000000;j+=i) pr[j]=0; } } while(t--){ ll a, p; cin >> a >> p; if(pr[p]) printf("1\n"); else printf("-1\n"); } return 0; }