#include using namespace std; #define REP(i,n) for(int i=0; i<(int)(n); i++) #define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++) #define ALL(x) (x).begin(), (x).end() const double PI = acos(-1); long long solve() { long long a, b, c; cin >> a >> b >> c; if (c == 1) return -1; vector ret; long long cur = 0; while (a > 1) { ret.push_back(cur + (a + c - 2) / (c - 1)); if (a % c == 0) { a /= c; } else { a -= a % c; } ++cur; } ret.push_back(++cur); long long r = *min_element(ret.begin(), ret.end()); return b * r; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int Q; cin >> Q; while (Q--) { cout << solve() << endl; } return 0; }