#pragma GCC optimize ("Ofast") #include using namespace std; inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline void rd(long long &x){ int k; int m=0; x=0; for(;;){ k = getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline int rd_int(void){ int x; rd(x); return x; } inline void wt_L(char a){ putchar_unlocked(a); } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ putchar_unlocked('-'); } while(s--){ putchar_unlocked(f[s]+'0'); } } long long N; long long K; int main(){ int Lj4PdHRW; long long res; long long cur; int KL2GvlyY = rd_int(); for(Lj4PdHRW=(0);Lj4PdHRW<(KL2GvlyY);Lj4PdHRW++){ rd(N); rd(K); if(K==1){ wt_L(N-1); wt_L('\n'); continue; } res = 0; N -= (cur = 1); while(N > 0){ N -= (cur *= K); res++; } wt_L(res); wt_L('\n'); } return 0; } // cLay varsion 20191006-1 // --- original code --- // ll N, K; // { // ll res, cur; // REP(rd_int()){ // rd(N,K); // if(K==1) wt(N-1), continue; // res = 0; // N -= (cur = 1); // while(N > 0) N -= (cur *= K), res++; // wt(res); // } // }