#include using namespace std; #define ll long long bool isprime[5000010]; void eratosthenes(){ isprime[0] = isprime[1] = false; for(int i = 2; i <= 5000000; i++) isprime[i] = true; for(int i = 2; i <= 5000000; i++) if(isprime[i]) for(int j = 2 * i; j <= 5000000; j += i) isprime[j] = false; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); eratosthenes(); ll n, a, p; cin >> n; while(cin >> a >> p){ if(isprime[p]) cout << (a % p ? 1 : 0) << '\n'; else cout << -1 << '\n'; } }