crystal eval 'require "big" read_line.to_i.times do a, b, c = read_line.split.map(&.to_big_i) if c == 1 puts -1 next end dfs = Hash(BigInt, BigInt).new do |h, n| if n == 0 h[n] = 0.to_big_i elsif n % c == 0 h[n] = {h[n // c] + b, (n + c - 2) // (c - 1) * b}.min else h[n] = {h[n - n % c] + b, (n + c - 2) // (c - 1) * b}.min end end puts dfs[a] end '