import sys def is_kadomatsu(a, b, c): if a == b or b == c or a == c: return False sorted_vals = sorted([a, b, c]) mid = sorted_vals[1] return mid == a or mid == c 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 # Check initial condition if is_kadomatsu(A, B, C): print(0) continue min_cost = float('inf') # Check possible operations up to 200 steps (adjust if necessary) max_steps = 200 for m in range(max_steps+1): for n in range(max_steps+1): for p in range(max_steps+1): a_new = A - m - p b_new = B - m - n c_new = C - n - p if a_new <1 or b_new <1 or c_new <1: continue if a_new == b_new or b_new == c_new or a_new == c_new: continue sorted_vals = sorted([a_new, b_new, c_new]) mid = sorted_vals[1] if mid == a_new or mid == c_new: cost = X * m + Y * n + Z * p if cost < min_cost: min_cost = cost if min_cost != float('inf'): print(min_cost) else: print(-1) if __name__ == '__main__': main()