import math mod=998244353 def solve(): a,b,c=map(int,input().split()) g=math.gcd(a,c**140) a=g if (a**140)%c!=0: print(0) return a_tmp=1 ans=b*200 for i in range(1,140): if c==1:break a_tmp*=a while a_tmp%c==0: a_tmp//=c c_tmp=c g=-1 while g!=1: g=math.gcd(a_tmp,c_tmp) c_tmp//=g if c_tmp>1: c_tmp=math.floor(math.pow(c_tmp,1/i)+0.0001) c_cnt=0 while c%c_tmp==0: c//=c_tmp c_cnt+=1 a_cnt=0 a_tmp_tmp=a while a_tmp_tmp%c_tmp==0: a_tmp_tmp//=c_tmp a_cnt+=1 #print(i,c_tmp) ans=min(ans,a_cnt*b//c_cnt) print(ans%mod) t=int(input()) for _ in range(t): solve()