#include #include #include #include #include #include #include #include #include #include #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll MOD=1e9+7; const ll INF=1e18; void solve(){ ll A,B,C; cin>>A>>B>>C; if(C==1){ cout<<-1<0){ if(res==INF&&now<=2*(C-1)){ res=ans+2*B; } if(now%C==0) now/=C; else now-=now%C; ans+=B; } cout<>Q; rep(i,Q) solve(); return 0; }