#include using namespace std; const int MX = 5500000; int main() { vector prime(MX , 0); for (int i = 2; i < MX; i++) { for (int j = i; j < MX; j += i) { prime[j]++; } } int t; cin >> t; while (t--) { long long a; int p; cin >> a >> p; if (prime[p] > 1) { cout << -1 << endl; } else { if (a % p == 0) cout << 0 << endl; else cout << 1 << endl; } } return 0; }