#include using namespace std; using ll = long long; using Graph = vector>; const ll mod = 1000000007; const int INF = 1001001001; bool is_prime[10000000]; void init(){ for(int i = 0; i < 6000000; i++){ is_prime[i] = true; } for(int i = 2; i < 6000000; i++){ if(!is_prime[i]) continue; for(int j = 2; i*j < 6000000; j++){ is_prime[i*j] = false; } } } int main(){ int t; cin >> t; init(); is_prime[0] = is_prime[1] = false; while(t--){ ll a, p; cin >> a >> p; if(!is_prime[p]){ cout << -1 << endl; continue; } if(a % p == 0) cout << 0 << endl; else cout << 1 << endl; } }