#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; //typedef long long int ll; using ll=__int128_t; typedef pair P; ll gcd(ll a, ll b){ if(b==0) return a; return gcd(b, a%b); } ll powmod(ll a, ll k, ll mod){ ll ap=a, ans=1; while(k){ if(k&1){ ans*=ap; ans%=mod; } ap=ap*ap; ap%=mod; k>>=1; } return ans; } ll inv(ll a, ll m){ ll b=m, x=1, y=0; while(b>0){ ll t=a/b; swap(a-=t*b, b); swap(x-=t*y, y); } return (x%m+m)%m; } vector

fac(ll x){ vector

ret; for(ll i=2; i*i<=x; i++){ if(x%i==0){ int e=0; while(x%i==0){ x/=i; e++; } ret.push_back({i, e}); } } if(x>1) ret.push_back({x, 1}); return ret; } mt19937 mt(334); ll solve1(ll p, ll q, int e, ll a){ int s=0; ll r=p-1; ll qs=1; while(r%q==0){ r/=q; qs*=q; s++; } ll qp=1; for(int i=0; i=s){ if(powmod(at, qp, p)!=a) return -1; else return at; } uniform_int_distribution<> rnd(1, p-1); ll rv; while(1){ rv=powmod(rnd(mt), r, p); if(powmod(rv, qs/q, p)!=1) break; } int i=0; ll qi=1; ll sq=1; while(sq*sq v(sq); ll rvi=powmod(rv, qp*qq*(p-2)%(p-1), p), rvp=powmod(rv, sq*qp*qq, p); ll x=powmod(powmod(at, qp, p)*inva%p, qq*(p-2)%(p-1), p), y=1; for(int j=0; j f=fac(g); ll ret=1; ll gp=1; for(auto r:f){ ll qp=1; for(int i=0; i=0) ret=powmod(ret, t, p); else ret=powmod(inv(ret, p), -t, p); if(s>=0) x=powmod(x, s, p); else x=powmod(inv(x, p), -s, p); (ret*=x)%=p; gp*=qp; } if(powmod(ret, k, p)!=a1) return -1; return ret; } int main() { int t; scanf("%d", &t); for(int i=0; i