q = int(input()) for _ in range(q): a, b, c = map(int, input().split()) if c == 1: print(-1) continue def f(x): if x < c: return 1 elif x - c + 1 < c: return f(x - c + 1) + 1 elif x % c == 0: return f(x // c) + 1 else: return f(x - x % c) + 1 print(f(a) * b)