#include using namespace std; class SieveOfEratosthenes { private: std::vector sieve; public: SieveOfEratosthenes(int n) : sieve(n + 1) { std::iota(sieve.begin(), sieve.end(), 0); for (int i = 2; i * i < n + 1; i++) { if (sieve[i] != i) continue; for (int j = i * i; j < n + 1; j += i) { if (sieve[j] == j) sieve[j] = i; } } } bool is_prime(int x) const { return x != 0 && x != 1 && sieve[x] == x; } std::vector factor(int x) const { std::vector res; while (x > 1) { res.emplace_back(sieve[x]); x /= sieve[x]; } return res; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; SieveOfEratosthenes soe(10000000); auto solve = [&]() { int64_t A, P; cin >> A >> P; if (soe.is_prime(P)) { if (gcd(A, P) == 1) { cout << 1 << '\n'; } else { cout << 0 << '\n'; } } else { cout << -1 << '\n'; } }; for (int i = 0; i < T; i++) { solve(); } return 0; }