#include #include #include using namespace std; 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 atcoder::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=1e18; while(r-l>1) { long long mid=(l+r)/2; if(count(mid,P,Q)<=K)l=mid; else r=mid; } cout<