#include #include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)n - 1; i > -1; i--) #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double #define INF 1000000000000000000 typedef pair pll; long long GCD(long long a, long long b) { if (b == 0) return a; else return GCD(b, a % b); } int main() { cin.tie(0); ios::sync_with_stdio(false); int MAX = 5 * pow(10, 6) + 5; vector is_prime(MAX, 1); is_prime[0] = 0, is_prime[1] = 0; for (int i = 2; i < MAX; ++i) { if (!is_prime[i]) continue; for (int j = 2 * i; j < MAX; j += i) is_prime[j] = 0; } int T; cin >> T; rep(i, T) { ll a, p; cin >> a >> p; if (is_prime[p]) { ll gcd = GCD(a, p); if (gcd == 1) { cout << 1 << endl; } else { cout << 0 << endl; } } else { cout << -1 << endl; } } return 0; }