#include "bits/stdc++.h" using namespace std; typedef long long int ll; typedef vector vl; typedef pair P; #define rep(i,n) for(ll i = 0; i < ll(n); i++) #define all(v) v.begin() , v.end() #define inputv(v,n) rep(i,n){\ ll x;\ cin>>x;\ v.push_back(x);\ } const ll INF = 99999999999999; const ll MOD = 1000000007; ll a, b, c, d, e, f, p, q, t, k, m, n, ans = 0; vl N, K; void input(void) { cin >> q; } void solve(void){ while (q--) { cin >> n >> k; if (k == 1) { cout << n - 1 << endl; } else { a = 1; ans = -1; while (n > 0) { n -= a; a = a * k; ans++; } cout << ans << endl; } } } int main() { cin.tie(0); ios::sync_with_stdio(false); input(); solve(); }