Problem:
Score : 400 points

Problem Statement

You start with the number 0 and you want to reach the number N.

You can change the number, paying a certain amount of coins, with the following operations:

Multiply the number by 2, paying A coins.

Multiply the number by 3, paying B coins.

Multiply the number by 5, paying C coins.

Increase or decrease the number by 1, paying D coins.

You can perform these operations in arbitrary order and an arbitrary number of times.

What is the minimum number of coins you need to reach N?

You have to solve T testcases.

Constraints

1 \le T \le 10

1 \le N \le 10^{18}

1 \le A, B, C, D \le 10^9

All numbers N, A, B, C, D are integers.

Input

The input is given from Standard Input. The first line of the input is

T

Then, T lines follow describing the T testcases.
Each of the T lines has the format

N A B C D

Output

For each testcase, print the answer on Standard Output followed by a newline.

Sample Input 1

5
11 1 2 4 8
11 1 2 2 8
32 10 8 5 4
29384293847243 454353412 332423423 934923490 1
900000000000000000 332423423 454353412 934923490 987654321

Sample Output 1

20
19
26
3821859835
23441258666

For the first testcase, a sequence of moves that achieves the minimum cost of 20 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 1 to multiply by 2 (x = 4).

Pay 2 to multiply by 3 (x = 12).

Pay 8 to decrease by 1 (x = 11).

For the second testcase, a sequence of moves that achieves the minimum cost of 19 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 2 to multiply by 5 (x = 10).

Pay 8 to increase by 1 (x = 11).

Solution in Ruby (2.7.1) (judge verdict: Wrong Answer)

DEBUG = ARGV[0] == '-d'

def log(&msg)
  $stderr.puts "# #{msg.call}" if DEBUG
end

class Resolver

  def self.init(a, b, c, d)
    log { "A = #{a}, B = #{b}, C = #{c}, D = #{d}" }
    @@resolved = { 0 => 0, 1 => d }
    @@label_map = {
      "A" => a,
      "B" => b,
      "C" => c,
      "D" => d,
    }
  end

  def self.resolved
    @@resolved
  end

  def self.label_map
    @@label_map
  end

  class Calc

    MUL = {
      "A" => 2,
      "B" => 3,
      "C" => 5,
    }

    attr_reader :next_n

    def initialize(next_n, cur_pay, abc, d_cnt)
      @next_n = next_n
      @cur_pay = cur_pay
      total_pay
      if DEBUG
        d = Resolver.label_map["D"]
        if abc
          @label = "#{abc}#{MUL[abc]}(#{Resolver.label_map[abc]})"
          if d_cnt > 0
            @label += " + #{d_cnt}D(#{d_cnt}*#{d})"
          end
        elsif d_cnt
          @label = "#{d_cnt}D(#{d_cnt}*#{d})"
        end
      end
    end

    def total_pay
      if @total_pay.nil? && @next_pay = Resolver.resolved[@next_n]
        @total_pay = @next_pay + @cur_pay
      end
      @total_pay
    end

    def to_s
      "# next_n = #{@next_n}, total pay = #{@total_pay}, cur pay: #{@label} = #{@cur_pay}"
    end
  end

  attr_reader :n, :resolved

  def initialize(n)
    @n = n
    @resolved = nil
    @stack = []
  end

  def register(next_n, cur_pay, abc, d_cnt)
    return if abc && next_n == 0
    @stack.push(Calc.new(next_n, cur_pay, abc, d_cnt))
  end

  def resolve
    if calc = @stack.find {|c| c.total_pay.nil? }
      calc.next_n
    else
      calc = @stack.min {|a, b| a.total_pay <=> b.total_pay }
      @resolved = @@resolved[@n] = calc.total_pay
      if DEBUG
        log { "[n = #{@n}, pay = #{@resolved}]" }
        @stack.each do |c|
          $stderr.puts c
        end
      end
      nil
    end
  end
  
end

class Entry
  attr_reader :n, :a, :b, :c, :d, :cache

  def initialize(line)
    @n, @a, @b, @c, @d = line.split.map {|i| i.to_i }
  end

  def pay
    Resolver.init(@a, @b, @c, @d)
    resolvers = []
    r = Resolver.new(@n)
    loop do
      n = r.n
      nn2, nm2 = n.divmod(2)
      p2c = @a + nm2 * @d
      r.register(nn2, p2c, "A", nm2)
      nn3, nm3 = n.divmod(3)
      p3c1 = @b + nm3 * @d
      p3c2 = @b + (3 - nm3) * @d
      r.register(nn3, p3c1, "B", nm3)
      r.register(nn3 + 1, p3c2, "B", 3 - nm3)
      nn5, nm5 = n.divmod(5)
      p5c1 = @c + nm5 * @d
      p5c2 = @c + (5 - nm5) * @d
      r.register(nn5, p5c1, "C", nm5)
      r.register(nn5 + 1, p5c2, "C", 5 - nm5)
      p1 = n * @d
      r.register(0, p1, nil, n)
      if next_n = r.resolve
        resolvers.push(r)
        r = Resolver.new(next_n)
      else
        loop do
          if resolvers.empty?
            return r.resolved
          end
          if next_n = resolvers[-1].resolve
            break
          end
          r = resolvers.pop
        end
        r = Resolver.new(next_n)
      end
    end
  end
end

T = $stdin.gets.to_i
0.upto(T - 1) do |i|
  e = Entry.new($stdin.gets)
  p = e.pay
  puts p
  log { p }
  log { "-" * 70 }
end