#include using namespace std; int main(){ int Q; cin >> Q; for (int i = 0; i < Q; i++){ int A, B, C; cin >> A >> B >> C; if (C == 1){ cout << -1 << endl; } else { long long ans = 0; while (A > 0){ if (C >= 3 && C < A && A <= C * 2 - 2){ ans += B * 2; break; } if (A % C != 0){ ans += B; A -= A % C; } if (A == 0){ break; } ans += B; A /= C; } cout << ans << endl; } } }