1 #include<iostream> 2 using namespace std; 3 int f[3000]; 4 int main() 5 { 6 unsigned long long a,b,n,i,j,t,ans,m; 7 cin>>t; 8 for(j=0;j<t;j++){ 9 cin>>a>>b>>n; 10 if(n==1||a==0) cout<<"0"<<endl; 11 else if(a==1||b==0) cout<<"1"<<endl; 12 else { 13 f[0]=0; 14 f[1]=1; 15 i=2; 16 for(i=2;i<3000;i++){ 17 f[i]=(f[i-1]+f[i-2])%n; 18 if(f[i-1]==1&&f[i]==0) 19 break; 20 } 21 m=i; 22 ans=1; 23 a%=m; 24 while(b>0){ 25 if(b%2==1) ans=(ans*a)%m; 26 a=(a*a)%m; 27 b>>=1; 28 } 29 cout<<f[ans]<<endl; 30 } 31 } 32 return 0; 33 34 } 35