#include #include #include #include #include #include #include #include #include #include #include #include #include #include typedef unsigned long long ULLONG; typedef long long LLONG; static const LLONG MOD_NUM = 1000000007; //998244353; template static void get(_T& a) { std::cin >> a; } template static void get(_T& a, _T& b) { std::cin >> a >> b; } template static void get(_T& a, _T& b, _T& c) { std::cin >> a >> b >> c; } template static _T tp_abs(_T a) { if (a < (_T)0) { a *= (_T)-1; } return a; } static void exec(); int main() { exec(); fflush(stdout); return 0; } static void eratosthenes(int targ, std::set& primes) { std::vector is_prime(targ + 1, 1); for (int i = 2; i * i <= targ; i++) { if (is_prime[i]) { for (int j = 0; (i * (j + 2)) <= targ; j++) { is_prime[i * (j + 2)] = 0; } } } for (int i = 2; i <= targ; i++) { if (is_prime[i]) primes.insert(i); } } template static _T tp_modpow(_T base, _T exp, _T mod = (_T)1) { _T ans = 1; while (exp > 0) { if (exp & 1) { ans = (ans * base) % mod; } base = (base * base) % mod; exp >>= 1; } return ans; } template static _T tp_modinv(_T a, _T mod) { return tp_modpow<_T>(a, mod - 2, mod); } static void exec() { int T; get(T); std::set primes; eratosthenes(5 * 1000000 + 1, primes); LLONG A; int P; while (T--) { scanf("%lld %d", &A, &P); LLONG ans = -1; if (primes.find(P) != primes.end()) { if (A % P) { ans = 1; } else { ans = 0; } } printf("%lld\n", ans); } }