#define _USE_MATH_DEFINES #include using namespace std; long long solve () { long long a, b, c; cin >> a >> b >> c; if (c == 1) return -1; for (int i = 1; ; i++) { if ((c - 1) * 2 >= a) a -= c - 1; else if (a % c == 0) a /= c; else a -= a % c; if (a <= 0) return b * i; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); int q; cin >> q; while (q--) cout << solve() << '\n'; return 0; }