#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 int mpow(int x,ll t,int m){ int res=1; while(t){ if(t&1)res=(1LL*res*x)%m; x=(1LL*x*x)%m; t>>=1; } return res; } int phi(int n){ int res=n; for(int i=2;i*i<=n;i++)if(n%i==0){ res=res/i*(i-1); while(n%i==0)n/=i; } if(n!=1)res=res/n*(n-1); return res; } int modroot(int k,int y,int m){ if(m==2)return y%m; int g=2; while(mpow(g,m>>1,m)==1)g++; unordered_map mp; int ig=mpow(g,m-2,m),val=y,sqrtm=ceil(sqrt(m)); rep(i,0,sqrtm+1)mp[val]=i,val=(1LL*val*ig)%m; val=1; int x=-1,pos=0,pg=mpow(g,sqrtm,m); while(x==-1){ if(mp.count(val))x=pos+mp[val]; val=(1LL*val*pg)%m; pos+=sqrtm; } int tmp=m-1,gcd=__gcd(tmp,k); if(x%gcd)return -1; tmp/=gcd; x/=gcd; k/=gcd; return mpow(g,1LL*x*mpow(k,phi(tmp)-1,tmp),m); } int main(){ int q; cin>>q; while(q--){ int k,y,m; cin>>m>>k>>y; cout<