#define _USE_MATH_DEFINES #include using namespace std; //template #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(a);i>(b);i--) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; typedef pair P; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } templatevoid Fill(A(&array)[N],const T &val){fill((T*)array, (T*)(array+N), val);} const int inf = 0x3fffffff; const ll INF = 0x3fffffffffffffff; //template end void solve(){ int a,b,c; scanf("%d%d%d",&a,&b,&c); if(c==1){ printf("-1\n"); return; } ll ans=inf,cnt=0; while(a){ chmin(ans,cnt+(a+c-2)/(c-1)); if(a%c)a-=a%c; else a/=c; cnt++; } printf("%lld\n",ans*b); } int main(){ int t; scanf("%d",&t); rep(rot,0,t)solve(); return 0; }