from math import gcd def solve(p, q, k): x = p * q y = (p - 1) * (q - 1) if y <= k: ans = x - 1 + k - y return ans if p > q: p, q = q, p dp = [0] * p tot = 0 for _ in range(p - 1): tot += q dp[tot % p] = tot dp[0] = p def cnt(x): t = 0 for a in dp: t += max(0, (x - a + p) // p) return t l = 0 r = p * q while r - l > 1: mid = (l + r) // 2 if cnt(mid) >= k: r = mid else: l = mid return r for _ in range(int(input())): p, q, k = map(int, input().split()) g = gcd(p, q) p //= g q //= g print(g * solve(p, q, k))