#include using namespace std; #define ll long long #define FOR(i,l,r) for(ll i=(l);i<(r);++i) #define REP(i,n) FOR(i,0,n) vector isprime(5000010, false); void make_table(){ for(int i = 2; i <= 5000000; i++) isprime[i] = true; for(int i = 2; i <= 5000000; i++) if(isprime[i]) for(int j = 2 * i; j <= 5000000; j += i) isprime[j] = false; } bool prime(int n){ for(int i = 2; i <= sqrt(n); i++) if(n % i == 0) return false; return true; } int main(){ int n; cin >> n; REP(i, n){ ll a, p; cin >> a >> p; if(!prime(p)) cout << -1 << endl; else cout << (a % p ? 1 : 0) << endl; } }