#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; bool b[5000010] = { false }; long long gcd(long long a, long long b) { if (a % b == 0) { return (b); } else { return (gcd(b, a % b)); } } int main() { for (int i = 2; i <= 5000000; i++) { if (!b[i]) { for (int j = i + i; j <= 5000000; j += i) { b[j] = true; } } } int t; cin >> t; long long a, p; for (int i = 0; i < t; i++) { cin >> a >> p; if (b[p]) { cout << "-1" << endl; } else { if (gcd(a, p) == 1) { cout << "1" << endl; } else { cout << "0" << endl; } } } return 0; }