/* -*- coding: utf-8 -*- * * 1140.cc: No.1140 EXPotentiaLLL! - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_P = 5000000; /* typedef */ typedef long long ll; /* global variables */ bool primes[MAX_P + 1]; /* subroutines */ void gen_primes(int maxp) { memset(primes, true, sizeof(primes)); primes[0] = primes[1] = false; for (int p = 2; p * p <= maxp; p++) if (primes[p]) for (int q = p * p; q <= maxp; q += p) primes[q] = false; } /* main */ int main() { gen_primes(MAX_P); int t; scanf("%d", &t); while (t--) { ll a; int p; scanf("%lld%d", &a, &p); if (primes[p]) printf("%d\n", (a % p == 0) ? 0 : 1); else puts("-1"); } return 0; }