#include #include using namespace std; namespace my{ #define eb emplace_back #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define FO(n) for(ll ij=n;ij--;) #define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);i=i##stop;i-=i##step) #define fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a) #define single_testcase void solve();}int main(){my::io();my::solve();}namespace my{ void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<>(istream&i,ulll&x){ull t;i>>t;x=t;return i;} ostream&operator<<(ostream&o,const ulll&x){return(x<10?o:o<>(istream&i,lll&x){ll t;i>>t;x=t;return i;} ostream&operator<<(ostream&o,const lll&x){return o<0?x:-x);} constexpr auto range(bool s,auto...a){arrayr{0,0,1};ll I=0;((r[I++]=a),...);if(!s&&I==1)swap(r[0],r[1]);r[0]-=s;return r;} constexpr char newline=10; constexpr char space=32; lll pw(lll x,ll n){assert(n>=0);lll r=1;while(n)n&1?r*=x:r,x*=x,n>>=1;return r;} templatestruct pair{ A a;B b; pair()=default; pair(A a,B b):a(a),b(b){} pair(const std::pair&p):a(p.first),b(p.second){} auto operator<=>(const pair&)const=default; friend ostream&operator<<(ostream&o,const pair&p){return o<>auto&sort(auto&a,const F&f={}){ranges::sort(a,f);return a;} templateostream&operator<<(ostream&o,const std::pair&p){return o<ostream&operator<<(ostream&o,const array&a){fo(i,n)o<ostream&operator<<(ostream&o,const unordered_map&m){fe(m,e)o<concept vectorial=is_base_of_v,V>; templateistream&operator>>(istream&i,vector&v){fe(v,e)i>>e;return i;} templateostream&operator<<(ostream&o,const vector&v){fe(v,e)o<?newline:space);return o;} templatestruct vec:vector{ using vector::vector; vec(const vector&v){vector::operator=(v);} vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;} vec operator^(const vec&u)const{return vec{*this}^=u;} vec&operator+=(const vec&u){vec&v=*this;fo(i,v.size())v[i]+=u[i];return v;} vec&operator-=(const vec&u){vec&v=*this;fo(i,v.size())v[i]-=u[i];return v;} vec operator+(const vec&u)const{return vec{*this}+=u;} vec operator-(const vec&u)const{return vec{*this}-=u;} vec&operator++(){fe(*this,e)++e;return*this;} vec&operator--(){fe(*this,e)--e;return*this;} vec operator-()const{vec v=*this;fe(v,e)e=-e;return v;} }; void lin(auto&...a){(cin>>...>>a);} templatevoid pp(const auto&...a){ll n=sizeof...(a);((cout<0,c)),...);cout<auto rle(const vec&a){vec>r;fe(a,e)r.size()&&e==r.back().a?++r.back().b:r.eb(e,1).b;return r;} templateauto rce(veca){return rle(sort(a));} constexpr uint64_t kth_root_floor(uint64_t a,ll k){ if (k==1)return a; auto within=[&](uint32_t x){uint64_t t=1;fo(k)if(__builtin_mul_overflow(t,x,&t))return false;return t<=a;}; uint64_t r=0; of(i,sizeof(uint32_t)*CHAR_BIT)if(within(r|(1u<T e(){return x;} } struct montgomery64{ using modular=montgomery64; using i64=__int64_t; using u64=__uint64_t; using u128=__uint128_t; static inline u64 N; static inline u64 N_inv; static inline u64 R2; static int set_mod(u64 N){ if(modular::N==N)return 0; assert(N<(1ULL<<63)); assert(N&1); modular::N=N; R2=-u128(N)%N; N_inv=N; fo(5)N_inv*=2-N*N_inv; assert(N*N_inv==1); return 0; } static inline int init=set_mod(998244353); static u64 mod(){return N;} u64 a; montgomery64(const i64&a=0):a(reduce((u128)(a%(i64)N+N)*R2)){} static u64 reduce(const u128&T){ u128 r=(T+u128(u64(T)*-N_inv)*N)>>64; return r>=N?r-N:r; } auto&operator+=(const modular&b){if((a+=b.a)>=N)a-=N;return*this;} auto&operator-=(const modular&b){if(i64(a-=b.a)<0)a+=N;return*this;} auto&operator*=(const modular&b){a=reduce(u128(a)*b.a);return*this;} auto&operator/=(const modular&b){*this*=b.inv();return*this;} friend auto operator+(const modular&a,const modular&b){return modular{a}+=b;} friend auto operator-(const modular&a,const modular&b){return modular{a}-=b;} friend auto operator*(const modular&a,const modular&b){return modular{a}*=b;} friend auto operator/(const modular&a,const modular&b){return modular{a}/=b;} friend bool operator==(const modular&a,const modular&b){return a.a==b.a;} auto operator-()const{return modular{}-modular{*this};} modular pow(u128 n)const{ modular r{1},x{*this}; while(n){ if(n&1)r*=x; x*=x; n>>=1; } return r; } modular inv()const{u64 a=val(),b=N,u=1,v=0;assert(gcd(a,b)==1);while(b)swap(u-=a/b*v,v),swap(a-=a/b*b,b);return u;} u64 val()const{return reduce(a);} friend istream&operator>>(istream&i,montgomery64&b){ll t;i>>t;b=t;return i;} friend ostream&operator<<(ostream&o,const montgomery64&b){return o<v{};ll I=0;((v[I++]=a),...);auto[l,r]=v;if(I==1)swap(l,r);static ll t=495;t^=t<<7,t^=t>>9;return las){ ll d=n-1; while(~d&1)d>>=1; using modular=montgomery64; auto pre_mod=modular::mod(); modular::set_mod(n); modular one=1,minus_one=n-1; fe(as,a){ if(a%n==0)continue; ll t=d; modular y=modular(a).pow(t); while(t!=n-1&&y!=one&&y!=minus_one)y*=y,t<<=1; if(y!=minus_one&&~t&1)return modular::set_mod(pre_mod),0; } return modular::set_mod(pre_mod),1; } bool is_prime(ll n){ if(~n&1)return n==2; if(n<=1)return 0; if(n<4759123141LL)return miller_rabin(n,{2,7,61}); return miller_rabin(n,{2,325,9375,28178,450775,9780504,1795265022}); } ll pollard_rho(ll n){ if(~n&1)return 2; if(is_prime(n))return n; using modular=montgomery64; auto pre_mod=modular::mod(); modular::set_mod(n); modular R,one=1; auto f=[&](const modular&x){return x*x+R;}; while(1){ modular x,y,ys,q=one; R=rand(2,n),y=rand(2,n); ll g=1; constexpr ll m=128; for(ll r=1;g==1;r<<=1){ x=y; fo(r)y=f(y); for(ll k=0;g==1&&k0); auto f=[](auto&f,ll m){ if(m==1)return vec{}; ll d=pollard_rho(m); return d==m?vec{d}:f(f,d)^f(f,m/d); }; return rce(f(f,n)); } templatecommon_type_tgcd(T a,U b){return b?gcd(b,a%b):a>0?a:-a;} templateauto gcd(const A&...a){common_type_tr=0;((r=gcd(r,a)),...);return r;} auto mod(auto a,auto m){return(a%=m)<0?a+m:a;} auto inv_mod(auto x,auto m){assert(gcd(x,m)==1);decltype(x)a=mod(x,m),b=m,u=1,v=0;while(b)swap(u-=a/b*v,v),swap(a-=a/b*b,b);return mod(u,m);} ll kth_root_mod_prime(ll a,ll k,ll P){ a=mod(a,P); if(k)if(a==0)return 0; k=mod(k,P-1); if(k==0)return(a==1?1:-1); if(a==0)return 0; if(P==2)return a; using modular=montgomery64; auto pre_mod=modular::mod(); modular::set_mod(P); ll g=gcd(k,P-1); if(modular(a).pow((P-1)/g)!=1)return modular::set_mod(pre_mod),-1; g=mod(g,P-1); if(g==0)return modular::set_mod(pre_mod),1; a=modular(a).pow(inv_mod(k/g,(P-1)/g)).val(); const modular one=1; auto peth_root_mod_prime=[&](modular c,ll p,ll e){ ll t=0; ll s=P-1; while(s%p==0)++t,s/=p; modular v=one; while(v.pow((P-1)/p)==one)v+=one; modular vs=v.pow(s); ll pe=pw(p,e); ll u=inv_mod(-s,pe); modular z=c.pow(((lll)s*u+1)/pe); modular c_inv=c.inv(); modular A=vs.pow(pw(p,t-1)),A_inv=A.inv(); while(1){ modular zpe_c=z.pow(pe)*c_inv; ll t_dash=0; modular zpe_c_pow=zpe_c; while(zpe_c_pow!=one){ zpe_c_pow=zpe_c_pow.pow(p); ++t_dash; } if(t_dash==0)break; ll E=t-t_dash; ll q=-1; modular B=zpe_c.inv().pow(pw(p,t_dash-1)); ll R=sqrt_floor(p)+1; unordered_mapdict; modular A_inv_R=A_inv.pow(R); modular A_inv_R_pow=1; fo(i,R){ dict[(B*A_inv_R_pow).val()]=i; A_inv_R_pow*=A_inv_R; } modular A_pow=1; fo(j,R){ if(ll key=A_pow.val();dict.contains(key)){q=R*dict[key]+j;break;} A_pow*=A; } z*=vs.pow(pw(p,E-e)).pow(q); } return z.val(); }; fe(factorize(g),p,e)a=peth_root_mod_prime(a,p,e); return modular::set_mod(pre_mod),a; } single_testcase void solve(){ LL(T); fo(T){ LL(P,k,a); pp(kth_root_mod_prime(a,k,P)); } }}