//为啥循环节一定要是0 1开头
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdlib> 7 #include<queue> 8 #include<vector> 9 #include<map> 10 #include<stack> 11 #include<string> 12 13 using namespace std; 14 15 const unsigned long long MAXN=1001; 16 17 unsigned long long T; 18 unsigned long long a,b; 19 unsigned long long n; 20 unsigned long long f[MAXN*MAXN]; 21 22 unsigned long long pow(unsigned long long a,unsigned long long b,unsigned long long n){ 23 unsigned long long tmp=a%n; 24 unsigned long long ans=1; 25 while (b!=0){ 26 if (b&1==1){ 27 ans=(ans*tmp)%n; 28 } 29 tmp=(tmp*tmp)%n; 30 b=b>>1; 31 } 32 //printf("%d ",ans); 33 return ans; 34 } 35 36 int main(){ 37 scanf("%llu",&T); 38 while (T--){ 39 scanf("%llu%llu%llu",&a,&b,&n); 40 f[0]=0%n; 41 f[1]=1%n; 42 unsigned long long len; 43 unsigned long long now=1; 44 while (1){ 45 f[now+1]=(f[now]+f[now-1])%n; 46 if (f[now]==f[0] && f[now+1]==f[1]){ 47 len=now; 48 break; 49 } 50 now++; 51 } 52 //printf("%d ",len); 53 printf("%llu ",f[pow(a,b,len)]); 54 } 55 return 0; 56 } 57 /* 58 3 59 1 1 2 60 2 3 1000 61 18446744073709551615 18446744073709551615 1000 62 63 1 64 18446744073709551615 18446744073709551615 1000 65 */ 66 /* 67 1 68 21 69 250 70 */