#include #include using namespace std; #define DEBUG(x) cerr<<#x<<": "< #define vl vector #define vii vector< vector > #define vll vector< vector > #define vs vector #define pii pair #define pis pair #define psi pair #define pll pair template pair operator+(const pair &s, const pair &t) { return pair(s.first + t.first, s.second + t.second); } template pair operator-(const pair &s, const pair &t) { return pair(s.first - t.first, s.second - t.second); } template ostream& operator<<(ostream& os, pair p) { os << "(" << p.first << ", " << p.second << ")"; return os; } #define X first #define Y second #define rep(i,n) for(int i=0;i<(n);i++) #define rep1(i,n) for(int i=1;i<=(n);i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define rrep1(i,n) for(int i=(n);i>0;i--) #define REP(i,a,b) for(int i=a;i bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; } #define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end()); const ll inf = 1000000001; const ll INF = (ll)1e18 + 1; const ll MOD = 1000000007; //const ll MOD = 998244353; const double pi = 3.14159265358979323846; #define Sp(p) cout< &mp, int a, int cnt) { if (a < 0) return INF; if (cnt == 40) return inf; if (a == 0) return 0; if (mp[a] != 0) return mp[a]; int mo = a % c; int mi; if (mo != 0) { mi = solve(mp, a - mo, cnt + 1); } else { mi = solve(mp, a / c, cnt + 1); } if (mo != c - 1) { chmin(mi, solve(mp, a - (c - 1), cnt + 1)); } mp[a] = mi + 1; return mi + 1; } int solve2(int a, int c) { int ans = 0; while (a > 0) { ans++; if (a % c == 0) { a /= c; } else { a -= a % c; } } return ans; } signed main() { fio(); int q; cin >> q; while (q--) { cin >> a >> b >> c; //* map mp; ll ans = solve(mp, a, 0); /* for (auto itr = mp.begin(); itr != mp.end(); itr++) { DEBUG(*itr); } //*/ ans = ans * b; //*/ //ll ans = solve2(a, c) * b; cout << ans << endl; } }