q = int(input()) abc = [[int(i) for i in input().split()] for j in range(q)] ans = 0 for i in range(q): a,b,c = abc[i] num = a ans = 0 if c == 1: print(-1) continue while num > 0: if num <= 1*(c-1): ans += 1 break elif num <= 2*(c-1): ans += 2 break if num%c != 0: ans += 1 num = (num//c)*c ans += 1 #print(num) num = num//c #print(num,ans) #print((987654321//57)*57) #print(987654321//57) print(ans*b)