#include #include #include #define ll long long //ll未対応 ll powmod(ll a,ll n,int m){ ll x=1; a%=m; while(n){ if(n%2)x=x*a%m; a=a*a%m; n/=2; } return x; } ll gcd(ll x,ll y){ while(y){ ll t=x%y; x=y; y=t; } return x; } ll inv(ll a,ll p){ ll b=p,tx,ty,t,x=1,y=0,x2=0,y2=1; while(b){ tx=x-a/b*x2;x=x2;x2=tx; ty=y-a/b*y2;y=y2;y2=ty; t=a%b;a=b;b=t; } if(a!=1)return-1; return x>0?x:x+p; } ll intsqrt(ll n){ ll x=sqrt(n); while((x+1)*(x+1)<=n)x++; return x; } ll intcbrt(ll n){ ll x=cbrt(n); while((x+1)*(x+1)*(x+1)<=n)x++; return x; } int subsub(ll x,ll y,int p,int mod){ //(Z/modZ)^*における位数pの元x,yに対し、x^n*y=1を満たすnを返す int cnt=0; while(y!=1){ y=y*x%mod; cnt++; } return cnt; } ll modrootsub(ll a,ll p,ll e,ll mod){ ll q=mod-1; int s=0; while(q%p==0)q/=p,s++; // Z/(p^s)Z * Z/qZ ll pe=1; for(int i=0;i=2なので //そのようなqであってp^(1/4)より大きなものは高々1つ for(ll i=2;i*i*i*i<=p-1;i++)if(d2%i==0){ d2/=i; int e=1; while(d2%i==0)d2/=i,e++; a=modrootsub(a,i,e,p); } if(d2!=1){ //d2はp^(1/4)より大きな素数qのperfect power ll q2=intsqrt(d2); ll q3=intcbrt(d2); if(q2*q2==d2)a=modrootsub(a,q2,2,p); else if(q3*q3*q3==d2)a=modrootsub(a,q3,3,p); else a=modrootsub(a,d2,1,p); } return a; } int main(){ int t; scanf("%d",&t); while(t--){ ll a,n,m; scanf("%lld%lld%lld",&m,&n,&a); printf("%lld\n",modroot(a,n,m)); } }