#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#define ll long long
using namespace std;
int powermod(ll a,ll b,ll c){
ll ans=1;
a = a % c;
while(b>0){
if(b%2) ans = (ans * a) %c;
b = b/2;
a = (a * a)%c;
}
return ans;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
ll a,b,c;
scanf("%I64d%I64d%I64d",&a,&b,&c);
printf("%d\n",powermod(a,b,c));
}
return 0;