Q = int(input()) dep = [] for _ in range(Q): n,k = map(int,input().split()) depth = 0 n = n - 1 # 最初のノードを摘出 if k == 1: dep.append(n) else: while n > 0: depth += 1 n -= k**(depth) dep.append(depth) for i in dep: print(i)