#include using namespace std; using ll = long long; long long floor_sum(long long n, long long m, long long a, long long b) { long long ans = 0; if (a >= m) { ans += (n - 1) * n * (a / m) / 2; ans = min(ans, 1ll << 60); a %= m; } if (b >= m) { ans += n * (b / m); ans = min(ans, 1ll << 60); b %= m; } long long y_max = (a * n + b) / m, x_max = (y_max * m - b); if (y_max == 0) return ans; ans += (n - (x_max + a - 1) / a) * y_max; ans = min(ans, 1ll << 60); ans += floor_sum(y_max, a, m, (a - x_max % a) % a); ans = min(ans, 1ll << 60); return ans; } int main(){ int t; cin >> t; while(t--){ ll p, q, k, g; cin >> p >> q >> k; g = __gcd(p, q); p /= g, q /= g; if(p == 1 || q == 1){ cout << g * k << '\n'; continue; } auto f = [&](ll v){ ll r = min(q, v / p + 1); return floor_sum(r, q, p, v + q - p * (r - 1)); }; ll ng = 0, ok = 1ll << 30, mid; while(ng + 1 < ok){ mid = (ng + ok) / 2; if(f(mid) <= k)ng = mid; else ok = mid; } cout << g * ok << '\n'; } }