here is the code:::::::::
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll r(ll a,ll n,ll p)
{
ll result = a%p;
for(int i=2;i<=n;i++)
{
result = pow(result,i);
result = result%p;
if(result ==1)
break;
}
return result;
}
int main() {
ll t,a,n,p; cin>>t;
while(t)
{
cin>>a>>n>>p;
cout<<r(a,n,p)<<endl;
t–;
}
return 0;
}