#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; } mt19937 mt; int modroot(int k,int y,int m){ if(m==2)return y&1; vector ds; int tmp=m-1; for(int p=2;p*p<=tmp;p++)if(tmp%p==0){ ds.push_back(p); if(p*p!=tmp)ds.push_back(tmp/p); } int g=2; while(1){ bool f=1; for(int d:ds)if(mpow(g,d,m)==1){f=0; break;} if(f)break; g++; } unordered_map mp; int cur=y,ig=mpow(g,m-2,m),block=sqrt(m); rep(i,0,block+2){mp[cur]=i; cur=(1LL*cur*ig)%m;} int gs=mpow(g,block,m),x=-1,pos=0; cur=1; while(x==-1){ if(mp.count(cur))x=pos+mp[cur]; cur=(1LL*cur*gs)%m; pos+=block; } int 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))%tmp,m); } int main(){ int q; cin>>q; while(q--){ int k,y,m; cin>>m>>k>>y; cout<