#include using namespace std; #ifdef LOCAL #include "debug.h" #else #define DEBUG(...) #endif vector sieve(int n) { vector b(n / 3 + 1, true); vector res{2, 3}; for (int p = 5, d = 4; p * p <= n; p += d = 6 - d) if (b[p / 3]) for (int i = p * p, di = p % 3 * 2 * p; i <= n; i += di = 6 * p - di) b[i / 3] = false; for (int p = 5, d = 4; p <= n; p += d = 6 - d) if(b[p / 3]) res.push_back(p); while (not res.empty() and res.back() > n) res.pop_back(); return res; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); auto primes = sieve(5e6); int tt; cin >> tt; using ll = long long; while (tt--) { ll a; cin >> a; int p; cin >> p; if (binary_search(begin(primes), end(primes), p)) { if (a % p) { cout << "1\n"; } else { cout << "0\n"; } } else { cout << "-1\n"; } } }