#include using namespace std; using LL = long long; using ULL = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int main() { int Q; cin >> Q; while (Q--) { LL N, K; cin >> N >> K; LL t = 1; LL ans = 0; if (K == 1) ans = N - 1; else { while (true) { N = max(0ll, N - t); if (N == 0) break; ans++; t *= K; } } cout << ans << endl; } return 0; }