include[HTML_REMOVED]
using namespace std;
typedef long long LL;
int output(int a,int b,int p){
int res = 1;
while(b){
if(b & 1) res = (LL)res * a % p;
b >>= 1;
a = (LL)a * a % p;
}
return res;
}
int main(){
int n;cin>>n;
while(n–){
int a,b,p;
cin>>a>>b>>p;
int ret = output(a,b,p);
cout << ret << endl;
}
return 0;
}