#include using namespace std; int main(void) { cin.tie(0); ios::sync_with_stdio(false); long long int a,b,c; int q; cin >> q; while(q--) { long long int res = 0; cin >> a >> b >> c; while(a > 0) { if(a >= c && a < 2*c) { res += b; a-=(c-1); } else { if(a%c!=0) { res += b; a -= (a%c); } else { a/=c; res+=b; } } } cout << res << '\n'; } return 0; }