(*
ocamlfind ocamlopt -package batteries -linkpkg main.ml -o a.out
*)
open Batteries


let t = Scanf.sscanf (read_line ()) "%d" ( fun t -> t)

let lst = 
        List.init t (
                fun _ -> 
                        Scanf.sscanf (read_line ()) "%d %d %d %d %d" (
                                fun n a b c d -> 
                                        (n, a, b, c, d)
                        )
        )

(* - purpose
        nに到達するまでに払うコインの最小値を求める
*)
(* - function type
        solver: int -> int -> int -> int -> int -> int -> int

   - accumulator(option)
   	ac1: (detail) 
   	ac2: (detail) 
*)

let sort lst = 
        List.fast_sort (fun el1 el2 ->
                match el1 with
                        | (_, el1) -> 
                                (
                                        match el2 with
                                                | (_, el2) -> 
                                                        if el1 = el2 then
                                                                0
                                                        else
                                                        if el1 < el2 then
                                                                -1
                                                        else
                                                                1
                                )
        ) lst


let rec f n a b c d current ac =
        if n - current = 0 then
                0
        else
                let ap = (0, abs (n - (current * 2))) in
                let bp = (1, abs (n - (current * 3))) in
                let cp = (2, abs (n - (current * 5))) in
                let dip = (3, abs (n - (current + 1))) in
                let ddp = (4, abs (n - (current - 1))) in
                let cnlst = [ap; bp; cp; dip; ddp] in
                let sorted = sort cnlst in
                let hd = List.hd sorted in
                        match hd with
                                | (cn, _) -> 
                                        if cn = 0 then
                                                a + (f n a b c d (current * 2) ([current] @ ac))
                                        else
                                        if cn = 1 then
                                                b + (f n a b c d (current * 3) ([current] @ ac))
                                        else
                                        if cn = 2 then
                                                c + (f n a b c d (current * 5) ([current] @ ac))
                                        else
                                        if cn = 3 then
                                                d + (f n a b c d (current + 1) ([current] @ ac))
                                        else
                                                d + (f n a b c d (current - 1) ([current] @ ac))


let () = 
        List.iter (fun elmnt ->
                match elmnt with
                        | (n, a, b, c, d) -> 
                                8 + f n a b c d 1 []
                                |> Printf.printf "%d\n"
        ) lst
(*
let rec f n a b c d current =
        if n - current = 0 then
                0
        else
        if current <= 0 then
                (* d(inc)を払った場合 *)
                max_int
        else
                (* aを払った場合 *)
                let ifa = (a, a + (f n a b c d (current * 2))) in
                (* bを払った場合 *)
                let ifb = (b, b + (f n a b c d (current * 3))) in
                (* cを払った場合 *)
                let ifc = (c, c + (f n a b c d (current * 5))) in
                (* d(inc)を払った場合 *)
                let ifdinc = (d, d + (f n a b c d (current + 1))) in
                (* d(dec)を払った場合 *)
                let ifddec = (d, d + (f n a b c d (current - 1))) in
                let cnlst = [ifa; ifb; ifc; ifdinc; ifddec] in
                let sorted = sort cnlst in
                let hd = List.hd sorted in
                        match hd with
                                | (cn, csum) -> cn + csum

let ans = f n a b c d 0

*)
