#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef unsigned long long ll; typedef long double ld; typedef pair P; int is_prime[5000001]; void init () { fill(is_prime, is_prime + 5000009, 1); is_prime[0] = is_prime[1] = 0; for (int i = 2; i <= 5000000; i++) { if (!is_prime[i]) continue; for (int j = i + i; j <= 5000000; j += i) is_prime[j] = 0; } } int main() { int t; cin >> t; init(); while (t--) { ll a; int p; cin >> a >> p; if (is_prime[p]) cout << (a % p == 0 ? 0 : 1) << "\n"; else cout << -1 << "\n"; } return 0; }