#include #include #include #include #include #include using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define dump(x) cout << #x << " = " << (x) << endl; template using vi = vector; template using vii = vector>; template using viii = vector>; template T floor_sum(T n, T m, T a, T b) { //sigma(i=0~n-1){[(ai+b)/m]} T res = 0; res += (a / m) * n * (n - 1) / 2; res += (b / m) * n; a = a % m; b = b % m; T ymax = a * n + b; if (ymax < m) return res; res += floor_sum(ymax / m, a, m, ymax % m); return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } int main() { int t; cin >> t; //ACL /* while (t--) { int n, m, a, b; cin >> n >> m >> a >> b; ll ans = floor_sum(n, m, a, b); cout << ans << endl; }*/ //yukicoder 2066 while (t--) { ll p, q, k; cin >> p >> q >> k; ll g = gcd(p, q); p /= g; q /= g; ll ok = 0, ng = min(p, q) * k + 1; while (ng - ok > 1) { ll mid = (ok + ng) / 2; ll cnt = min(mid / q, p - 1); ll n = mid / q + 1; ll m = p; ll a = q; ll b = mid % q; cnt += floor_sum(n, m, a, b); if (n >= p) cnt -= floor_sum(n - p, m, a, b); if (cnt < k) ok = mid; else ng = mid; } cout << ng * g << endl; } return 0; } // https://atcoder.jp/contests/practice2/tasks/practice2_c