import math Q = int(input()) def S(r, n): return (pow(r, n+1)-1)//(r-1) for _ in range(Q): N,K = map(int, input().split()) if K == 1: print(N-1) continue l = 0 r = int(math.log(N, K)) + 3 while r-l > 1: m = (r+l)//2 if S(K, m) < N: l = m else: r = m if S(K, l) >= N: ans = l else: ans = r print(ans)