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