import math import sys from collections import Counter q = int(input()) nkq = [None] * q for i in range(q): nkq[i] = list(map(int, input().split(' '))) for nk in nkq: if nk[1] == 1: print(nk[0] -1) else: n = math.floor(math.log(nk[0] * (nk[1] - 1) + 1, nk[1])) print(n)