// Problem: D2. Removal of a Sequence (Hard Version) // Contest: Codeforces - Educational Codeforces Round 184 (Rated for Div. 2) // URL: https://codeforces.com/contest/2169/problem/D2 // Memory Limit: 512 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #include #include using namespace std; #ifndef __DEBUG__ #define dbg(...) 42 #endif template using mpq = priority_queue, greater>; namespace rngs = std::ranges; using ll = long long; using a2l = array; using vl = vector; void solve() { ll x, y, k, n = 1e12; cin >> x >> y >> k; if (y == 1) { assert(x != 0); cout << -1 << '\n'; return; } ll t = y - 1; while (x > 0) { ll d = k / t; if (d) { ll a = (t - k % t) / d + 1; // a = min(a, (n - k) / d); a = min(a, x); dbg(a, x, d, k); k += a * d; x -= a; } else { break; } } if (k <= n) { cout << k << '\n'; } else { cout << -1 << '\n'; } } int main(int argc, char **argv) { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); ll t = 1; cin >> t; while (t--) solve(); return 0; };