#include using lint = long long; void solve() { lint a, b, c; std::cin >> a >> b >> c; if (c == 1) { std::cout << -1 << std::endl; return; } lint ans = a, cost = 0; while (a > 0) { ans = std::min(ans, cost + (a + c - 2) / (c - 1)); if (a % c != 0) ++cost; a -= a % c; ans = std::min(ans, cost + (a + c - 2) / (c - 1)); ++cost; a /= c; } std::cout << ans * b << std::endl; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int q; std::cin >> q; while (q--) solve(); return 0; }