#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace atcoder; #define rep(i,n) for (int i=0;i-1;i--) #define pb push_back #define all(x) (x).begin(), (x).end() template using vec = vector; template using vvec = vec>; template using vvvec = vec>; using ll = long long; using pii = pair; using pll = pair; template bool chmin(T &a, T b){ if (a>b){ a = b; return true; } return false; } template bool chmax(T &a, T b){ if (a T sum(vec x){ T res=0; for (auto e:x){ res += e; } return res; } template ostream& operator<<(ostream& os, const vec& A){ os << "["; rep(i,A.size()){ os << A[i]; if (i!=A.size()-1){ os << ' '; } } os<<"]"; return os; } template ostream& operator<<(ostream& os, const set& A){ os << "{"; for (auto e:A){ os << e; auto it = A.find(e); it++; if (it!=A.end()){ os << " "; } } os << "}"; return os; } template ostream& operator<<(ostream& os, const pair& A){ os << "(" << A.first <<", " << A.second << ")"; return os; } template ostream& operator<<(ostream& os, const deque& A){ os << "deque({"; rep(i,A.size()){ os << A[i]; if (i!=A.size()-1){ os << ' '; } } os<<"})"; return os; } ll solve(){ ll P,Q,K; cin>>P>>Q>>K; ll g = gcd(P,Q); ll p = P/g, q = Q/g; auto [r0,m0] = crt({1,0},{p,q}); ll x0 = (1-r0)/p, y0 = r0/q; if (0 <= x0){ return K * g; } x0 *= -1; ll PQ_less = 0; auto cnt = [&](ll m){ if (m < p*q){ ll tmp = 0; tmp += floor_sum(p,p,y0,y0) * (m/p) + (m/p) * (m/p-1)/2 * y0 * p + floor_sum(m%p,p,y0,y0) + (m/p) * y0 * (m%p); tmp -= floor_sum(q,q,x0,x0-1) * (m/q) + (m/q) * (m/q-1)/2 * x0 * q + floor_sum(m%q,q,x0,x0-1) + (m/q) * x0 * (m%q); return tmp; } else{ return PQ_less + m-p*q+1; } }; PQ_less = cnt(p*q-1); ll ok = 1e18; ll ng = 0; while (ok-ng>1){ ll mid = (ok+ng)/2; if (cnt(mid) >= K){ ok = mid; } else{ ng = mid; } } return ok * g; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int T; cin>>T; while (T--){ cout << solve() << endl; } }