// #include using namespace std; #define fi first #define se second #define all(x) x.begin(), x.end() #define lch (o << 1) #define rch (o << 1 | 1) typedef double db; typedef long long ll; typedef unsigned int ui; typedef pair pint; typedef tuple tint; const int N = 5e6 + 5; const int INF = 0x3f3f3f3f; const ll INF_LL = 0x3f3f3f3f3f3f3f3f; bool notPri[N]; int pri[N]; void Sieve() { notPri[0] = 1; for (int i = 2; i < N; i++) { if (!notPri[i]) { pri[++pri[0]] = i; } for (int j = 1; j <= pri[0]; j++) { int p = pri[j]; if (i * p >= N) break; notPri[i * p] = 1; if (i % p == 0) break; } } } int main() { Sieve(); int t; scanf("%d", &t); while (t--) { ll a; int p; scanf("%lld%d", &a, &p); if (notPri[p]) printf("-1\n"); else printf("%d\n", (a % p != 0)); } return 0; }