#include using namespace std; using lint = long long; template using V = vector; template using VV = V< V >; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int q; cin >> q; while (q--) { int a, b, c; cin >> a >> b >> c; if (c == 1) { cout << -1 << '\n'; continue; } auto fn = [&](auto& F, int x) -> int { if (!x) return 0; if (x % c) { return min(F(F, x / c * c) + 1, (x + c - 2) / (c - 1)); } else { return min(F(F, x / c) + 1, (x + c - 2) / (c - 1)); } }; cout << (lint)b * fn(fn, a) << '\n'; } }