#include #define rep(i, n) for(int i = 0; i < (int)n; i++) using namespace std; using ll = long long; using P = pair; int main() { int q; cin >> q; rep(query, q) { ll a, b, c; cin >> a >> b >> c; if(c == 1) { cout << -1 << endl; continue; } ll ans = 0; while(a > 1) { if(a % c) ans++; a /= c; if(a) ans++; } cout << ans * b << endl; } return 0; }