#include using namespace std; #define REP(i,x,y) for(ll i=x; i<=y; i++) #define BIT(t) ((long long 1) << t) #define PER(i,y,x) for(ll i=y; i>=x; i--) #define SIZE(v) ll(v.size()) #define vll vector #define vvll vector> #define pll pair #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); typedef long long ll; int main(){ ll q; cin >> q; vll ans; REP(iii,1,q){ ll a,b,c; cin >> a >> b >> c; if(c == 1){ ans.push_back(0); continue; } vll v; ll cnt = 0; while(a > 0){ if(a < c){ cnt++; a = 0; }else if(a <= 2*(c-1)){ cnt += 2; a = 0; }else if(a % c == 0){ cnt++; a /= c; }else{ cnt++; a /= c; a *= c; } } ans.push_back(cnt * b); } for(auto x:ans){ cout << x << endl; } }