#include using namespace std; typedef long long ll; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++) #define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++) #define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--) #define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((ll)(x).size()) #define len(x) ((ll)(x).length()) #define endl "\n" struct isPrime { private: vector data; public: isPrime() {} isPrime(int n) { data.resize(n + 1, true); data[0] = data[1] = false; for(long long i = 2; (i * i) <= n; i++) { if (!data[i]) continue; for(long long j = 2; (i * j) <= n; j++) { data[i * j] = false; } } } bool is_prime(int n) { return data[n]; } vector get_primes_vector() { vector ans; int n = data.size(); for(int i = 0; i < n; i++) { if (data[i]) ans.push_back(i); } return ans; } set get_primes_set() { set ans; int n = data.size(); for(int i = 0; i < n; i++) { if (data[i]) ans.insert(i); } return ans; } }; isPrime ip(5 * 1e6); void solve() { ll a, p; cin >> a >> p; if (!ip.is_prime(p)) { cout << -1 << endl; return; } cout << ((__gcd(a, p) == 1) ? 1 : 0) << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); // ifstream in("input.txt"); // cin.rdbuf(in.rdbuf()); ll t; cin >> t; rep(i, t) solve(); return 0; }