#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair P; typedef pair, int> PP; const ll INF = 1e15; const int MAX = 510000; const ll MOD = 1000000007; int main(void) { int Q; cin >> Q; for (int i = 0; i < Q; i++) { ll A, B, C; cin >> A >> B >> C; ll ans = 0; while (A != 0) { if (C <= A && A <= 2 * C - 2) { ans += 2 * B; break; } else if (A%C != 0) { A -= A % C; ans += B; } else { A /= C; ans += B; } } cout << ans << endl; } }