import sys def is_valid_kadomatsu(a, b, c): if a <= 0 or b <=0 or c <=0: return False if a == b or b == c or a == c: return False sorted_nums = sorted([a, b, c]) if sorted_nums[1] == a or sorted_nums[1] == c: return True return False def compute_min_cost(a, b, c, x, y, z): # Check if original is already valid if is_valid_kadomatsu(a, b, c): return 0 min_cost = float('inf') # Enumerate possible possibilities by trying various number of operations # This approach tries a few iterations considering possible operation combinations # Not exhaustive but covers common cases for efficiency # Try all possible values of t where t is a small number (up to 4) # This heuristic approach works due to the problem's constraints but may not cover all edge cases for t1 in range(0, 5): for t2 in range(0, 5): for t3 in range(0, 5): new_a = a - t1 - t3 new_b = b - t1 - t2 new_c = c - t2 - t3 if new_a <=0 or new_b <=0 or new_c <=0: continue if is_valid_kadomatsu(new_a, new_b, new_c): cost = t1 * x + t2 * y + t3 * z if cost < min_cost: min_cost = cost return min_cost if min_cost != float('inf') else -1 def main(): input = sys.stdin.read().split() T = int(input[0]) idx = 1 for _ in range(T): A = int(input[idx]) B = int(input[idx+1]) C = int(input[idx+2]) X = int(input[idx+3]) Y = int(input[idx+4]) Z = int(input[idx+5]) idx +=6 res = compute_min_cost(A, B, C, X, Y, Z) print(res if res != float('inf') else -1) if __name__ == '__main__': main()