#include "bits/stdc++.h" #define REP(i, n) for(int i = 0; i < int(n); i++) #define FOR(i,n,m) for(int i = int(n); i < int(m); i++) using namespace std; typedef long long ll; const int MOD = 1e9 + 7; const int INF = 1e9 + 6; const ll LLINF = 1e18 + 1; ll a, b, c; ll solve(ll n = a) { if (n == 0) return 0; ll res = (n + c - 2) / (c - 1); res = min(res, solve(n / c) + 1 + (n % c == 0 ? 0 : 1)); return res; } int main() { int q; cin >> q; REP(i, q) { cin >> a >> b >> c; if (c == 1) puts("-1"); else { cout << solve() * b << endl; } } return 0; }