#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--) { lint a, b, c; cin >> a >> b >> c; if (c == 1) { cout << -1 << '\n'; continue; } lint x = (a + c - 2) / (c - 1); lint t = 0; while (a) { if (a % c) { a = a / c * c; ++t; } else { a /= c; ++t; } } cout << min(t, x) * b << '\n'; } }