#include using namespace std; using ll = long long; int MAX = 1e7; vector prime(MAX+1); void f() { prime[0] = prime[1] = 1; 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 { cout << (a % p == 0) << endl; } } int main() { cin.tie(0);ios::sync_with_stdio(0); f(); int T; cin >> T; while(T--)solve(); }