#include #include #include using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); uint32_t T, i; cin >> T; vector A(T); vector P(T); for (i = 0; i != T; ++i) cin >> A[i] >> P[i]; vector is_prime(5000001, true); is_prime[0] = is_prime[1] = false; for (i = 2; i <= 5000000; ++i) if (is_prime[i]) for (uint32_t j = i * 2; j <= 5000000; j += i) is_prime[j] = false; for (i = 0; i != T; ++i) { if (!is_prime[P[i]]) cout << "-1\n"; else if (A[i] % P[i] == 0) cout << "0\n"; else cout << "1\n"; } return 0; }