//#define _GLIBCXX_DEBUG #include "bits/stdc++.h" using namespace std; //------------------------------- Libraries --------------------------------// //------------------------------- Type Names -------------------------------// using i64 = int_fast64_t; using seika = string; //akari : 1D, yukari : 2D, maki : 3D vector template using akari = vector; template using yukari = akari>; template using maki = akari>; //akane : ascending order, aoi : decending order template using akane = priority_queue, greater>; template using aoi = priority_queue; //------------------------------- Dubug Functions ---------------------------// inline void print() { cout << endl; } template void print(const First &first, const Rest &... rest) { cout << first << ' '; print(rest...); } //------------------------------- Solver ------------------------------------// void solve() { int q; cin >> q; while (q--) { i64 a, b, c; cin >> a >> b >> c; i64 ans = (a + c - 2) / (c - 1), t = 0; if (c == 1) { cout << -1 << endl; continue; } while (1) { if (a % c != 0) { t++; } a = (a - a % c) / c; if (a == 0) { break; } t++; for (i64 x = 1, y = 1; x <= a; x *= c, y++) { i64 d = a - x; ans = min(ans, t + (d + c - 2) / (c - 1) + y); } ans = min(ans, t + (a + c - 2) / (c - 1)); } ans = min(ans, t); ans *= b; cout << ans << endl; } } int main() { solve(); return 0; }