defmodule Main do def input, do: IO.read(:line) |> String.trim def ii, do: input() |> String.to_integer def li, do: input() |> String.split |> Enum.map(&String.to_integer/1) def yn(b), do: IO.puts(if b, do: "Yes", else: "No") @inf 10 ** 14 def main do t = ii() for _ <- 1..t do [a, b, c, x, y, z] = li() case kadomatu(a, b, c) do [] -> -1 res -> res |> Enum.map(fn {a1, b1, c1} -> cost(a, b, c, a1, b1, c1, x, y, z) end) |> Enum.min end |> then(fn x -> if x >= @inf, do: -1, else: x end) end |> Enum.join("\n") |> IO.puts end # {a, b, c} -> {a1, b1, c1} にするコスト # a <= a1, b <= b1, c <= c1 def cost(a, b, c, a1, b1, c1, x, y, z) do co_a = (a1 - a) * y co_b = (b1 - b) * z co_c = (c1 - c) * x da = (b1 - b) + (c1 - c) db = (a1 - a) + (c1 - c) dc = (a1 - a) + (b1 - b) cond do is_kadomatu(a - da, b - db, c - dc) -> co_a + co_b + co_c true -> @inf end end def kadomatu(a, b, c) do for {a, b, c} <- [{b, a, c}, {b, c, a}, {a, c, b}, {c, a, b}] do f(a, b, c) end |> List.flatten |> Enum.uniq |> Enum.filter(fn {x, y, z} -> x >= a and y >= b and z >= c end) end # a < b < c となるように b, c に加算 def f(a, b, c) do cond do a >= b -> f(a, a+1, c) b >= c -> f(a, b, b+1) true -> [{a, c, b}, {b, c, a}, {b, a, c}, {c, a, b}] end end def is_kadomatu(a, b, c) do cond do a <= 0 or b <= 0 or c <= 0 -> false a == b or b == c or c == a -> false true -> [_, x, _] = Enum.sort([a, b, c]) b != x end end end