Question and Answer from L4-M2
def solve():
T = int(input())
for _ in range(T):
N, E, H, A, B, C = map(int, input().split())
min_cost = float('inf')
found = False
# Try all possible numbers of cakes (x)
for x in range(0, N+1):
eggs_left = E - x
choco_left = H - x
if eggs_left < 0 or choco_left < 0:
continue
# Maximum omelettes we can make with remaining eggs
max_omelette = eggs_left // 2
# Maximum milkshakes with remaining chocolates
max_milkshake = choco_left // 3
# The rest should be covered by omelettes and milkshakes
total_left = N - x
# y = number of omelettes, z = number of milkshakes
# Try to assign as much as possible according to cost
y = min(max_omelette, total_left)
z = total_left - y
if z <= max_milkshake:
found = True
total_cost = x * C + y * A + z * B
min_cost = min(min_cost, total_cost)
print(min_cost if found else -1)
if __name__ == "__main__":
solve()