#include using namespace std; #define REP(i,n) for(int i=0; i<(int)(n); i++) template void psieve(bool (&p)[N]) { for (int i = 0; i < N; i++) p[i] = true; p[0] = p[1] = false; for (int i = 2; i * i < N; i++) { if (!p[i]) continue; for (int j = i * i; j < N; j += i) p[j] = false; } } bool prime[5000000+1]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); psieve(prime); int T; cin >> T; REP (i, T) { long long A; int P; cin >> A >> P; if (!prime[P]) cout << -1 << endl; else if (A % P == 0) cout << 0 << endl; else cout << 1 << endl; } return 0; }