#include #include using namespace std; long long floor_sum(long long N,long long M,long long A,long long B) //Sum[floor((A*i+B)/M),{i,0,N-1}] { long long ans=0; if(B>=M) { ans+=N*(B/M); B%=M; } while(true) { ans+=N*(N-1)/2*(A/M); A%=M; long long Ym=(A*N+B)/M,Xm=Ym*M-B; if(Ym==0)return ans; long long TX=(Xm+A-1)/A; ans+=(N-TX)*Ym; N=Ym; B=TX*A-Xm; swap(A,M); } } int gcd(int a,int b) { while(b) { int t=a%b; a=b; b=t; } return a; } long long count(long long X,int P,int Q) //Sum[floor((X-Pi+Q)/Q),{i,0,Q-1}] { long long R=min((long long)Q,X/P+1); return floor_sum(R,Q,P,X+Q-P*(R-1)); } int main() { int T; cin>>T; for(;T--;) { int P,Q,K; cin>>P>>Q>>K; int g=gcd(P,Q); P/=g; Q/=g; long long l=0,r=(long long)P*Q+K; while(r-l>1) { long long mid=(l+r)/2; if(count(mid,P,Q)<=K)l=mid; else r=mid; } cout<