#include<cstdio>
typedef long long i64;
i64 gcd(i64 a,i64 b){
for(i64 c;b;c=a,a=b,b=c%b);
return a;
}
i64 phi(i64 x){
i64 y=x;
for(int i=2;i64(i)*i<=x;++i)if(x%i==0){
y=y/i*(i-1);
do x/=i;while(x%i==0);
}
if(x>1)y=y/x*(x-1);
return y;
}
i64 mul(i64 a,i64 b,i64 p){
i64 c=0;
for(;b;b>>=1,a=(a<<1)%p)if(b&1)c=(c+a)%p;
return c;
}
i64 pw(i64 a,i64 n,i64 p){
i64 v=1;
for(;n;n>>=1,a=mul(a,a,p))if(n&1)v=mul(v,a,p);
return v;
}
i64 cal(i64 A,i64 B){
i64 x=phi(B),y=x;
for(int i=2;i64(i)*i<=x;++i)if(x%i==0){
while(y%i==0&&pw(A,y/i,B)==1)y/=i;
do x/=i;while(x%i==0);
}
if(x>1&&pw(A,y/x,B)==1)y/=x;
return y;
}
int main(){
int T;
for(scanf("%d",&T);T;--T){
i64 a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
i64 g=gcd(a,b);
a/=g,b/=g;
int a1=0;
while(1){
i64 x=gcd(b,c);
if(x==1)break;
b/=x;
++a1;
}
printf("%d %lld\n",a1,b==1?0ll:cal(c,b));
}
return 0;
}