![这里写图片描述](https://img-blog.csdn.net/20180713160323270?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0ZpcmVfdG9fY2hlYXRf/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
#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;
}