n = int(input()) for i in range(n): a,b,c = map(int,input().split()) if c == 1: print(a * b) continue x = 0 y = 10 ** 100 while a > 0: y = min(y, x + b * ((a + c - 2) // (c - 1))) if a % c == 0: a //= c else: a -= a % c x += b print(min(x, y))