#include using namespace std; long long f(long long n, long long a, long long b) { return a * n + b; } long long floorSum(long long n, long long m, long long a, long long b) { if (n == 0) return 0; long long toAdd = (a / m) * n * (n - 1) / 2 + (b / m) * n; a %= m; b %= m; return toAdd + f(n - 1, a, b) / m + floorSum(f(n - 1, a, b) / m, a, m, f(n - 1, a, b) % m); } int P, Q, K; void read() { cin >> P >> Q >> K; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } void work() { int div = gcd(P, Q); P /= div; Q /= div; long long LT = 0, GE = 1LL * P * Q - 1; while (LT + 1 < GE) { long long mid = (LT + GE) / 2; if (floorSum(mid / P + 1, Q, P, mid - (mid / P) * P) + mid / P < K) { LT = mid; } else { GE = mid; } } // for (int i = 1; i <= 20; ++i) { // cout << i << ' ' << floorSum(i / 7 + 1, 3, 7, i - (i / 7) * 7) + i / 7 << endl; // } // cout << "fs: " << floorSum(GE / P + 1, Q, P, GE - (GE / P) * P) + GE / P << " K: " << K << endl; if (GE == 1LL * P * Q - 1) { long long toAdd = K - (floorSum(GE / P + 1, Q, P, GE - (GE / P) * P) + GE / P); cout << (GE + toAdd) * div << endl; } else { cout << GE * div << endl; } } int main() { int T; cin >> T; for (int i = 0; i < T; ++i) { read(); work(); } return 0; }