mod = 998244353 def main(): import sys from math import gcd input = sys.stdin.readline # sum([(a * i + b) // m for i in range(n)]) def floor_sum(n, m, a, b): ret = 0 while True: ret += (a // m) * n * (n - 1) // 2 + (b // m) * n a %= m b %= m y = (a * n + b) // m x = b - y * m if y == 0: return ret ret += (x // a + n) * y n, m, a, b = y, a, m, x % a for _ in range(int(input())): p, q, k = map(int, input().split()) g = gcd(p, q) p //= g q //= g if p > q: p, q = q, p k0 = floor_sum(q, q, p, p) + q - 1 if k >= k0: ans = p * q + (k - k0) print(ans * g) else: ok = p * q ng = 0 mid = (ok + ng) // 2 while ok - ng > 1: x0 = mid // p tmp = floor_sum(x0+1, q, p, mid - p * x0) + x0 if tmp >= k: ok = mid else: ng = mid mid = (ok + ng) // 2 print(ok) if __name__ == '__main__': main()