def solve(A, B, C): if C == 1: return -1 res = 0 while A > 2 * C - 2: A, mod = divmod(A, C) res += B if mod: res += B res += B if A < C else 2 * B return res Q = int(input()) for _ in range(Q): print(solve(*map(int, input().split())))