#include using namespace std; using ll = long long; using ull = unsigned long long; using V = vector; using VV = vector; using VVV = vector; using VL = vector; using VVL = vector; using VVVL = vector; template using VE = vector; template using P = pair; #define rep(i,n) for(int i=0;i<(n);i++) #define rep1(i,n) for(int i=1;i<=(n);i++) #define REP(i,k,n) for(int i=(k);i<(n);i++) #define all(a) (a).begin(),(a).end() #define output(x,y) cout << fixed << setprecision(y) << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } const ll MOD = 1e9 + 7; // const ll MOD = 998244353; ll upper = MOD + MOD; ll under = -upper; ll UPPER = MOD * MOD; ll UNDER = -UPPER; const long double pi = 3.141592653589793; const int N = 5050505; VE Sieve(N + 100, true); void precalc() { Sieve[0] = Sieve[1] = false; REP(i, 2, N + 1) { if (Sieve[i]) { for (int j = i + i; j <= N; j += i) Sieve[j] = false; } } } int main() { precalc(); int t; cin >> t; while (t--) { ll x, p; scanf("%lld%lld", &x, &p); if (!Sieve[p]) printf("%d\n", -1); else printf("%d\n", gcd(x, p) == 1); } return 0; }