def floor_sum(n,m,a,b): ans=0 while True: if a>=m: ans+=(n-1)*n*(a//m)//2 a%=m if b>=m: ans+=n*(b//m) b%=m y_max=(a*n+b)//m x_max=(y_max*m-b) if y_max==0: return ans ans+=(n-(x_max+a-1)//a)*y_max n,m,a,b=y_max,a,m,(a-x_max%a)%a def calc(p,q,m): n=min(q,m//p+1) return floor_sum(n,q,p,q+m-p*(n-1)) import math def solve(): p,q,k=map(int,input().split()) g=math.gcd(p,q) p//=g q//=g ng,ok=0,2*10**9+1 while ok-ng>1: mid=(ok+ng)//2 if calc(p,q,mid)>k: ok=mid else: ng=mid print(ok*g) for _ in range(int(input())): solve()