/*** author: yuji9511 ***/ #include using namespace std; typedef long long ll; typedef pair lpair; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) #define printa(x,n) for(ll i=0;i void print(H&& h, T&&... t){cout<(t)...);} ll calc(ll A, ll C){ ll amari = A % C; if(amari == A){ return 1; } ll res = min(1 + calc((A-amari)/C, C) + (amari == 0 ? 0 : 1), (A + C-2) / (C-1)); return res; } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll Q; cin >> Q; while(Q--){ ll A,B,C; cin >> A >> B >> C; if(C == 1){ print(-1); }else{ ll res = calc(A, C); print(res * B); } } }