#include // ver2.2.4 #define int long long #define endl "\n" #define ALL(v) (v).begin(),(v).end() #define Vi vector #define VVi vector #define Vs vector #define Pii pair #define VPii vector #define Tiii tuple #define PQi priority_queue #define PQir priority_queue,greater> #define pb push_back #define mp make_pair #define mt make_tuple #define itos to_string #define stoi stoll #define FI first #define SE second #define cYES cout<<"YES"<()) #define rep(i,a,b) for(int i=a;i=b;i--) #define leng(n) (int)(log10(n)+1) #define dem(a,b) ((a+b-1)/(b)) #define Vin(a) rep(iI,0,a.size())cin>>a[iI] #define Vout(a) rep(lZ,0,a.size()-1)cout<> q; int a,b,c,ans; rep(QA,0,q){ cin >> a >> b >> c; ans = 0; while(a >= 2*c){ if(a%c==0){ ans++; a = a/c; } else { a -= a%c; ans++; } } ans += dem(a,c-1); ans *= b; cout << ans << endl; } return 0; }