#include using namespace std; using ll = long long; constexpr ll INF = 1LL << 60; ll q, a, b, c; int main() { cin >> q; while(q--) { cin >> a >> b >> c; if(c == 1) { cout << -1 << '\n'; continue; } ll ans = INF, cnt = 0; while(a) { ans = min(ans, cnt + (a + c - 2) / (c - 1)); if(a % c) a -= a % c; else a /= c; ++cnt; } cout << ans * b << '\n'; } return 0; }