R=range J=lambda:map(int,input().split()) for t in R(sum(J())): A,B,C=J() r,c,D=[0,1],0,1 for p in R(1,136): D*=A while D%C<1:c+=1;D//=C if r[0]*p