#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; long long A,P; void solve() { cin >> A >> P; bool prime = true; for(int i = 2;i*i <= P;i++) if(P%i == 0) prime = false; if(prime) { if(A%P == 0) cout << 0 << "\n"; else cout << 1 << "\n"; } else cout << -1 << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tt = 1; cin >> tt; while(tt--) solve(); }