#include using namespace std; typedef long long ll; #define all(x) (x).begin(),(x).end() const int mod=1000000007,MAX=100003,INF=1<<30; int main(){ int Q;cin>>Q; while(Q){ ll A,B,C;cin>>A>>B>>C; if(C==1){ cout<<-1<=A){ A-=(C-1); A=max(0LL,A); cnt++; }else if(A%C){ A-=A%C; cnt++; }else{ A/=C; cnt++; } //cout<