#include #include #include using namespace std; using int64 = long long; struct aaa{aaa(){cin.tie(nullptr); ios::sync_with_stdio(false); cout<= MOD) x-=MOD; return *this; } mint& operator-=(const mint a) { if ( (x+= MOD-a.x) >= MOD) x-= MOD; return *this; } mint& operator*=(const mint a) { (x*=a.x) %= MOD; return *this; } mint& operator/=(const mint a) { return (*this) *= a.inv();} mint operator+(const mint a) const { mint res(*this); return res+=a; } mint operator-(const mint a) const { mint res(*this); return res-=a; } mint operator*(const mint a) const { mint res(*this); return res*=a; } mint operator/(const mint a) const { mint res(*this); return res/=a; } mint inv() const { return pow(MOD-2);} friend ostream& operator<<(ostream &os, const mint a) noexcept { return os << a.x; } constexpr bool operator == (const mint& r) const noexcept { return this->x == r.x; } constexpr bool operator != (const mint& r) const noexcept { return this->x != r.x; } mint pow(long long t) const { if (!t) return mint(1); mint a = pow(t>>1); a*=a; if(t&1) a*= *this; return a; } }; vector isPrime(6*1000000, true); void primes(const int64 _max, vector& isPrime) { isPrime[0] = isPrime[1] = false; for (int64 i=4; i<=_max; i+=2) isPrime[i] = false; for (int64 i=3; i<=_max; i+=2) { if (!isPrime[i]) continue; for (int64 j=i*i; j<=_max; j+=i) { isPrime[j] = false; } } } void solve() { int64 a,p; cin >> a >> p; MOD = p; if (isPrime[p]) { cout << (a%p!=0) << endl; } else { cout << -1 << endl; } } int main() { int t; cin >> t; primes(5*1000000, isPrime); for (int i=0; i