#include using namespace std; using ll = long long; #define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i) #define all(a) (a).begin(), (a).end() int main() { cin.tie(nullptr); int T; cin >> T; vector isPrime(5e6 + 1, true); isPrime[0] = false; isPrime[1] = false; rep(p, 2, 5e6) { if (!isPrime[p]) continue; for (int q = p * 2; q <= 5e6; q += p) isPrime[q] = false; } rep(test, 0, T) { ll A, P; cin >> A >> P; if (!isPrime[P]) { cout << -1 << '\n'; continue; } ll g = gcd(A, P); if (g != 1) cout << 0 << '\n'; else cout << 1 << '\n'; } }