/* #include namespace my{entry void main(){ multi_test_cases LL(a,b,K);K++; ll g=gcd(a,b); a/=g,b/=g; ll N=binary_search_first_true(1,oo,[&](ll N){ ll L=min(b,N/a+1); return floor_sum(L,b,-a,N)+L>=K; }); pp(N*g); }}*/ #include #if __has_include() #endif using namespace std; #define RD(T,...) T __VA_ARGS__;lin(__VA_ARGS__) #define LL(...) RD(ll,__VA_ARGS__) #define FO(n) for(ll IJK=n;IJK-->0;) #define defpp templatevoid pp(const auto&...a){[[maybe_unused]]const char*c="";((o<(a...);} #define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{ #define multi_test_cases LL(T);FO(T)main2();}void main2(){ namespace my{ void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<>...>>a);} constexpr auto abs(auto x){return x<0?-x:x;} constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;} templateconstexpr ll maxsize10(){return size10(numeric_limits::max());} auto min(auto...a){return min(initializer_list>{a...});} templatecommon_type_tgcd(T a,U b){return b?gcd(b,a%b):abs(a);} ll binary_search_first_true(ll l,ll r,auto&&f){while(l+1struct infinity{ templatestatic constexpr T ones(size_t n){return n?ones(n-1)*10+1:0;} templateconstexpr operator T()const{return ones(maxsize10())*(1-is_negative*2);} }; constexpr infinity oo; } namespace my{ templateT floor_sum(lll n,lll m,lll a,lll b){ if(n<=0)return 0; if(a<0)return-floor_sum(n,m,-a,-b+m-1); if(b<0)return floor_sum(n,m,a,b*(1-m))+(T)b*n; if(a>=m||b>=m)return floor_sum(n,m,a%m,b%m)+(T)(n-1)*n/2*(a/m)+(T)n*(b/m); if(a==0)return 0; return floor_sum((a*n+b)/m,a,m,(a*n+b)%m); } } namespace my{entry void main(){ multi_test_cases LL(a,b,K);K++; ll g=gcd(a,b); a/=g,b/=g; ll N=binary_search_first_true(1,oo,[&](ll N){ ll L=min(b,N/a+1); return floor_sum(L,b,-a,N)+L>=K; }); pp(N*g); }}