import sys def is_kadomatsu(a, b, c): if a == b or b == c or a == c: return False sorted_nums = sorted([a, b, c]) second = sorted_nums[1] return second == a or second == c def compute_min_cost(A, B, C, X, Y, Z): if is_kadomatsu(A, B, C): return 0 min_cost = float('inf') # Generate possible values for A', B', C' by considering up to 2 reductions each # This is a heuristic approach to limit the search space for a_red in range(0, 3): for b_red in range(0, 3): for c_red in range(0, 3): for k in range(0, a_red + 1): for m in range(0, b_red + 1): for n in range(0, c_red + 1): A_new = A - k - n B_new = B - k - m C_new = C - m - n if A_new <= 0 or B_new <= 0 or C_new <= 0: continue if is_kadomatsu(A_new, B_new, C_new): cost = k * X + m * Y + n * 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() idx = 0 T = int(input[idx]) 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 result = compute_min_cost(A, B, C, X, Y, Z) print(result if result != float('inf') else -1) if __name__ == '__main__': main()