Q = int(input()) def S(r, n): if r == 1: return n+1 return (pow(r, n+1)-1)//(r-1) for _ in range(Q): N,K = map(int, input().split()) l = 0 r = N+1 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)