#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef pair P; typedef tuple T; long long int INF = 1e18; long long int MOD = 1e9 + 7; int main(){ int Q; cin >> Q; for(int loop = 0; loop < Q; loop++){ long long int A, B, C; cin >> A >> B >> C; if(C == 1){ cout << -1 << endl; continue; } long long int ans = 0; while(A > 2 * (C - 1)){ if(A % C == 0){ A /= C; }else{ A = A / C * C; } ans += B; } if(A > C - 1){ ans += B * 2; }else{ ans += B; } cout << ans << endl; } return 0; }