#include<iostream>
using namespace std;
typedef unsigned long long ULL;
const int maxn = 1000+10;
ULL F[maxn*maxn];
ULL quick_pow_mod(ULL a,ULL n,ULL m){
if (n == 0)return 1;
ULL res = 1;
while (n > 0){
if (n & 1)
res = res*a%m;
a = (a%m)*(a%m)%m;
n >>= 1;
}
return res;
}
int main(){
ULL a, b, n,t,T,i;
cin >> t;
while (t--){
cin >> a >> b >> n;
F[0] = 0; F[1] = 1;
if (n == 1){ printf("0
"); continue; }
for (i = 2;i<n*n; i++){
F[i] = (F[i - 1] + F[i - 2]) % n;
if (F[i] == 1 && F[i - 1] == 0)break;
}
T = i - 1;
ULL id = quick_pow_mod(a, b, T);
cout << F[id] << endl;
}
return 0;
}