#include using namespace std; // sum_{i=0}^{n-1} floor((a * i + b) / m) // O(log(n + m + a + b)) // __int128 can be used for T template T floor_sum(T n, T a, T b, T m) { if (n == 0) return 0; T res = 0; if (a >= m) { res += n * (n - 1) * (a / m) / 2; a %= m; } if (b >= m) { res += n * (b / m); b %= m; } if (a == 0) return res; T ymax = (a * n + b) / m, xmax = ymax * m - b; if (ymax == 0) return res; res += (n - (xmax + a - 1) / a) * ymax; res += floor_sum(ymax, m, (a - xmax % a) % a, a); return res; } using pint = pair; using pll = pair; 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; } #define REP(i, n) for (long long i = 0; i < (long long)(n); ++i) #define REP2(i, a, b) for (long long i = a; i < (long long)(b); ++i) #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template ostream& operator << (ostream &s, pair P) { return s << '<' << P.first << ", " << P.second << '>'; } template ostream& operator << (ostream &s, vector P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, deque P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, vector > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template ostream& operator << (ostream &s, set P) { for(auto it : P) { s << "<" << it << "> "; } return s; } template ostream& operator << (ostream &s, map P) { for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s; } // calc #n that can be expressed n = Px + Qy (P, Q is coprime) // 0 <= n <= M long long calc_num(__int128 P, __int128 Q, __int128 M) { __int128 mp = M / P; __int128 N = min(mp + 1, Q); __int128 a = P, b = M + Q - a * (N - 1); return floor_sum(N, a, b, Q) - 1; } int main() { /* COUT(calc_num(3, 4, 3)); COUT(calc_num(3, 4, 4)); COUT(calc_num(3, 4, 5)); */ int CASE; cin >> CASE; while (CASE--) { long long P, Q, K; cin >> P >> Q >> K; long long G = gcd(P, Q); P /= G, Q /= G; long long low = -1, high = 1LL<<50; while (high - low > 1) { long long M = (low + high) / 2; if (calc_num(P, Q, M) >= K) high = M; else low = M; } cout << high * G << endl; } }