#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef __int128 ll; /** * https://kenkoooo.hatenablog.com/entry/2016/11/30/163533 * より。Project Eularであまりにも使うので。 */ __int128 parse(string &s) { __int128 ret = 0; for (int i = 0; i < s.length(); i++) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } istream & operator >> (istream &in, __int128_t &m){ string s; cin >> s; m = parse(s); return in; } ostream &operator<<(std::ostream &dest, __int128_t value) { ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(ios_base::badbit); } } return dest; } template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } /** * ax + by = gcd(a, b)を満たす(x, y)をつめて、gcd(a, b)を返します。 */ template T extGCD(T a, T b, T &x, T &y) { if (b == 0) { x = 1; y = 0; return a; } T d = extGCD(b, a%b, y, x); y -= a/b * x; return d; } // calc ceil(a/b) template T ceil_div(T a, T b){ return (a+b-1)/b; } namespace internal{ ll safe_mod(ll x, ll m) { x %= m; if (x < 0) x += m; return x; } ll floor_sum_( ll n, ll m, ll a, ll b) { ll ans = 0; while (true) { if (a >= m) { ans += n * (n - 1) / 2 * (a / m); a %= m; } if (b >= m) { ans += n * (b / m); b %= m; } ll y_max = a * n + b; if (y_max < m) break; // y_max < m * (n + 1) // floor(y_max / m) <= n n = ( ll)(y_max / m); b = ( ll)(y_max % m); std::swap(m, a); } return ans; } } ll floor_sum(ll n, ll m, ll a, ll b) { ll ans = 0; if (a < 0) { ll a2 = internal::safe_mod(a, m); ans -= n * (n - 1) / 2 * ((a2 - a) / m); a = a2; } if (b < 0) { ll b2 = internal::safe_mod(b, m); ans -= n * ((b2 - b) / m); b = b2; } return ans + internal::floor_sum_(n, m, a, b); } void solve(){ long p, q, k; cin >> p >> q >> k; if(p == q){ cout << p*k << endl; return; } if(p > q) swap(p, q); long a, b; extGCD(p, q, a, b); if(a < 0){ ll x = ceil_div(-a, q); a += x*q; b -= x*p; } if(a > q){ ll x = a/q; a -= x*q; b += x*p; } b *= -1; ll g = gcd(p, q); p /= g; q /= g; function count = [&](ll x){ if(x >= p*q){ return count(p*q-1)+x-p*q+1; } ll upper = atcoder::floor_sum(x+1, q, a, 0); ll lower = atcoder::floor_sum(x+1, p, b, 0); ll on_line = x/p; return upper-lower+on_line; }; ll l = 0, r = p*q+k; while(r-l > 1){ ll x = (l+r)/2; if(count(x) >= k) r = x; else l = x; } cout << r*g << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int t; cin >> t; while(t--) solve(); }