#include using namespace std; using ll = long long; int MAX = 1e7; vector prime(MAX+1); void f() { for(int i = 2; i <= MAX; i++) { if(prime[i])continue; for(int j = i + i; j <= MAX; j += i)prime[j] = true; } } void solve() { ll a, p; cin >> a >> p; if(prime[p]) { cout << -1 << endl; } else if(a % p == 0LL) { cout << 0 << endl; } else { cout << 1 << endl; } } int main() { cin.tie(0);ios::sync_with_stdio(0); f(); int T; cin >> T; while(T--)solve(); }