#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 1012345678;
int main() {
	int Q;
	cin >> Q;
	while (Q--) {
		int A, B, C;
		cin >> A >> B >> C;
		int ops = inf;
		vector<int> seq = { A };
		do {
			int x = seq.back();
			seq.back() %= C;
			seq.push_back(x / C);
			int cost = int(seq.size()) - 1;
			for (int i : seq) {
				cost += (i + C - 2) / (C - 1);
			}
			ops = min(ops, cost);
		} while (C != 1 && seq.back() >= C);
		cout << 1LL * ops * B << endl;
	}
	return 0;
}