#define _USE_MATH_DEFINES #include using namespace std; //template #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(a);i>(b);i--) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; void tostr(ll x,string& res){while(x)res+=('0'+(x%10)),x/=10; reverse(ALL(res)); return;} template inline bool chmax(T& a,T b){ if(a inline bool chmin(T& a,T b){ if(a>b){a=b;return 1;}return 0; } //end ll mpow(ll x,ll k,ll m){ ll res=1; while(k){ if(k&1)res=(res*x)%m; x=(x*x)%m; k>>=1; } return res; } ll inv(ll a,ll m){ ll b=m,u=1,v=0; while(b){ ll t=a/b; a-=t*b; swap(a,b); u-=t*v; swap(u,v); } u%=m; if(u<0)u+=m; return u; } ll sub_root(ll p,int e,ll a,ll m){//x^(p^e)==a(mod m) ll q=m-1; int s=0; while(q%p==0){q/=p; s++;} int d=s-e; ll pe=mpow(p,e,m),res=mpow(a,((pe-1)*inv(q,pe)%pe*q+1)/pe,m),c=1; while(mpow(c,(m-1)/p,m)==1)c++; c=mpow(c,q,m); map mp; ll v=1,block=sqrt(d*p)+1,bs=mpow(c,mpow(p,s-1,m-1)*block%(m-1),m); rep(i,0,block+1)mp[v]=i,v=v*bs%m; ll gs=inv(mpow(c,mpow(p,s-1,m-1),m),m); cerr<1)a=sub_root(g,1,a,m); return a; } int main(){ int q; cin>>q; while(q--){ ll k,a,m; scanf("%lld%lld%lld",&m,&k,&a); ll res=mod_root(k,a,m); printf("%lld\n",res); //if(res!=-1 and mpow(res,k,m)!=a)cerr<<"-----------------------------------"<