#include using namespace std; #define DEBUG(x) cerr<<#x<<": "< #define vl vector #define vii vector< vector > #define vll vector< vector > #define vs vector #define pii pair #define pis pair #define psi pair #define pll pair template pair operator+(const pair &s, const pair &t) { return pair(s.first + t.first, s.second + t.second); } template pair operator-(const pair &s, const pair &t) { return pair(s.first - t.first, s.second - t.second); } template ostream& operator<<(ostream& os, pair p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define X first #define Y second #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep1(i,n) for(int i=1;i<=(int)(n);i++) #define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--) #define rrep1(i,n) for(int i=(int)(n);i>0;i--) #define REP(i,a,b) for(int i=a;i bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = (ll)1e18 + 1; const long double pi = 3.1415926535897932384626433832795028841971L; #define Sp(p) cout< bool is_prime_impl(const Uint &n, const Uint *witness, BinOp modmul) { if (n == 2) return true; if (n < 2 || n % 2 == 0) return false; const Uint m = n - 1, d = m / (m & -m); auto modpow = [&](Uint a, Uint b) { Uint res = 1; for (; b; b /= 2) { if (b & 1) res = modmul(res, a); a = modmul(a, a); } return res; }; auto suspect = [&](Uint a, Uint t) { a = modpow(a, t); while (t != n - 1 && a != 1 && a != n - 1) { a = modmul(a, a); t = modmul(t, 2); } return a == n - 1 || t % 2 == 1; }; for (const Uint *w = witness; *w; w++) { if (*w % n != 0 && !suspect(*w, d)) return false; } return true; } bool is_prime(const u128 &n) { assert(n < 1ULL << 63); if (n < 1ULL << 32) { // n < 2^32 constexpr u64 witness[] = {2, 7, 61, 0}; auto modmul = [&](u64 a, u64 b) { return a * b % n; }; return is_prime_impl(n, witness, modmul); } else { // n < 2^63 constexpr u128 witness[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022, 0}; // if u128 is available auto modmul = [&](u128 a, u128 b) { return a * b % n; }; return is_prime_impl(n, witness, modmul); } } signed main() { int t; cin >> t; while (t--) { ll a, p; cin >> a >> p; if (is_prime(p)) { cout << 1 << endl; } else { cout << -1 << endl; } } }