import sys

def main():
    input = sys.stdin.read().split()
    Q = int(input[0])
    idx = 1
    for _ in range(Q):
        N = int(input[idx])
        K = int(input[idx + 1])
        idx += 2
        
        if K == 1:
            print(N - 1)
            continue
        
        low = 0
        high = 60
        ans = 0
        while low <= high:
            mid = (low + high) // 2
            total = 0
            current = 1
            possible = False
            for _ in range(mid + 1):
                total += current
                if total >= N:
                    possible = True
                    break
                current *= K
            if possible:
                ans = mid
                high = mid - 1
            else:
                low = mid + 1
        print(ans)

if __name__ == '__main__':
    main()