#include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define int long long #define double long double typedef vector VI; typedef pair pii; typedef vector VP; typedef vector VS; typedef priority_queue PQ; templatebool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define fore(i,a) for(auto &i:a) #define REP(i,n) for(int i=0;i, greater > q2; int solve(int A, int B, int C) { if (A == 0)return 0; int res = 0; int c = C - 1; if (A%C) { res += min(B + solve(A - A % C, B, C), ((A - 1) / c + 1) * B); } else { res += min(B + solve(A / C, B, C), ((A - 1) / c + 1) * B); } //cout << A << " " << res << endl; return res; } signed main() { cin.tie(0); ios::sync_with_stdio(false); int Q; cin >> Q; while (Q--) { int A, B, C; cin >> A >> B >> C; if (C == 1) { cout << -1 << endl; continue; } cout << solve(A, B, C) << '\n'; } return 0; }