#define _GLIBCXX_DEBUG #include #define FOR(i,a,b) for(ll i=a;i=b;i--) #define per(i,a) ROF(i,a,0) #define pb push_back using namespace std; using ll=long long; using ld=long double; using ch=char; typedef pair P; typedef vector vl; typedef vector vvl; typedef vector

vP; typedef vector vc; typedef vector vvc; const ll MOD=1000000007; const ll MOD2=998244353; const ld PI=acos(-1); const ll INF=1e18; struct edge{ll to,cost;}; struct edge2{ll from,to,cost;}; template bool chmax(T &a, const T& b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T &a, const T& b) { if (a > b) { a = b; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t=1; cin >> t; vector is_prime(5e6+1,true); is_prime[0]=is_prime[1]=false; rep(i,5e6+1){ if(is_prime[i]){ ll j=2*i; while(j<=5e6){ is_prime[j]=false; j+=i; } } } rep(tt,t){ ll A,P; cin >> A >> P; if(!is_prime[P]){ cout << -1 << '\n'; } else{ if(A%P==0){ cout << 0 << '\n'; } else{ cout << 1 << '\n'; } } } }