#include
#include
#include
using namespace std;
long long fast(long long a,long long b ,long long c){
long long temp=1;
if(b==0)
return 1;
if(b==1)
return a%c;
temp = fast(a,b/2,c);
temp = temp*temp%c;
if(b&0x1){
temp = temp*a%c;
}
return temp;
}
int main(){
int n;
scanf("%d",&n);
while(n--){
long long a,b,c;
scanf("%lld",&a);
scanf("%lld",&b);
scanf("%lld",&c);
long long x = fast(a,b,c);
printf("%lld
",x);
}
return 0;
}