#include <stdio.h>
#include <math.h>
int powMod2(int x, int y, int m){
long long res = 1;
while(y){
if(y % 2 == 1){
res *= x;
res %= m;
}
x *= x;
x %= m;
y /= 2;
}
return res;
}
int main(){
int t;
scanf("%d", &t);
while(t--){
int x, y, m;
scanf("%d%d%d", &x, &y, &m);
printf("%d\n", powMod2(x,y,m));
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+CgppbnQgcG93TW9kMihpbnQgeCwgaW50IHksIGludCBtKXsKCWxvbmcgbG9uZyByZXMgPSAxOwoJd2hpbGUoeSl7CgkJaWYoeSAlIDIgPT0gMSl7CgkJCXJlcyAqPSB4OwoJCQlyZXMgJT0gbTsKCQl9CgkJeCAqPSB4OwoJCXggJT0gbTsKCQl5IC89IDI7Cgl9CglyZXR1cm4gcmVzOwp9CgppbnQgbWFpbigpewoJaW50IHQ7CglzY2FuZigiJWQiLCAmdCk7Cgl3aGlsZSh0LS0pewoJCWludCB4LCB5LCBtOwoJCXNjYW5mKCIlZCVkJWQiLCAmeCwgJnksICZtKTsKCQlwcmludGYoIiVkXG4iLCBwb3dNb2QyKHgseSxtKSk7Cgl9Cn0=