import java.io.{BufferedOutputStream, BufferedReader, InputStreamReader, PrintWriter}

import scala.collection.mutable

object Main {
  def main(args: Array[String]): Unit = {
    val in = new BufferedReader(new InputStreamReader(System.in))
    val out = new PrintWriter(new BufferedOutputStream(System.out))
    val t = in.readLine().toInt

    case class Elem(value: Long, cost: Long)

    def minCost(n: Long, a: Int, b: Int, c: Int, d: Int): BigInt = {
      val memo = mutable.HashMap.empty[Long, BigInt]
      memo.update(0L, 0L)
      memo.update(1L, d)

      def inner(n: Long): BigInt = {
        memo.getOrElseUpdate(n,
          List(
            BigInt(d) * n,
            BigInt(d) * (n - n / 5 * 5) + c + inner(n / 5),
            BigInt(d) * ((n + 4) / 5 * 5 - n) + c + inner((n + 4) / 5),
            BigInt(d) * (n - n / 3 * 3) + b + inner(n / 3),
            BigInt(d) * ((n + 2) / 3 * 3 - n) + b + inner((n + 2) / 3),
            BigInt(d) * (n - n / 2 * 2) + a + inner(n / 2),
            BigInt(d) * ((n + 1) / 2 * 2 - n) + a + inner((n + 1) / 2)
          ).min
        )
      }

      inner(n)
    }

    for (_ <- 0 until t) {
      val (n, a, b, c, d) = in.readLine().split("\\s+") match {
        case Array(n, a, b, c, d) => (n.toLong, a.toInt, b.toInt, c.toInt, d.toInt)
      }
      out.println(minCost(n, a, b, c, d))
    }

    out.flush()
  }
}