#include using namespace std; #define ll long long #define rep(i, n) for (int i = 0; i < (n); i++) #define P pair int main() { vector memo(5000005, true); for (int i = 2; i <= 5000000; i++) { if (memo[i]) { for (int j = i*2; j <= 5000000; j += i) memo[j] = false; } } auto isPrime = [&](int x) { return memo[x]; }; int t; cin >> t; rep(ti, t) { ll a; int p; cin >> a >> p; if (isPrime(p)) { if (a%p == 0) cout << 0 << endl; else cout << 1 << endl; } else { cout << -1 << endl; } } }