#include using namespace std; using ll = long long; using Pll = pair; using Pii = pair; constexpr ll MOD = 1000000007; constexpr long double EPS = 1e-10; constexpr int dyx[4][2] = { { 0, 1}, {-1, 0}, {0,-1}, {1, 0} }; ll solve(ll n, ll k) { if(k == 1) return n-1; if(n == 1) return 0; ll ng = 0, ok = n; while(ok - ng > 1LL) { ll mid = (ok + ng) / 2; if((pow(k, mid) - 1) / (k-1) >= n) { ok = mid; } else { ng = mid; } } return ok-1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; ll n, k; while(Q--) { cin >> n >> k; cout << solve(n, k) << endl; } }