Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.

Consider a hexagonal tiling of the plane as on the picture below. [Image] 

Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] 

Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell.


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows.

The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon.

The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).


-----Output-----

For each testcase output the smallest cost of a path from the origin to the given cell.


-----Example-----
Input
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Output
18
1000000000000000000



-----Note-----

The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]

Difficulty: HARD
Tags: Constructive algorithms

Shortest paths

Implementation

Complete search

Greedy algorithms

Mathematics
Skill types: Complete search

Greedy algorithms

Starter code:

Solutions:
import io
import os
import sys
import math
import heapq
input = sys.stdin.readline
mod = 10 ** 9 + 7
t = int(input())
for _ in range(t):
	(x, y) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	arr = arr + arr + arr
	for z in range(20):
		for i in range(1, 16):
			arr[i] = min(arr[i], arr[i - 1] + arr[i + 1])
	(c1, c2, c3, c4, c5, c6) = arr[6:12]
	if x >= 0 and y >= 0:
		ans = 0
		mm = min(x, y)
		x -= mm
		y -= mm
		ans += mm * c1
		ans += y * c2
		ans += x * c6
		print(ans)
	elif x >= 0 and y < 0:
		ans = 0
		ans += abs(y) * c5
		ans += x * c6
		print(ans)
	elif x < 0 and y < 0:
		ans = 0
		(x, y) = (abs(x), abs(y))
		mm = min(x, y)
		x -= mm
		y -= mm
		ans += mm * c4
		ans += y * c5
		ans += x * c3
		print(ans)
	else:
		ans = 0
		ans += abs(y) * c2
		ans += abs(x) * c3
		print(ans)


from collections import defaultdict
mod = 10 ** 9 + 7
INF = float('inf')

def getlist():
	return list(map(int, input().split()))

def inverse(N, mod):
	return pow(N, mod - 2, mod)

def main():
	T = int(input())
	for _ in range(T):
		(x, y) = getlist()
		C = getlist()
		D = [INF] * 8
		D[0] = min(C[5], C[0] + C[4])
		D[1] = min(C[0], C[1] + C[5])
		D[2] = min(C[1], C[0] + C[2])
		D[3] = min(C[1] + C[2], C[0] + 2 * C[2], C[3] + 2 * C[1])
		D[4] = min(C[2], C[1] + C[3])
		D[5] = min(C[3], C[2] + C[4])
		D[6] = min(C[4], C[3] + C[5])
		D[7] = min(C[4] + C[5], C[3] + 2 * C[5], C[0] + 2 * C[4])
		if x >= 0 and y >= 0:
			m = min(x, y)
			ans = D[1] * m + D[0] * (x - m) + D[2] * (y - m)
		elif x >= 0 and y < 0:
			m = min(x, -y)
			ans = D[7] * m + D[0] * (x - m) + D[6] * (-y - m)
		elif x < 0 and y >= 0:
			m = min(-x, y)
			ans = D[3] * m + D[4] * (-x - m) + D[2] * (y - m)
		else:
			m = min(-x, -y)
			ans = D[5] * m + D[4] * (-x - m) + D[6] * (-y - m)
		print(ans)

def __starting_point():
	main()
__starting_point()


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write('\n'.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
INF = float('inf')
mod = 10 ** 9 + 7
for t in range(int(data())):
	(y, x) = mdata()
	c = mdata()
	if x >= 0 and y >= 0:
		out(min(c[1] * x + c[5] * y, c[0] * min(x, y) + c[1] * (x - min(x, y)) + c[5] * (y - min(x, y)), c[0] * max(x, y) + c[4] * (max(x, y) - x) + c[2] * (max(x, y) - y)))
	elif x > 0 and y < 0:
		y = abs(y)
		out(min(c[1] * x + c[2] * y, c[0] * x + c[2] * (x + y), c[3] * y + c[1] * (x + y)))
	elif x < 0 and y > 0:
		x = abs(x)
		out(min(c[4] * x + c[5] * y, c[3] * x + c[5] * (x + y), c[0] * y + c[4] * (x + y)))
	else:
		x = abs(x)
		y = abs(y)
		out(min(c[4] * x + c[2] * y, c[3] * min(x, y) + c[4] * (x - min(x, y)) + c[2] * (y - min(x, y)), c[3] * max(x, y) + c[1] * (max(x, y) - x) + c[5] * (max(x, y) - y)))


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		(a, b) = ria()
		(c1, c2, c3, c4, c5, c6) = ria()
		if a >= 0 and b >= 0:
			cost1 = c6 * a + c2 * b
			if a >= b:
				cost2 = c1 * b + c6 * (a - b)
				cost3 = c1 * a + c5 * (a - b)
			else:
				cost2 = c1 * b + c3 * (b - a)
				cost3 = c1 * a + c2 * (b - a)
		elif a <= 0 and b >= 0:
			a = -a
			cost1 = c3 * a + c2 * b
			cost2 = c4 * a + c2 * (a + b)
			cost3 = c1 * b + c3 * (a + b)
		elif a <= 0 and b <= 0:
			a = -a
			b = -b
			cost1 = c3 * a + c5 * b
			if a >= b:
				cost2 = c4 * b + c3 * (a - b)
				cost3 = c4 * a + c2 * (a - b)
			else:
				cost2 = c4 * b + c6 * (b - a)
				cost3 = c4 * a + c5 * (b - a)
		elif b <= 0 and a >= 0:
			b = -b
			cost1 = c6 * a + c5 * b
			cost2 = c1 * a + c5 * (a + b)
			cost3 = c4 * b + c6 * (a + b)
		print(min(cost1, cost2, cost3))
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


def read_int():
	return int(input())

def read_ints():
	return map(int, input().split(' '))
d = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
t = read_int()
for case_num in range(t):
	(x, y) = read_ints()
	c = list(read_ints())
	ans = -1
	for i in range(6):
		dx = d[i][0]
		dy = d[i][1]
		if dx != 0 and x // dx >= 0:
			t = x // dx
			cost = t * c[i]
			y1 = d[i][1] * t
			for j in range(6):
				if j == i or d[j][0] != 0:
					continue
				t1 = (y - y1) // d[j][1]
				if t1 >= 0:
					nc = cost + t1 * c[j]
					if ans == -1 or ans > nc:
						ans = nc
		if dy != 0 and y // dy >= 0:
			t = y // dy
			cost = t * c[i]
			x1 = d[i][0] * t
			for j in range(6):
				if j == i or d[j][1] != 0:
					continue
				t1 = (x - x1) // d[j][0]
				if t1 >= 0:
					nc = cost + t1 * c[j]
					if ans == -1 or ans > nc:
						ans = nc
	print(ans)


t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	path1 = abs(x) * c[0 if x >= 0 else 3] + abs(y - x) * c[1 if y - x >= 0 else 4]
	path2 = abs(y) * c[0 if y >= 0 else 3] + abs(x - y) * c[5 if x - y >= 0 else 2]
	path3 = abs(y) * c[1 if y >= 0 else 4] + abs(x) * c[5 if x >= 0 else 2]
	print(min(path1, path2, path3))


t = int(input())
while t > 0:
	(y, x) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	rh = min(c2, c1 + c3)
	lh = min(c5, c4 + c6)
	uv = min(c6, c1 + lh)
	lv = min(c3, rh + c4)
	ud = min(c1, uv + rh)
	ld = min(c4, lv + lh)
	a = 0
	if y > 0:
		rs = y * ud
		ls = y * uv
		dl = x - y
		if dl > 0:
			rs += dl * rh
		else:
			rs += abs(dl) * lh
		if x > 0:
			ls += x * rh
		else:
			ls += abs(x) * lh
		if x <= y and x >= 0:
			print(min(rs, ls, x * ud + (y - x) * uv))
		else:
			print(min(rs, ls))
	else:
		rs = abs(y) * ld
		ls = abs(y) * lv
		dl = x - y
		if dl > 0:
			rs += dl * rh
		else:
			rs += abs(dl) * lh
		if x > 0:
			ls += x * rh
		else:
			ls += abs(x) * lh
		if x <= 0 and x >= y:
			print(min(rs, ls, abs(x) * ld + (abs(y) - abs(x)) * lv))
		else:
			print(min(rs, ls))
	t -= 1


def solve():
	(y, x) = map(int, input().split())
	c = list(map(int, input().split()))
	if x == 0 and y == 0:
		return 0
	d1 = 0
	if x > 0:
		d1 += x * c[1]
	else:
		d1 += abs(x) * c[4]
	if y > 0:
		d1 += y * c[5]
	else:
		d1 += abs(y) * c[2]
	d2 = 0
	if x > 0:
		d2 += x * c[0]
	else:
		d2 += abs(x) * c[3]
	if x > y:
		d2 += (x - y) * c[2]
	else:
		d2 += (y - x) * c[5]
	d3 = 0
	if y > 0:
		d3 += y * c[0]
	else:
		d3 += abs(y) * c[3]
	if y > x:
		d3 += (y - x) * c[4]
	else:
		d3 += (x - y) * c[1]
	return min(d1, d2, d3)
t = int(input())
i = 0
while i < t:
	print(solve())
	i += 1


n = int(input())

def calc(x, y, z):
	if x >= 0:
		return x * y
	else:
		return abs(x) * z
for j in range(n):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	best = 9223372036854775807
	best = min(best, calc(x, c6, c3) + calc(y, c2, c5))
	best = min(best, calc(y, c1, c4) + calc(x - y, c6, c3))
	best = min(best, calc(x, c1, c4) + calc(y - x, c2, c5))
	print(best)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from heapq import *
from math import gcd, factorial, floor, ceil
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 10 ** 9 + 7

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = 10 ** 30

def solve(case):
	(x, y) = sep()
	c = lis()
	(c[2], c[4]) = (c[4], c[2])
	ans = inf
	if x >= 0 and y >= 0:
		ans = min(ans, c[5] * abs(x) + c[1] * abs(y))
		if y == x:
			ans = min(ans, c[0] * x)
		elif abs(y) < x:
			ans = min(ans, c[0] * abs(y) + c[5] * (x - y))
			ans = min(ans, c[0] * abs(x) + c[2] * (x - y))
		else:
			ans = min(ans, c[0] * abs(x) + c[1] * (y - x))
			ans = min(ans, c[0] * abs(y) + c[4] * (y - x))
	if x >= 0 and y <= 0:
		ans = min(ans, c[5] * x + c[2] * -y)
		if y == x:
			ans = min(ans, c[0] * x)
		elif abs(y) < abs(x):
			ans = min(ans, c[3] * -y + c[5] * (x - y))
			ans = min(ans, c[0] * x + c[2] * (x - y))
		else:
			ans = min(ans, c[0] * x + c[2] * (-y + x))
			ans = min(ans, c[3] * -y + c[5] * (-y + x))
	if x <= 0 and y >= 0:
		ans = min(ans, c[4] * abs(x) + c[1] * y)
		if y == x:
			ans = min(ans, c[0] * x)
		elif abs(y) < abs(x):
			ans = min(ans, c[0] * y + c[4] * (-x + y))
			ans = min(ans, c[3] * -x + c[1] * (-x + y))
		else:
			ans = min(ans, c[3] * -x + c[1] * (y - x))
			ans = min(ans, c[0] * y + c[4] * (y - x))
	if x <= 0 and y <= 0:
		ans = min(ans, c[4] * abs(x) + c[2] * -y)
		if y == x:
			ans = min(ans, c[3] * abs(x))
		elif abs(y) < abs(x):
			ans = min(ans, c[3] * abs(y) + c[4] * (abs(x) - abs(y)))
			ans = min(ans, c[3] * -x + c[1] * (abs(x) - abs(y)))
		else:
			ans = min(ans, c[3] * -x + c[2] * (abs(y) - abs(x)))
			ans = min(ans, c[3] * -y + c[5] * (abs(y) - abs(x)))
	print(ans)
testcase(int(inp()))


t = int(input())
while t:
	t += -1
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	(ans, t2) = (999999999999999999999, 0)
	if y > 0:
		t1 = min(c2 * y, (c1 + c3) * y)
		if x > 0:
			t2 = min(c6 * x, (c5 + c1) * x)
		else:
			t2 = min(c3, c4 + c2) * abs(x)
		ans = min(ans, t1 + t2)
		t1 = min(c1, c6 + c2) * y
		if x > y:
			t2 = min(c6, c5 + c1) * (x - y)
		else:
			t2 = min(c3, c4 + c2) * (y - x)
		ans = min(ans, t1 + t2)
	else:
		t1 = min(c5, c4 + c6) * abs(y)
		if x > 0:
			t2 = min(c6 * x, (c5 + c1) * x)
		else:
			t2 = min(c3, c4 + c2) * abs(x)
		ans = min(ans, t1 + t2)
		t1 = min(c4, c5 + c3) * abs(y)
		if x > y:
			t2 = min(c6, c5 + c1) * (x - y)
		else:
			t2 = min(c3, c4 + c2) * (y - x)
		ans = min(ans, t1 + t2)
	if x > 0:
		t1 = min(c6, c5 + c1) * x
		if y > 0:
			t2 = min(c2, c3 + c1) * y
		else:
			t2 = min(c5, c4 + c6) * abs(y)
		ans = min(ans, t1 + t2)
		t1 = min(c1, c6 + c2) * x
		if y > x:
			t2 = min(c2, c3 + c1) * (y - x)
		else:
			t2 = min(c5, c4 + c6) * (x - y)
		ans = min(ans, t1 + t2)
	else:
		t1 = min(c3, c4 + c2) * abs(x)
		if y > 0:
			t2 = min(c2, c3 + c1) * y
		else:
			t2 = min(c5, c4 + c6) * abs(y)
		ans = min(ans, t1 + t2)
		t1 = min(c4, c5 + c3) * abs(x)
		if y > x:
			t2 = min(c2, c3 + c1) * (y - x)
		else:
			t2 = min(c5, c4 + c6) * (x - y)
		ans = min(ans, t1 + t2)
	print(ans)


from math import *
from sys import *
input = stdin.readline
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	for turn in range(36):
		for index in range(6):
			e1 = index - 1
			if e1 < 0:
				e1 = e1 + 6
			c[index] = min(c[index], c[e1] + c[(index + 1) % 6])
	ans = 0
	if x >= 0 and y >= 0:
		d = min(x, y)
		(x, y) = (x - d, y - d)
		ans = d * c[0]
		ans += x * c[5] + y * c[1]
	elif x < 0 and y < 0:
		d = abs(max(x, y))
		(x, y) = (x + d, y + d)
		ans = d * c[3]
		ans += abs(x) * c[2] + abs(y) * c[4]
	elif x <= 0 and y >= 0:
		ans = abs(x) * c[2] + y * c[1]
	elif x >= 0 and y <= 0:
		ans = x * c[5] + abs(y) * c[4]
	stdout.write(str(ans) + '\n')


import math as mt
import bisect
import sys
input = sys.stdin.readline
t = int(input())
import collections
import heapq
p = 10 ** 9 + 7

def ncr_util():
	inv[0] = inv[1] = 1
	fact[0] = fact[1] = 1
	for i in range(2, 300001):
		inv[i] = inv[i % p] * (p - p // i) % p
	for i in range(1, 300001):
		inv[i] = inv[i - 1] * inv[i] % p
		fact[i] = fact[i - 1] * i % p

def solve(x, y):
	for i in range(36):
		l[1] = min(l[1], l[2] + l[6])
		l[2] = min(l[2], l[1] + l[3])
		l[3] = min(l[3], l[2] + l[4])
		l[4] = min(l[4], l[3] + l[5])
		l[5] = min(l[5], l[4] + l[6])
		l[6] = min(l[6], l[1] + l[5])
	if x == 0 and y == 0:
		return 0
	ans = 0
	if x >= 0 and y >= 0:
		mini = min(x, y)
		x -= mini
		y -= mini
		ans += mini * l[1]
		if y > 0:
			ans += y * l[2]
		else:
			ans += x * l[6]
	elif x <= 0 and y <= 0:
		x = abs(x)
		y = abs(y)
		mini = min(x, y)
		x -= mini
		y -= mini
		ans += mini * l[4]
		if y > 0:
			ans += y * l[5]
		else:
			ans += x * l[3]
	elif x >= 0 and y <= 0:
		ans += abs(x) * l[6]
		ans += abs(y) * l[5]
	else:
		ans += abs(y) * l[2]
		ans += abs(x) * l[3]
	return ans
for _ in range(t):
	(x, y) = map(int, input().split())
	l = list(map(int, input().split()))
	l.insert(0, 0)
	print(solve(x, y))


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c for j in range(b)] for i in range(a)]

def list3d(a, b, c, d):
	return [[[d for k in range(c)] for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** (-10)
for _ in range(INT()):
	(gy, gx) = MAP()
	C = LIST()
	C[5] = min(C[5], C[0] + C[4])
	C[1] = min(C[1], C[0] + C[2])
	C[2] = min(C[2], C[3] + C[1])
	C[4] = min(C[4], C[3] + C[5])
	C[5] = min(C[5], C[0] + C[4])
	C[1] = min(C[1], C[0] + C[2])
	C[2] = min(C[2], C[3] + C[1])
	C[4] = min(C[4], C[3] + C[5])
	ans = 0
	if gx > 0 and gy > 0:
		mn = min(gx, gy)
		ans += mn * min(C[1] + C[5], C[0])
		if gx > gy:
			ans += (gx - mn) * C[1]
		elif gx < gy:
			ans += (gy - mn) * C[5]
	elif gx < 0 and gy < 0:
		(gx, gy) = (abs(gx), abs(gy))
		mn = min(gx, gy)
		ans += mn * min(C[2] + C[4], C[3])
		if gx > gy:
			ans += (gx - mn) * C[4]
		elif gx < gy:
			ans += (gy - mn) * C[2]
	else:
		if gx < 0:
			ans += abs(gx) * C[4]
		elif gx > 0:
			ans += gx * C[1]
		if gy < 0:
			ans += abs(gy) * C[2]
		elif gy > 0:
			ans += gy * C[5]
	print(ans)


import sys
import math
from functools import reduce
import bisect

def getN():
	return int(input())

def getNM():
	return map(int, input().split())

def getList():
	return list(map(int, input().split()))

def input():
	return sys.stdin.readline().rstrip()

def index(a, x):
	i = bisect.bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	return False
for _ in range(int(input())):
	(y, x) = getNM()
	(c1, c2, c3, c4, c5, c6) = getNM()
	for i in range(10):
		c1 = min(c1, c2 + c6)
		c2 = min(c2, c1 + c3)
		c3 = min(c3, c2 + c4)
		c4 = min(c4, c3 + c5)
		c5 = min(c5, c4 + c6)
		c6 = min(c6, c5 + c1)
	if x >= 0:
		if y >= 0:
			m = min(x, y)
			x -= m
			y -= m
			print(c1 * m + c2 * x + c6 * y)
		else:
			y = abs(y)
			print(c3 * y + c2 * x)
	elif y >= 0:
		x = abs(x)
		print(c5 * x + c6 * y)
	else:
		x = abs(x)
		y = abs(y)
		m = min(x, y)
		x -= m
		y -= m
		print(c4 * m + c5 * x + c3 * y)


gans = []
for _ in range(int(input())):
	(y, x) = list(map(int, input().split()))
	(c2, c3, c4, c5, c6, c1) = list(map(int, input().split()))
	for i in range(6):
		c1 = min(c1, c6 + c2)
		c2 = min(c2, c1 + c3)
		c3 = min(c3, c2 + c4)
		c4 = min(c4, c3 + c5)
		c5 = min(c5, c4 + c6)
		c6 = min(c6, c5 + c1)
	if x == 0 and y == 0:
		gans.append(0)
		continue
	elif x == 0:
		if y > 0:
			ans = y * c1
		else:
			ans = -y * c4
		gans.append(ans)
		continue
	elif y == 0:
		if x > 0:
			ans = x * c3
		else:
			ans = -x * c6
		gans.append(ans)
		continue
	if x * y > 0:
		if x > 0:
			(a, b, c) = (c1, c2, c3)
		else:
			(a, b, c) = (c4, c5, c6)
			x = -x
			y = -y
		ans = min(x, y) * b
		if x > y:
			ans += (x - y) * c
		else:
			ans += (y - x) * a
		gans.append(ans)
	else:
		if x < 0:
			ans = -c6 * x + c1 * y
		else:
			ans = -c4 * y + c3 * x
		gans.append(ans)
print('\n'.join(map(str, gans)))


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	(n, m) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	c1 = min(c1, c2 + c6)
	c2 = min(c2, c1 + c3)
	c3 = min(c3, c2 + c4)
	c4 = min(c4, c3 + c5)
	c5 = min(c5, c4 + c6)
	c6 = min(c6, c5 + c1)
	ans = 0
	if m > 0:
		if n <= 0:
			ans = c3 * -n + c2 * m
		elif n <= m:
			ans = c1 * n + c2 * (m - n)
		else:
			ans = c1 * m + c6 * (n - m)
	elif m < 0:
		if n <= 0:
			if n <= m:
				ans = c4 * -m + c3 * (-n + m)
			else:
				ans = c4 * -n + c5 * (n - m)
		else:
			ans = c6 * n + c5 * -m
	elif n <= 0:
		ans = c3 * -n
	else:
		ans = c6 * n
	print(ans)


def calc():
	global c1, c2, c3, c4, c5, c6, x1, x2, x3, x4, x5, x6
	return x1 * c1 + x2 * c2 + x3 * c3 + x4 * c4 + x5 * c5 + x6 * c6
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	ans = []
	if x > 0 and y > 0:
		(x1, x2, x3, x4, x5, x6) = (0, y, 0, 0, 0, x)
	elif x > 0:
		(x1, x2, x3, x4, x5, x6) = (0, 0, 0, 0, -y, x)
	elif y > 0:
		(x1, x2, x3, x4, x5, x6) = (0, y, -x, 0, 0, 0)
	else:
		(x1, x2, x3, x4, x5, x6) = (0, 0, -x, 0, -y, 0)
	ans.append(calc())
	if x > 0 and y - x > 0:
		(x1, x2, x3, x4, x5, x6) = (x, y - x, 0, 0, 0, 0)
	elif y - x > 0:
		(x1, x2, x3, x4, x5, x6) = (0, y - x, 0, -x, 0, 0)
	elif x > 0:
		(x1, x2, x3, x4, x5, x6) = (x, 0, 0, 0, x - y, 0)
	else:
		(x1, x2, x3, x4, x5, x6) = (0, 0, 0, -x, x - y, 0)
	ans.append(calc())
	if y > 0 and x - y > 0:
		(x1, x2, x3, x4, x5, x6) = (y, 0, 0, 0, 0, x - y)
	elif x - y > 0:
		(x1, x2, x3, x4, x5, x6) = (0, 0, 0, -y, 0, x - y)
	elif y > 0:
		(x1, x2, x3, x4, x5, x6) = (y, 0, y - x, 0, 0, 0)
	else:
		(x1, x2, x3, x4, x5, x6) = (0, 0, y - x, -y, 0, 0)
	ans.append(calc())
	print(min(ans))


for _ in range(int(input())):
	(y, x) = map(int, input().split())
	(uu, w, r, dd, q, e) = map(int, input().split())
	(uuo, wo, ro, ddo, qo, eo) = [None] * 6
	while (uuo, wo, ro, ddo, qo, eo) != (uu, w, r, dd, q, e):
		(uuo, wo, ro, ddo, qo, eo) = (uu, w, r, dd, q, e)
		uu = min(uuo, w + e)
		dd = min(ddo, q + r)
		r = min(ro, dd + w)
		e = min(eo, uu + q)
		q = min(qo, dd + e)
		w = min(wo, uu + r)
	if x >= 0 and y >= 0:
		ans = min(x, y) * uu
		if y > x:
			ans += e * (y - x)
		else:
			ans += w * (x - y)
	elif x <= 0 and y <= 0:
		x = abs(x)
		y = abs(y)
		ans = min(x, y) * dd
		if y > x:
			ans += r * (y - x)
		else:
			ans += q * (x - y)
	elif x >= 0:
		ans = x * w - y * r
	else:
		ans = -x * q + y * e
	print(ans)


for i in range(int(input())):
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	b.append(b[0])
	b.insert(0, b[-2])
	for j in range(1, len(b) - 1):
		b[j] = min(b[j - 1] + b[j + 1], b[j])
	s = 0
	if a[1] > 0 and a[0] > 0:
		s += min(a[1], a[0]) * b[1]
		c = min(a[1], a[0])
		a[1] -= c
		a[0] -= c
	elif a[1] < 0 and a[0] < 0:
		s -= max(a[1], a[0]) * b[4]
		c = max(a[1], a[0])
		a[1] -= c
		a[0] -= c
	if a[0] > 0:
		s += a[0] * b[6]
	if a[0] < 0:
		s += -a[0] * b[3]
	if a[1] > 0:
		s += a[1] * b[2]
	if a[1] < 0:
		s += -a[1] * b[5]
	print(s)


for nt in range(int(input())):
	(y, x) = list(map(int, input().split()))
	(c1, c2, c3, c4, c5, c6) = list(map(int, input().split()))
	for i in range(10):
		c2 = min(c2, c1 + c3)
		c3 = min(c3, c2 + c4)
		c4 = min(c4, c3 + c5)
		c5 = min(c5, c4 + c6)
		c6 = min(c6, c5 + c1)
		c1 = min(c1, c6 + c2)
	if x >= 0:
		if y >= 0:
			m = min(x, y)
			x -= m
			y -= m
			print(m * c1 + c2 * x + c6 * y)
		else:
			y = abs(y)
			print(x * c2 + y * c3)
	else:
		x = abs(x)
		if y >= 0:
			print(x * c5 + y * c6)
		else:
			y = abs(y)
			m = min(x, y)
			x -= m
			y -= m
			print(m * c4 + x * c5 + y * c3)


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	if x == y == 0:
		print(0)
		continue
	ans = 10 ** 20
	if x >= 0 and y >= 0 and (y >= x):
		ans = min(ans, c2 * (y - x) + c1 * x)
	if y >= 0 and x <= y:
		ans = min(ans, c1 * y + c3 * (y - x))
	if x >= 0 and y <= x:
		ans = min(ans, c1 * x + c5 * (x - y))
	if x >= 0 and y <= x and (y >= 0):
		ans = min(ans, c6 * (x - y) + c1 * y)
	if x <= 0 and y >= 0:
		ans = min(ans, c2 * y + c3 * abs(x))
	if x <= 0 and y >= x:
		ans = min(ans, c2 * (y - x) + c4 * abs(x))
	if x >= 0 and y >= 0:
		ans = min(ans, c2 * y + c6 * x)
	if y <= 0 and x <= y:
		ans = min(ans, c3 * (y - x) + c4 * abs(y))
	if x <= 0 and y <= 0:
		ans = min(ans, c3 * abs(x) + c5 * abs(y))
	if x <= 0 and y <= 0 and (y <= x):
		ans = min(ans, c4 * abs(x) + c5 * (x - y))
	if y <= 0 and x >= y:
		ans = min(ans, c4 * abs(y) + c6 * (x - y))
	if x >= 0 and y <= 0:
		ans = min(ans, c5 * abs(y) + c6 * x)
	print(ans)


p = lambda : list(map(int, input().split()))
T = int(input())
for _ in range(T):
	(Y, X) = p()
	c = p()
	for i in range(6):
		c[i] = min(c[i], c[(i + 1) % 6] + c[(i + 5) % 6])
	M = min(abs(X), abs(Y))
	if X > 0:
		if Y > 0:
			C = M * c[0]
			X -= M
			Y -= M
			C += c[1] * X
			C += c[5] * Y
			print(C)
		else:
			C = c[1] * X
			C += c[2] * -Y
			print(C)
	elif Y > 0:
		C = c[5] * Y
		C += c[4] * -X
		print(C)
	else:
		C = M * c[3]
		X += M
		Y += M
		C += c[4] * -X
		C += c[2] * -Y
		print(C)


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	c1 = min(c1, c2 + c6)
	c2 = min(c2, c1 + c3)
	c3 = min(c3, c2 + c4)
	c4 = min(c4, c3 + c5)
	c5 = min(c5, c4 + c6)
	c6 = min(c6, c1 + c5)
	ans = 0
	if x * y <= 0:
		ans += c6 * x if x >= 0 else c3 * -x
		ans += c2 * y if y >= 0 else c5 * -y
	else:
		if x < 0 or y < 0:
			cc = max(x, y)
			x -= cc
			y -= cc
			ans += c4 * -cc
		else:
			cc = min(x, y)
			x -= cc
			y -= cc
			ans += c1 * cc
		ans += c6 * x if x >= 0 else c3 * -x
		ans += c2 * y if y >= 0 else c5 * -y
	print(ans)


from math import sqrt

def hex2cart(r, c):
	return (c - r / 2, sqrt(3) / 2 * r)

def cart2hex(x, y):
	r = 2 * (y / sqrt(3))
	c = x + r / 2
	return (int(r), int(c))

def hex2slope(r, c):
	(x, y) = hex2cart(r, c)
	return y / x
DIRECTIONS = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
HDIRECTIONS = DIRECTIONS[:3]
CARDINAL_SLOPES = [0.0, sqrt(3), -sqrt(3)]

def reach(r0, c0, r1, c1):
	r1 -= r0
	c1 -= c0
	for (i, (dr, dc)) in enumerate(DIRECTIONS):
		m = max(abs(r1), abs(c1))
		if (m * dr, m * dc) == (r1, c1):
			return (i, m)
	return (-1, 0)

def try2(r, c, costs):
	best = None
	for (di, (dr0, dc0)) in enumerate(HDIRECTIONS):
		for (dj, (dr1, dc1)) in enumerate(HDIRECTIONS):
			if di == dj:
				continue
			matrix = [[dr0, -dr1, 0 - r], [dc0, -dc1, 0 - c]]
			if matrix[0][0] == 0:
				matrix = matrix[::-1]
			if matrix[0][0] == -1:
				matrix[0] = [-x for x in matrix[0]]
			if matrix[1][0] != 0:
				matrix[1] = [x - matrix[1][0] * y for (x, y) in zip(matrix[1], matrix[0])]
			if matrix[1][1] < 0:
				matrix[1] = [-x for x in matrix[1]]
			if matrix[0][1] != 0:
				matrix[0] = [x - matrix[0][1] * y for (x, y) in zip(matrix[0], matrix[1])]
			if matrix[1][:2] == [0, 0] and matrix[1][-1] != 0:
				continue
			if matrix[1][:2] == [0, 1]:
				m = -matrix[1][-1]
				k = m * -matrix[0][1] - matrix[0][2]
				r0 = dr0 * k
				c0 = dc0 * k
				m1 = reach(0, 0, r0, c0)
				m2 = reach(r0, c0, r, c)
				cost = m1[1] * costs[m1[0]] + m2[1] * costs[m2[0]]
				if best is None or cost < best:
					best = cost
	return best

def solve(r, c, costs):
	t2 = try2(r, c, costs)
	return t2

def main():
	nt = int(input())
	for _ in range(nt):
		(x, y) = map(int, input().split())
		costs = list(map(int, input().split()))
		print(solve(x, y, costs))

def test():
	print(solve(-3, 1, [1, 3, 5, 7, 9, 11]))
	pass
main()


for _ in range(int(input())):
	(x0, y0) = input().split()
	(x0, y0) = (int(x0), int(y0))
	c = [0] + list(map(int, input().split()))
	c[0] = c[-1]
	c.append(c[1])
	for i in range(1, 7):
		c[i] = min(c[i], c[i - 1] + c[i + 1])
	c[7] = c[1]
	s = (None, [1, 1], [0, 1], [-1, 0], [-1, -1], [0, -1], [1, 0], [1, 1])
	ans = 1e+30
	for i in range(1, 7):
		(x1, y1) = s[i]
		(x2, y2) = s[i + 1]
		a = (x0 * y2 - y0 * x2) // (x1 * y2 - y1 * x2)
		b = (x0 * y1 - y0 * x1) // (x2 * y1 - y2 * x1)
		if a >= 0 and b >= 0:
			ans = min(ans, a * c[i] + b * c[i + 1])
	print(ans)


n = int(input())
for _ in range(n):
	(y, x) = map(int, input().split())
	costs = list(map(int, input().split()))
	for i in range(36):
		costs[i % 6] = min(costs[i % 6], costs[(i + 5) % 6] + costs[(i + 1) % 6])
	ans = 0
	if x >= 0:
		if y >= 0:
			if x >= y:
				ans = (x - y) * costs[1] + y * costs[0]
			else:
				ans = x * costs[0] + (y - x) * costs[5]
		else:
			ans = x * costs[1] - y * costs[2]
	elif y >= 0:
		ans = -x * costs[4] + y * costs[5]
	elif x <= y:
		ans = (y - x) * costs[4] - y * costs[3]
	else:
		ans = -x * costs[3] + (x - y) * costs[2]
	print(ans)


t = int(input())
while t > 0:
	(x, y) = map(int, input().split())
	lst = list(map(int, input().split()))
	cost = lst.copy()
	for i in range(1, 7):
		cost[i % 6] = min(cost[i % 6], cost[(i - 1) % 6] + cost[(i + 1) % 6])
	cnt = [0] * 6
	if x == 0 and y == 0:
		pass
	elif x >= 0 and y >= 0:
		if x == y:
			cnt[0] = x
		elif x > y:
			cnt[0] = y
			cnt[5] = x - y
		elif x < y:
			cnt[0] = x
			cnt[1] = y - x
	elif x <= 0 and y <= 0:
		if x == y:
			cnt[3] = -x
		elif x > y:
			cnt[3] = -x
			cnt[4] = x - y
		elif x < y:
			cnt[3] = -y
			cnt[2] = y - x
	elif x >= 0 and y <= 0:
		cnt[4] = -y
		cnt[5] = x
	else:
		cnt[2] = -x
		cnt[1] = y
	ans = 0
	for i in range(6):
		ans += cnt[i] * cost[i]
	print(ans)
	t -= 1


for _ in range(int(input())):
	(y, x) = map(int, input().split())
	d = abs(x - y)
	ans = 10000000000000000000000000000
	c = [0] + list(map(int, input().split()))
	if x >= 0 and y >= 0:
		path1 = c[2] * x + c[6] * y
		path2 = c[1] * x + c[3] * d
		path3 = c[1] * y + c[2] * d
		if x < y:
			path2 = c[1] * y + c[5] * d
			path3 = c[1] * x + c[6] * d
		ans = min(ans, path1)
		ans = min(ans, path2)
		ans = min(ans, path3)
	elif x < 0 and y >= 0:
		path1 = c[5] * -x + c[6] * y
		path2 = c[1] * y + c[5] * d
		path3 = c[4] * -x + c[6] * d
		ans = min(ans, path1)
		ans = min(ans, path2)
		ans = min(ans, path3)
	elif x < 0 and y < 0:
		path1 = c[5] * -x + c[3] * -y
		path2 = c[4] * -x + c[6] * d
		path3 = c[4] * -y + c[5] * d
		if x > y:
			path2 = c[4] * -y + c[2] * d
			path3 = c[4] * -x + c[3] * d
		ans = min(ans, path1)
		ans = min(ans, path2)
		ans = min(ans, path3)
	elif x >= 0 and y < 0:
		path1 = c[2] * x + c[3] * -y
		path2 = c[1] * x + c[3] * d
		path3 = c[4] * -y + c[2] * d
		ans = min(ans, path1)
		ans = min(ans, path2)
		ans = min(ans, path3)
	print(ans)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
from copy import deepcopy
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var, end='\n'):
	sys.stdout.write(str(var) + end)

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	(x, y) = sp()
	arr = l()
	arr.insert(0, 0)
	answer = inf
	arr[1] = min(arr[1], arr[6] + arr[2])
	arr[4] = min(arr[4], arr[3] + arr[5])
	if x >= 0:
		if y >= x:
			answer = min(answer, arr[1] * x + arr[2] * (y - x))
		else:
			answer = min(answer, arr[1] * x + arr[5] * (x - y))
		if y >= 0:
			answer = min(answer, arr[6] * x + arr[2] * y)
		else:
			answer = min(answer, arr[6] * x + arr[5] * abs(y))
	else:
		if y >= x:
			answer = min(answer, arr[4] * abs(x) + arr[2] * (y - x))
		else:
			answer = min(answer, arr[4] * abs(x) + arr[5] * abs(y - x))
		if y >= 0:
			answer = min(answer, arr[3] * -x + arr[2] * y)
		else:
			answer = min(answer, arr[3] * -x + arr[5] * abs(y))
	if y >= 0:
		if x < y:
			answer = min(answer, arr[1] * y + arr[3] * abs(x - y))
		else:
			answer = min(answer, arr[1] * y + arr[6] * (x - y))
	elif x < y:
		answer = min(answer, arr[4] * abs(y) + arr[3] * abs(x - y))
	else:
		answer = min(answer, arr[4] * abs(y) + arr[6] * (x - y))
	out(answer)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def solve_case():
	(y, x) = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	if x == 0 and y == 0:
		print(0)
		return
	d1 = 0
	if x > 0:
		d1 += x * c[1]
	else:
		d1 -= x * c[4]
	if y > 0:
		d1 += y * c[5]
	else:
		d1 -= y * c[2]
	d2 = 0
	if x > 0:
		d2 += x * c[0]
	else:
		d2 -= x * c[3]
	if x > y:
		d2 += (x - y) * c[2]
	else:
		d2 += (y - x) * c[5]
	d3 = 0
	if y > 0:
		d3 += y * c[0]
	else:
		d3 -= y * c[3]
	if y > x:
		d3 += (y - x) * c[4]
	else:
		d3 += (x - y) * c[1]
	print(min(d1, d2, d3))

def main():
	for _ in range(int(input())):
		solve_case()
main()


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	l = list(map(int, input().split()))
	a = min(l[2], l[3] + l[1])
	b = min(l[5], l[4] + l[0])
	c = min(l[4], l[3] + l[5])
	d = min(l[1], l[0] + l[2])
	e = min(l[1] + l[5], l[0])
	f = min(l[2] + l[4], l[3])
	an = 0
	if x > 0 and y > 0:
		an = min(x, y)
		x -= an
		y -= an
		an *= e
	elif x < 0 and y < 0:
		an = max(x, y)
		x -= an
		y -= an
		an *= -f
	if x < 0:
		an += -x * a
	else:
		an += x * b
	if y < 0:
		an += -y * c
	else:
		an += y * d
	print(an)


import itertools
import sys
(*data,) = map(int, sys.stdin.read().split()[::-1])

def inp():
	return data.pop()

def app(c, speeds, combi, d):
	x = 0
	y = 0
	cost = 0
	for (dire, dist) in zip(combi, d):
		y += speeds[dire][0] * dist
		x += speeds[dire][1] * dist
		cost += c[dire] * dist
	return (y, x, cost)
res = []
for _ in range(inp()):
	(y, x) = (inp(), inp())
	(*c,) = [inp() for _ in range(6)]
	speeds = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
	distances = [abs(x), abs(y), abs(y - x), abs(y + x)]
	ans = float('inf')
	for r in range(4):
		for combi in itertools.combinations(range(6), r):
			is_ok = True
			for i in range(3):
				if i in combi and i + 3 in combi:
					is_ok = False
					break
			if not is_ok:
				continue
			for d in itertools.permutations(distances, r):
				(yy, xx, cost) = app(c, speeds, combi, d)
				if xx == x and yy == y:
					ans = min(ans, cost)
	res.append(ans)
print('\n'.join(map(str, res)))


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = lambda : sys.stdin.buffer.readline().rstrip()
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	a1 = min(c1, c6 + c2)
	a2 = min(c2, c1 + c3)
	a3 = min(c3, c4 + c2)
	a4 = min(c4, c3 + c5)
	a5 = min(c5, c4 + c6)
	a6 = min(c6, c5 + c1)
	c1 = a1
	c2 = a2
	c3 = a3
	c4 = a4
	c5 = a5
	c6 = a6
	k = 0
	if x >= 0 and y >= 0:
		r = min(x, y)
		k += c1 * r
		x -= r
		y -= r
	if x <= 0 and y <= 0:
		r = max(x, y)
		x -= r
		y -= r
		r *= -1
		k += c4 * r
	if y >= 0:
		k += c2 * y
	else:
		y *= -1
		k += c5 * y
	if x >= 0:
		k += c6 * x
	else:
		x *= -1
		k += c3 * x
	print(k)


import sys
input = sys.stdin.readline

def main():
	(x, y) = map(int, input().split())
	clst = list(map(int, input().split()))
	clst[0] = min(clst[0], clst[5] + clst[1])
	clst[1] = min(clst[1], clst[0] + clst[2])
	clst[2] = min(clst[2], clst[1] + clst[3])
	clst[3] = min(clst[3], clst[2] + clst[4])
	clst[4] = min(clst[4], clst[3] + clst[5])
	clst[5] = min(clst[5], clst[4] + clst[0])
	directions = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
	ans = 10 ** 20
	for i in range(6):
		(dx1, dy1) = directions[i]
		(dx2, dy2) = directions[(i + 1) % 6]
		a = (x * dy2 - y * dx2) // (dx1 * dy2 - dy1 * dx2)
		b = (x * dy1 - y * dx1) // (dx2 * dy1 - dy2 * dx1)
		if a < 0 or b < 0:
			continue
		ans = min(ans, clst[i] * a + clst[(i + 1) % 6] * b)
	print(ans)
for _ in range(int(input())):
	main()


import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
import heapq, collections

def getSurroundings(i, j, currCost):
	return [(i + 1, j + 1, currCost + c1), (i, j + 1, currCost + c2), (i - 1, j, currCost + c3), (i - 1, j - 1, currCost + c4), (i, j - 1, currCost + c5), (i + 1, j, currCost + c6)]
allAns = []
t = int(input())
for _ in range(t):
	(x, y) = [int(z) for z in input().split()]
	(c1, c2, c3, c4, c5, c6) = [int(z) for z in input().split()]
	minDist = collections.defaultdict(lambda : -1)
	queue = [(0, (0, 0))]
	coverCnts = 0
	while coverCnts < 6:
		(cost, (i, j)) = heapq.heappop(queue)
		if minDist[i, j] == -1:
			minDist[i, j] = cost
			if (i, j) in {(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)}:
				coverCnts += 1
			for (iNext, jNext, costNext) in getSurroundings(i, j, cost):
				if minDist[iNext, jNext] == -1 and abs(iNext) <= 3 and (abs(jNext) <= 3):
					heapq.heappush(queue, (costNext, (iNext, jNext)))
	(c1, c2, c3, c4, c5, c6) = [minDist[1, 1], minDist[0, 1], minDist[-1, 0], minDist[-1, -1], minDist[0, -1], minDist[1, 0]]
	ans = float('inf')
	t = y
	s = x - t
	ans2 = 0
	if t >= 0:
		ans2 += c1 * abs(t)
	else:
		ans2 += c4 * abs(t)
	if s >= 0:
		ans2 += c6 * abs(s)
	else:
		ans2 += c3 * abs(s)
	ans = min(ans, ans2)
	s = x
	t = y
	ans2 = 0
	if s >= 0:
		ans2 += c6 * abs(s)
	else:
		ans2 += c3 * abs(s)
	if t >= 0:
		ans2 += c2 * abs(t)
	else:
		ans2 += c5 * abs(t)
	ans = min(ans, ans2)
	t = x
	s = y - t
	ans2 = 0
	if s >= 0:
		ans2 += c2 * abs(s)
	else:
		ans2 += c5 * abs(s)
	if t >= 0:
		ans2 += c1 * abs(t)
	else:
		ans2 += c4 * abs(t)
	ans = min(ans, ans2)
	allAns.append(ans)
multiLineArrayPrint(allAns)


import sys as _sys

def main():
	t = int(input())
	for i_t in range(t):
		(x, y) = _read_ints()
		cs = tuple(_read_ints())
		result = find_min_path_cost((x, y), cs)
		print(result)

def _read_line():
	result = _sys.stdin.readline()
	assert result[-1] == '\n'
	return result[:-1]

def _read_ints():
	return map(int, _read_line().split())

def find_min_path_cost(pos, directions_costs):
	(c1, c2, c3, c4, c5, c6) = directions_costs
	(c_0p, c_pp, c_p0, c_0m, c_mm, c_m0) = (c2, c1, c6, c5, c4, c3)
	get_cs = lambda : (c_0p, c_pp, c_p0, c_0m, c_mm, c_m0)
	while True:
		old_cs = get_cs()
		c_0p = min(c_0p, c_pp + c_m0)
		c_p0 = min(c_p0, c_pp + c_0m)
		c_pp = min(c_pp, c_0p + c_p0)
		c_0m = min(c_0m, c_mm + c_p0)
		c_m0 = min(c_m0, c_mm + c_0p)
		c_mm = min(c_mm, c_0m + c_m0)
		if get_cs() == old_cs:
			break
	(x, y) = pos
	result = 0
	if x > 0 and y > 0:
		operation_pp_n = min(x, y)
		result += c_pp * operation_pp_n
		x -= operation_pp_n
		y -= operation_pp_n
	if x < 0 and y < 0:
		operation_mm_n = min(abs(x), abs(y))
		result += c_mm * operation_mm_n
		x += operation_mm_n
		y += operation_mm_n
	if x > 0:
		result += abs(x) * c_p0
	elif x < 0:
		result += abs(x) * c_m0
	if y > 0:
		result += abs(y) * c_0p
	elif y < 0:
		result += abs(y) * c_0m
	return result
main()


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	a1 = min(c1, c6 + c2)
	a2 = min(c2, c1 + c3)
	a3 = min(c3, c4 + c2)
	a4 = min(c4, c3 + c5)
	a5 = min(c5, c4 + c6)
	a6 = min(c6, c5 + c1)
	c1 = a1
	c2 = a2
	c3 = a3
	c4 = a4
	c5 = a5
	c6 = a6
	k = 0
	if x >= 0 and y >= 0:
		r = min(x, y)
		k += c1 * r
		x -= r
		y -= r
	if x <= 0 and y <= 0:
		r = max(x, y)
		x -= r
		y -= r
		r *= -1
		k += c4 * r
	if y >= 0:
		k += c2 * y
	else:
		y *= -1
		k += c5 * y
	if x >= 0:
		k += c6 * x
	else:
		x *= -1
		k += c3 * x
	print(k)


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
cases = int(input())
for t in range(cases):
	(x, y) = list(map(int, input().split()))
	c = list(map(int, input().split()))
	east = min(c[1], c[0] + c[2])
	west = min(c[4], c[3] + c[5])
	neast = min(c[0], c[1] + c[5])
	nwest = min(c[5], c[0] + c[4])
	seast = min(c[2], c[1] + c[3])
	swest = min(c[3], c[2] + c[4])
	out1 = 0
	if x >= 0:
		out1 += abs(x) * nwest
	else:
		out1 += abs(x) * seast
	if y >= 0:
		out1 += abs(y) * east
	else:
		out1 += abs(y) * west
	out2 = 0
	if x >= 0:
		out2 += abs(x) * neast
	else:
		out2 += abs(x) * swest
	if y >= x:
		out2 += abs(y - x) * east
	else:
		out2 += abs(y - x) * west
	out3 = 0
	if y >= 0:
		out3 += abs(y) * neast
	else:
		out3 += abs(y) * swest
	if y >= x:
		out3 += abs(y - x) * seast
	else:
		out3 += abs(y - x) * nwest
	print(min(out1, out2, out3))


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	arr = [0] * 7
	cost = 0
	if x >= 0 and y >= 0:
		arr[1] = min(x, y)
		x -= arr[1]
		y -= arr[1]
		cost += arr[1] * min(c2 + c6, c1)
		if x:
			arr[6] = x
			cost += arr[6] * min(c1 + c5, c6)
		if y:
			arr[2] = y
			cost += arr[2] * min(c1 + c3, c2)
	elif x >= 0 and y <= 0:
		arr[6] = x
		cost += arr[6] * min(c6, c5 + c1)
		arr[5] = -y
		cost += arr[5] * min(c4 + c6, c5)
	elif x <= 0 and y >= 0:
		arr[2] = y
		cost += arr[2] * min(c1 + c3, c2)
		arr[3] = -x
		cost += arr[3] * min(c2 + c4, c3)
	elif x < 0 and y < 0:
		arr[4] = min(-x, -y)
		x += arr[4]
		y += arr[4]
		cost += arr[4] * min(c3 + c5, c4)
		if x:
			arr[3] = -x
			cost += arr[3] * min(c2 + c4, c3)
		if y:
			arr[5] = -y
			cost += arr[5] * min(c4 + c6, c5)
	print(cost)


t = int(input())
while t:
	t -= 1
	(x, y) = list(map(int, input().strip().split()))
	(c1, c2, c3, c4, c5, c6) = list(map(int, input().strip().split()))
	(t1, t2, t3, t4, t5, t6) = (c1, c2, c3, c4, c5, c6)
	c1 = min(c1, t2 + t6)
	c2 = min(c2, t1 + t3)
	c3 = min(c3, t2 + t4)
	c4 = min(c4, t3 + t5)
	c5 = min(c5, t4 + t6)
	c6 = min(c6, t5 + t1)
	ans = 0
	if x >= 0 and y >= 0:
		tmp = min(x, y)
		ans += tmp * c1
		x -= tmp
		y -= tmp
	elif x < 0 and y < 0:
		tmp = min(-x, -y)
		ans += tmp * c4
		x += tmp
		y += tmp
	if x < 0:
		ans += -x * c3
	elif x > 0:
		ans += x * c6
	if y < 0:
		ans += -y * c5
	elif y > 0:
		ans += y * c2
	print(ans)


from sys import stdin
input = stdin.readline

def solve():
	(y, x) = map(int, input().split())
	c = list(map(int, input().split()))
	res = float('inf')
	r1 = abs(y) * c[0 if y >= 0 else 3] + abs(x - y) * c[1 if y <= x else 4]
	r2 = abs(y) * c[5 if y >= 0 else 2] + abs(x) * c[1 if x >= 0 else 4]
	r3 = abs(x) * c[0 if x >= 0 else 3] + abs(x - y) * c[5 if x <= y else 2]
	print(min(r1, r2, r3))
t = int(input())
for _ in range(t):
	solve()


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.buffer.readline())

def MI():
	return map(int, sys.stdin.buffer.readline().split())

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def BI():
	return sys.stdin.buffer.readline().rstrip()

def SI():
	return sys.stdin.buffer.readline().rstrip().decode()
inf = 10 ** 20
for _ in range(II()):
	(x, y) = MI()
	cc = LI()

	def move(x, y, d):
		cost = 0
		if d > 0:
			cost += cc[0] * d
		elif d < 0:
			cost += -cc[3] * d
		x -= d
		y -= d
		return (x, y, cost)

	def to0(x, y):
		cost = 0
		if x > 0:
			cost += cc[5] * x
		elif x < 0:
			cost += cc[2] * -x
		if y > 0:
			cost += cc[1] * y
		elif y < 0:
			cost += cc[4] * -y
		return cost
	ans = inf
	(x1, y1, cur) = move(x, y, x)
	cur += to0(x1, y1)
	ans = min(ans, cur)
	(x1, y1, cur) = move(x, y, y)
	cur += to0(x1, y1)
	ans = min(ans, cur)
	cur = to0(x, y)
	ans = min(ans, cur)
	print(ans)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def NLI(n):
	return [[int(i) for i in input().split()] for i in range(n)]

def NLI_(n):
	return [[int(i) - 1 for i in input().split()] for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def RLI(n=8, a=1, b=10):
	return [random.randint(a, b) for i in range(n)]

def RI(a=1, b=10):
	return random.randint(a, b)

def INP():
	N = 10
	A = 10
	n = random.randint(1, N)
	a = [random.randint(1, A) for i in range(n)]
	return (n, a)

def Rtest(T):
	(case, err) = (0, 0)
	for i in range(T):
		inp = INP()
		a1 = naive(*inp)
		a2 = solve(*inp)
		if a1 != a2:
			print((a1, a2), inp)
			err += 1
		case += 1
	print('Tested', case, 'case with', err, 'errors')

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()
show_flg = False
show_flg = True
ans = 0

def t(c, x):
	n = len(x)
for _ in range(I()):
	ans = inf
	(h, w) = LI()
	g = LI()
	H = (1, 0, -1, -1, 0, 1)
	W = (1, 1, 0, -1, -1, 0)
	for i in range(6):
		for j in range(i + 1, 6):
			(a, c) = (H[i], W[i])
			(b, d) = (H[j], W[j])
			dlt = a * d - b * c
			(s, t) = (d * h - b * w, -h * c + a * w)
			if dlt == 0 or s % dlt != 0 or t % dlt != 0:
				continue
			s *= dlt
			t *= dlt
			if s < 0 or t < 0:
				continue
			ans = min(ans, s * g[i] + t * g[j])
	print(ans)


def getDistances(x, y, dx, dy):
	res = []
	if dx == 0:
		if x == 0:
			res += [0]
	else:
		cnt = x // -dx
		if cnt >= 0:
			res += [cnt]
	if dy == 0:
		if y == 0:
			res += [0]
	else:
		cnt = y // -dy
		if cnt >= 0:
			res += [cnt]
	if dx == dy:
		if x == y:
			res += [0]
	else:
		a = x - y
		b = dx - dy
		cnt = a // -b
		if cnt >= 0:
			res += [cnt]
	return res
tt = int(input())
for _ in range(tt):
	(x, y) = [int(x) for x in input().strip().split()]
	(c1, c2, c3, c4, c5, c6) = [int(x) for x in input().strip().split()]
	x = -x
	y = -y
	c = [(c1, (1, 1)), (c2, (0, 1)), (c3, (-1, 0)), (c4, (-1, -1)), (c5, (0, -1)), (c6, (1, 0))]
	res = 1e+47
	for a in c:
		dx = a[1][0]
		dy = a[1][1]
		ds = getDistances(x, y, dx, dy)
		for d in ds:
			xx = x + d * dx
			yy = y + d * dy
			add = 1e+47
			if xx == 0 and yy >= 0:
				add = c5 * yy
			elif xx == 0 and yy < 0:
				add = c2 * -yy
			elif yy == 0 and xx >= 0:
				add = c3 * xx
			elif yy == 0 and xx < 0:
				add = c6 * -xx
			elif xx == yy and xx >= 0:
				add = c4 * xx
			elif xx == yy and xx < 0:
				add = c1 * -xx
			else:
				print(a, d, xx, yy)
				raise Exception()
			cur = d * a[0] + add
			res = min(res, cur)
	print(res)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
t = int(input())
for ti in range(t):
	(X, Y) = map(int, input().split())
	li = [int(i) for i in input().split()]
	mv = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
	for jj in range(10):
		for (p, i) in enumerate(li):
			li[p] = min(li[(p - 1) % 6] + li[(p + 1) % 6], i)
	ans = 0
	if X > 0 and Y > 0:
		d = min(X, Y)
		ans += d * li[0]
		X -= d
		Y -= d
	elif X < 0 and Y < 0:
		d = max(X, Y)
		ans += -d * li[3]
		X -= d
		Y -= d
	if X > 0:
		ans += X * li[5]
	else:
		ans += -X * li[2]
	if Y > 0:
		ans += Y * li[1]
	else:
		ans += -Y * li[4]
	print(ans)


import heapq

def dijkstra(g: list, start, goal=None):
	num = len(g)
	dist = [float('inf') for i in range(num)]
	dist[start] = 0
	q = []
	heapq.heappush(q, (0, start))
	while q:
		(prov_cost, src) = heapq.heappop(q)
		if dist[src] < prov_cost:
			continue
		for (node, cost) in g[src]:
			if dist[node] > dist[src] + cost:
				dist[node] = dist[src] + cost
				heapq.heappush(q, (dist[node], node))
	return dist
dxdy = ((1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0))
INF = 3 * 10 ** 18
t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	g = [[] for _ in range(25)]
	for i in range(5):
		for j in range(5):
			for k in range(6):
				(dx, dy) = dxdy[k]
				if 0 <= i + dx < 5 and 0 <= j + dy < 5:
					g[i * 5 + j].append(((i + dx) * 5 + j + dy, c[k]))
	dist = dijkstra(g, 12)
	for i in range(6):
		(dx, dy) = dxdy[i]
		c[i] = dist[12 + dx * 5 + dy]
	ans = 0
	if x > 0 and y > 0:
		m = min(x, y)
		ans += m * c[0]
		x -= m
		y -= m
	elif x < 0 and y < 0:
		m = max(x, y)
		ans -= m * c[3]
		x -= m
		y -= m
	if x > 0:
		ans += x * c[5]
	else:
		ans -= x * c[2]
	if y > 0:
		ans += y * c[1]
	else:
		ans -= y * c[4]
	print(ans)


from bisect import *
from collections import *
from math import *
from heapq import *
from typing import List
from itertools import *
from operator import *
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

@lru_cache(None)
def fact(x):
	if x < 2:
		return 1
	return fact(x - 1) * x

@lru_cache(None)
def per(i, j):
	return fact(i) // fact(i - j)

@lru_cache(None)
def com(i, j):
	return per(i, j) // fact(j)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class smt:

	def __init__(self, l, r, arr):
		self.l = l
		self.r = r
		self.value = (1 << 31) - 1 if l < r else arr[l]
		mid = (l + r) // 2
		if l < r:
			self.left = smt(l, mid, arr)
			self.right = smt(mid + 1, r, arr)
			self.value &= self.left.value & self.right.value

	def setvalue(self, x, val):
		if self.l == self.r:
			self.value = val
			return
		mid = (self.l + self.r) // 2
		if x <= mid:
			self.left.setvalue(x, val)
		else:
			self.right.setvalue(x, val)
		self.value = self.left.value & self.right.value

	def ask(self, l, r):
		if l <= self.l and r >= self.r:
			return self.value
		val = (1 << 31) - 1
		mid = (self.l + self.r) // 2
		if l <= mid:
			val &= self.left.ask(l, r)
		if r > mid:
			val &= self.right.ask(l, r)
		return val

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d
t = N()
for i in range(t):
	(x, y) = RL()
	c = RLL()
	if x >= 0 and y >= 0:
		if x >= y:
			ans = min(c[5] * x + c[1] * y, y * c[0] + (x - y) * c[5], x * c[0] + (x - y) * c[4])
		else:
			ans = min(c[5] * x + c[1] * y, x * c[0] + (-x + y) * c[1], y * c[0] + (-x + y) * c[2])
	elif x <= 0 and y <= 0:
		(x, y) = (-x, -y)
		c = [c[(i + 3) % 6] for i in range(6)]
		if x >= y:
			ans = min(c[5] * x + c[1] * y, y * c[0] + (x - y) * c[5], x * c[0] + (x - y) * c[4])
		else:
			ans = min(c[5] * x + c[1] * y, x * c[0] + (-x + y) * c[1], y * c[0] + (-x + y) * c[2])
	elif x >= 0 and y <= 0:
		y *= -1
		ans = min(x * c[5] + y * c[4], (x + y) * c[4] + x * c[0], (x + y) * c[5] + y * c[3])
	else:
		x *= -1
		ans = min(c[2] * x + c[1] * y, c[1] * (x + y) + x * c[3], c[2] * (x + y) + c[0] * y)
	print(ans)


import sys
from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	(X, Y) = map(int, stdin.readline().split())
	c = [None] + list(map(int, stdin.readline().split()))
	ans = 0
	(x, y) = (X, Y)
	if x >= 0:
		ans += x * c[6]
	else:
		ans += abs(x) * c[3]
	if y >= 0:
		ans += y * c[2]
	else:
		ans += abs(y) * c[5]
	(x, y) = (X, Y)
	tmp = 0
	if x >= 0:
		tmp += c[1] * x
		y -= x
	else:
		tmp += c[4] * abs(x)
		y -= x
	if y >= 0:
		tmp += y * c[2]
	else:
		tmp += abs(y) * c[5]
	ans = min(ans, tmp)
	(x, y) = (X, Y)
	tmp = 0
	if y >= 0:
		tmp += c[1] * y
		x -= y
	else:
		tmp += c[4] * abs(y)
		x -= y
	if x >= 0:
		tmp += x * c[6]
	else:
		tmp += abs(x) * c[3]
	ans = min(ans, tmp)
	print(ans)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):
	print(stack)

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24

def solve():
	(X, Y) = sep()
	(c1, c2, c3, c4, c5, c6) = sep()
	u = min(c2, c1 + c3)
	d = min(c5, c4 + c6)
	r = min(c6, c1 + c5)
	l = min(c3, c4 + c2)
	ru = min(r + u, c1)
	rd = r + d
	lu = l + u
	ld = min(l + d, c4)
	dx = X
	dy = Y
	ans = 0
	if dx >= 0 and dy >= 0:
		t1 = min(dy, dx)
		ans += t1 * ru
		t2 = abs(dx - dy)
		if dx >= dy:
			ans += r * t2
		else:
			ans += u * t2
		print(ans)
		return
	if dx <= 0 and dy >= 0:
		(dx, dy) = (abs(dx), abs(dy))
		t1 = min(dy, dx)
		ans += t1 * lu
		t2 = abs(dx - dy)
		if dx >= dy:
			ans += l * t2
		else:
			ans += u * t2
		print(ans)
		return
	if dx <= 0 and dy <= 0:
		(dx, dy) = (abs(dx), abs(dy))
		t1 = min(dy, dx)
		ans += t1 * ld
		t2 = abs(dx - dy)
		if dx >= dy:
			ans += l * t2
		else:
			ans += d * t2
		print(ans)
		return
	if dx >= 0 and dy <= 0:
		(dx, dy) = (abs(dx), abs(dy))
		t1 = min(dy, dx)
		ans += t1 * rd
		t2 = abs(dx - dy)
		if dx >= dy:
			ans += r * t2
		else:
			ans += d * t2
		print(ans)
		return
testcase(int(inp()))


t = int(input())
for _ in range(t):
	(x, y) = list(map(int, input().split()))
	(c1, c2, c3, c4, c5, c6) = list(map(int, input().split()))
	a1 = min(c1, c6 + c2)
	a2 = min(c2, c1 + c3)
	a3 = min(c3, c2 + c4)
	a4 = min(c4, c3 + c5)
	a5 = min(c5, c4 + c6)
	a6 = min(c6, c5 + c1)
	if x > 0 and y > 0:
		if x > y:
			ans = a1 * y + a6 * (x - y)
		else:
			ans = a1 * x + a2 * (y - x)
	elif x >= 0 and y <= 0:
		ans = a5 * -y + a6 * x
	elif x <= 0 and y >= 0:
		ans = a2 * y + a3 * -x
	else:
		x *= -1
		y *= -1
		if x > y:
			ans = a4 * y + a3 * (x - y)
		else:
			ans = a4 * x + a5 * (y - x)
	print(ans)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(x, y) = I()
	l = I()
	a = min(l[2], l[3] + l[1])
	b = min(l[5], l[4] + l[0])
	c = min(l[4], l[3] + l[5])
	d = min(l[1], l[0] + l[2])
	e = min(l[1] + l[5], l[0])
	f = min(l[2] + l[4], l[3])
	pk = 0
	if x > 0 and y > 0:
		pk = min(x, y)
		x -= pk
		y -= pk
		pk *= e
	elif x < 0 and y < 0:
		pk = max(x, y)
		x -= pk
		y -= pk
		pk *= -f
	an = pk
	if x < 0:
		an += -x * a
	else:
		an += x * b
	if y < 0:
		an += -y * c
	else:
		an += y * d
	print(an)


for _ in range(int(input())):
	(x, y) = [int(i) for i in input().split()]
	z = [0] + [int(i) for i in input().split()]
	k1 = abs(x) * z[3 if x < 0 else 6] + abs(y) * z[5 if y < 0 else 2]
	k2 = abs(y) * z[4 if y < 0 else 1] + abs(x - y) * z[3 if x - y < 0 else 6]
	k3 = abs(x) * z[4 if x < 0 else 1] + abs(y - x) * z[5 if y - x < 0 else 2]
	print(min((k1, k2, k3)))


import sys
input = sys.stdin.readline
for f in range(int(input())):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	c2 = min(c2, c1 + c3)
	c5 = min(c5, c4 + c6)
	c6 = min(c6, c1 + c5)
	c3 = min(c3, c2 + c4)
	c1 = min(c1, c2 + c6)
	c4 = min(c4, c3 + c5)
	if x * y > 0:
		if x > 0:
			d = min(x, y)
			x -= d
			y -= d
			print(x * c6 + y * c2 + d * c1)
		else:
			x = -x
			y = -y
			d = min(x, y)
			x -= d
			y -= d
			print(x * c3 + y * c5 + d * c4)
	else:
		cost = 0
		if x > 0:
			cost += x * c6
		else:
			cost -= x * c3
		if y > 0:
			cost += y * c2
		else:
			cost -= y * c5
		print(cost)


def find_shortest_route(c1, c2, c3, c4, c5, c6, x, y):
	costs = []
	n1 = y
	n2 = x - n1
	if n1 >= 0 and n2 >= 0:
		costs.append(c1 * n1 + c2 * n2)
	n1 = x
	n3 = n1 - y
	if n1 >= 0 and n3 >= 0:
		costs.append(c1 * n1 + c3 * n3)
	n1 = y
	n5 = n1 - x
	if n1 >= 0 and n5 >= 0:
		costs.append(c1 * n1 + c5 * n5)
	n1 = x
	n6 = y - n1
	if n1 >= 0 and n6 >= 0:
		costs.append(c1 * n1 + c6 * n6)
	n2 = x
	n3 = -y
	if n2 >= 0 and n3 >= 0:
		costs.append(c2 * n2 + c3 * n3)
	n4 = -y
	n2 = x + n4
	if n2 >= 0 and n4 >= 0:
		costs.append(c2 * n2 + c4 * n4)
	n2 = x
	n6 = y
	if n2 >= 0 and n6 >= 0:
		costs.append(c2 * n2 + c6 * n6)
	n4 = -x
	n3 = -y - n4
	if n3 >= 0 and n4 >= 0:
		costs.append(c3 * n3 + c4 * n4)
	n5 = -x
	n3 = -y
	if n3 >= 0 and n5 >= 0:
		costs.append(c3 * n3 + c5 * n5)
	n4 = -y
	n5 = -x - n4
	if n4 >= 0 and n5 >= 0:
		costs.append(c4 * n4 + c5 * n5)
	n4 = -x
	n6 = y + n4
	if n4 >= 0 and n6 >= 0:
		costs.append(c4 * n4 + c6 * n6)
	n5 = -x
	n6 = y
	if n5 >= 0 and n6 >= 0:
		costs.append(c5 * n5 + c6 * n6)
	print(min(*costs))
n_t = int(input())
for t in range(n_t):
	(y, x) = map(int, input().split(' '))
	(c1, c2, c3, c4, c5, c6) = map(int, input().split(' '))
	find_shortest_route(c1, c2, c3, c4, c5, c6, x, y)


import time, math as mt, bisect, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return

def readTree(v):
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return adj

def red(c, a):
	if a >= 0:
		return c[1] * a
	return -c[4] * a

def blue(c, a):
	if a >= 0:
		return c[0] * a
	return -c[3] * a

def green(c, a):
	if a >= 0:
		return c[5] * a
	return -c[2] * a

def solve():
	(x, y) = IP()
	c = L()
	blue_red = blue(c, x) + red(c, y - x)
	green_red = green(c, x) + red(c, y)
	green_blue = green(c, x - y) + blue(c, y)
	print(min(blue_red, green_red, green_blue))
	return
t = II()
for i in range(t):
	solve()


def func(x, c2, c5):
	if x < 0:
		return -x * c5
	else:
		return x * c2

def func_2(x, c4, c1):
	if x < 0:
		return -x * c4
	else:
		return x * c1

def func_3(x, c6, c3):
	if x < 0:
		return -x * c3
	else:
		return x * c6

def main():
	(y, x) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	lu = (1, 0)
	ru = (1, 1)
	rd = (-1, 0)
	r = (0, 1)
	l = (1, 0)
	ld = (-1, -1)
	a = func_3(y, c6, c3) + func(x, c2, c5)
	b = func_2(y, c4, c1) + func(x - y, c2, c5)
	c = func_2(x, c4, c1) + func_3(y - x, c6, c3)
	print(min(a, b, c))

def __starting_point():
	t = int(input())
	for i in range(t):
		main()
__starting_point()


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())
masks = []
dirs = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
for i in range(1 << 6):
	bad = False
	for j in range(3):
		if (i >> j & 1) + (i >> j + 3 & 1) != 1:
			bad = True
	if not bad:
		if i & 1 != 0:
			i1 = 0
		else:
			i1 = 3
		if i & 2 != 0:
			i2 = 1
		else:
			i2 = 1 + 3
		if i & 4 != 0:
			i3 = 2
		else:
			i3 = 2 + 3
		masks.append((i, dirs[i1], dirs[i2], dirs[i3], i1, i2, i3))

def solve():
	(x, y) = mints()
	c = list(mints())
	best = int(1e+30)
	for (i, v1, v2, v3, i1, i2, i3) in masks:
		d1 = v2[0] * v3[1] - v2[1] * v3[0]
		(tx, ty) = (x, y)
		k1 = (tx * v3[1] - ty * v3[0]) // d1
		k2 = (ty * v2[0] - tx * v2[1]) // d1
		(tx, ty) = (x - v1[0], y - v1[1])
		k1v = (tx * v3[1] - ty * v3[0]) // d1
		k2v = (ty * v2[0] - tx * v2[1]) // d1
		dk1 = k1v - k1
		dk2 = k2v - k2
		ck1 = -k1 // dk1
		ck2 = -k2 // dk2
		if dk1 > 0:
			mk1 = ck1
			Mk1 = int(2e+20)
		else:
			mk1 = int(0)
			Mk1 = ck1
		if dk2 > 0:
			mk2 = ck2
			Mk2 = int(2e+20)
		else:
			mk2 = int(0)
			Mk2 = ck2
		m = max(max(mk1, mk2), 0)
		M = min(Mk1, Mk2)
		if m <= M:
			(tx, ty) = (x - v1[0] * m, y - v1[1] * m)
			k1 = (tx * v3[1] - ty * v3[0]) // d1
			k2 = (ty * v2[0] - tx * v2[1]) // d1
			best = min(best, m * c[i1] + k1 * c[i2] + k2 * c[i3])
			(tx, ty) = (x - v1[0] * M, y - v1[1] * M)
			k1 = (tx * v3[1] - ty * v3[0]) // d1
			k2 = (ty * v2[0] - tx * v2[1]) // d1
			best = min(best, M * c[i1] + k1 * c[i2] + k2 * c[i3])
	print(best)
for i in range(mint()):
	solve()


def ans2(x, y, cx, cy, cxy):
	ret = 0
	xy = min(x, y)
	x -= xy
	y -= xy
	ret += cxy * xy
	ret += x * cx
	ret += y * cy
	return ret

def rotate(x, y):
	return (y, y - x)

def ans(x, y, costs):
	for _ in range(100):
		costs[0] = min(costs[0], costs[1] + costs[5])
		costs[1] = min(costs[1], costs[2] + costs[0])
		costs[2] = min(costs[2], costs[3] + costs[1])
		costs[3] = min(costs[3], costs[4] + costs[2])
		costs[4] = min(costs[4], costs[5] + costs[3])
		costs[5] = min(costs[5], costs[0] + costs[4])
	while not (x >= 0 and y >= 0):
		(x, y) = rotate(x, y)
		costs = costs[1:] + [costs[0]]
	return ans2(x, y, costs[5], costs[1], costs[0])
for _ in range(int(input())):
	(x, y) = input().split()
	x = int(x)
	y = int(y)
	costs = input().split()
	costs = list(map(int, costs))
	print(ans(x, y, costs))


n = int(input())
from math import inf as inf
for _ in range(n):
	(x, y) = map(int, input().split())
	costs = list(map(int, input().split()))
	movements = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
	min = inf
	for i in range(5):
		for j in range(i + 1, 6):
			(v1, v2) = movements[i]
			(w1, w2) = movements[j]
			if v2 * w1 - v1 * w2 != 0:
				steps1 = (x * w2 - y * w1) // (v1 * w2 - v2 * w1)
				steps2 = (y * v1 - x * v2) // (v1 * w2 - v2 * w1)
				if steps1 >= 0 and steps2 >= 0:
					if steps1 * costs[i] + steps2 * costs[j] < min:
						min = steps1 * costs[i] + steps2 * costs[j]
	print(min)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	d1 = min(c1, c2 + c6)
	d2 = min(c2, c1 + c3)
	d3 = min(c3, c2 + c4)
	d4 = min(c4, c3 + c5)
	d5 = min(c5, c4 + c6)
	d6 = min(c6, c5 + c1)
	if x >= 0 and y >= 0:
		a1 = x * d6 + y * d2
		a2 = x * d1 + (y - x) * d2 if y - x >= 0 else x * d1 + (x - y) * d5
		a3 = (x - y) * d6 + y * d1 if x - y >= 0 else (y - x) * d3 + y * d1
		print(min(a1, a2, a3))
	elif x >= 0 and y < 0:
		y = -y
		a1 = x * d6 + y * d5
		print(a1)
	elif x < 0 and y >= 0:
		x = -x
		a1 = x * d3 + y * d2
		print(a1)
	else:
		x = -x
		y = -y
		a1 = x * d3 + y * d5
		a2 = x * d4 + (y - x) * d5 if y - x >= 0 else x * d4 + (x - y) * d2
		a3 = (x - y) * d3 + y * d4 if x - y >= 0 else (y - x) * d6 + y * d4
		print(min(a1, a2, a3))


def det(x, y, z, w):
	return x * w - y * z
t = int(input())
for test in range(t):
	[x, y] = list(map(int, input().rstrip().split()))
	c = list(map(int, input().rstrip().split()))
	for j in range(6):
		for i in range(6):
			c[i] = min(c[i], c[(i - 1) % 6] + c[(i + 1) % 6])
	direction = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
	cost = float('inf')
	for i in range(6):
		for j in range(i + 1, 6):
			d = direction[i]
			e = direction[j]
			dete = det(d[0], e[0], d[1], e[1])
			if dete != 0:
				a = det(x, e[0], y, e[1]) // dete
				b = det(d[0], x, d[1], y) // dete
				if a * d[0] + b * e[0] == x and a * d[1] + b * e[1] == y and (a > 0) and (b > 0):
					cost = min(cost, a * c[i] + b * c[j])
	if x == 0:
		if y > 0:
			cost = min(cost, y * c[1])
		elif y == 0:
			cost = 0
		else:
			cost = min(cost, -y * c[4])
	elif y == x:
		if y > 0:
			cost = min(cost, y * c[0])
		else:
			cost = min(cost, -y * c[3])
	elif y == 0:
		if x > 0:
			cost = min(cost, x * c[5])
		else:
			cost = min(cost, -x * c[2])
	print(cost)


for t in range(int(input())):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())

	def cal(x, y):
		if x == 0 and y == 0:
			return 0
		elif x > 0 and y == 0:
			return min(c1 + c5, c6) * x
		elif x < 0 and y == 0:
			return min(c2 + c4, c3) * x * -1
		elif x == 0 and y > 0:
			return min(c1 + c3, c2) * y
		elif x == 0 and y < 0:
			return min(c4 + c6, c5) * y * -1
		elif x == y and x > 0:
			return min(c2 + c6, c1) * x
		elif x == y and x < 0:
			return min(c3 + c5, c4) * x * -1
		else:
			temp = cal(x, 0) + cal(0, y)
			if abs(x) > abs(y):
				diff = abs(x) - abs(y)
				if x < 0:
					diff *= -1
				temp = min(temp, cal(diff, 0) + cal(x - diff, y))
			elif abs(y) > abs(x):
				diff = abs(y) - abs(x)
				if y < 0:
					diff *= -1
				temp = min(temp, cal(0, diff) + cal(x, y - diff))
			return temp
	print(cal(x, y))


n = int(input())
for i in range(n):
	(x, y) = list(map(int, input().split()))
	c = [0] + list(map(int, input().split()))
	p1 = abs(x) * c[3 if x < 0 else 6] + abs(y) * c[5 if y < 0 else 2]
	p2 = abs(y) * c[4 if y < 0 else 1] + abs(x - y) * c[3 if x - y < 0 else 6]
	p3 = abs(x) * c[4 if x < 0 else 1] + abs(y - x) * c[5 if y - x < 0 else 2]
	print(min((p1, p2, p3)))


cases = int(input())
for t in range(cases):
	(x, y) = list(map(int, input().split()))
	c = list(map(int, input().split()))
	east = min(c[1], c[0] + c[2])
	west = min(c[4], c[3] + c[5])
	neast = min(c[0], c[1] + c[5])
	nwest = min(c[5], c[0] + c[4])
	seast = min(c[2], c[1] + c[3])
	swest = min(c[3], c[2] + c[4])
	out1 = 0
	if x >= 0:
		out1 += abs(x) * nwest
	else:
		out1 += abs(x) * seast
	if y >= 0:
		out1 += abs(y) * east
	else:
		out1 += abs(y) * west
	out2 = 0
	if x >= 0:
		out2 += abs(x) * neast
	else:
		out2 += abs(x) * swest
	if y >= x:
		out2 += abs(y - x) * east
	else:
		out2 += abs(y - x) * west
	out3 = 0
	if y >= 0:
		out3 += abs(y) * neast
	else:
		out3 += abs(y) * swest
	if y >= x:
		out3 += abs(y - x) * seast
	else:
		out3 += abs(y - x) * nwest
	print(min(out1, out2, out3))


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import time
start_time = time.time()
import collections as col
import math, string
from functools import reduce

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	(X, Y) = getInts()
	C = [0] + getInts()
	tmp = [0] * 7
	tmp[1] = min(C[1], C[2] + C[6])
	tmp[2] = min(C[2], C[1] + C[3])
	tmp[3] = min(C[3], C[2] + C[4])
	tmp[4] = min(C[4], C[3] + C[5])
	tmp[5] = min(C[5], C[4] + C[6])
	tmp[6] = min(C[6], C[1] + C[5])
	C = tmp[:]
	if X == 0:
		if Y == 0:
			return 0
		if Y < 0:
			return abs(Y) * C[5]
		if Y > 0:
			return Y * C[2]
	if X < 0:
		if Y < X:
			dl = abs(X)
			left = X - Y
			return C[4] * dl + C[5] * left
		if Y > 0:
			dr = abs(X)
			right = Y
			return C[3] * dr + C[2] * right
		dl = abs(Y)
		dr = abs(X) - abs(Y)
		return C[3] * dr + C[4] * dl
	if Y > X:
		dr = abs(X)
		right = Y - X
		return C[1] * dr + C[2] * right
	if Y < 0:
		dl = abs(X)
		left = abs(Y)
		return C[6] * dl + C[5] * left
	dr = abs(Y)
	dl = X - abs(Y)
	return C[1] * dr + C[6] * dl
for _ in range(getInt()):
	print(solve())


import sys
from random import *
pl = 1
from copy import *
if pl:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('outpt.txt', 'w')

def li():
	return [int(xxx) for xxx in input().split()]

def fi():
	return int(input())

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())
t = fi()
while t > 0:
	t -= 1
	tar = li()
	cost = li()
	for j in range(36):
		for i in range(6):
			cost[i] = min(cost[i], cost[i - 1] + cost[(i + 1) % 6])
	ans = 0
	(x, y) = tar
	c = cost
	while [x, y] != [0, 0]:
		if x > 0 and y > 0:
			p = min(x, y)
			ans += cost[0] * p
			x -= p
			y -= p
		elif x < 0 and y < 0:
			p = min(-x, -y)
			ans += cost[3] * p
			x += p
			y += p
		elif x == 0:
			if y > 0:
				ans += y * c[1]
				y = 0
			else:
				ans += -y * c[4]
				y = 0
		elif x > 0:
			ans += x * c[5]
			x = 0
		else:
			ans += -x * c[2]
			x = 0
	print(ans)


for _ in range(int(input())):
	repl = True
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	while repl:
		repl = False
		if c1 + c3 < c2:
			c2 = c1 + c3
			repl = True
		if c2 + c4 < c3:
			c3 = c2 + c4
			repl = True
		if c3 + c5 < c4:
			c4 = c3 + c5
			repl = True
		if c4 + c6 < c5:
			c5 = c4 + c6
			repl = True
		if c5 + c1 < c6:
			c6 = c5 + c1
			repl = True
		if c6 + c2 < c1:
			c1 = c6 + c2
			repl = True
	if x >= 0 and y >= 0:
		pivot = min(x, y)
		print(c1 * pivot + c2 * (y - pivot) + c6 * (x - pivot))
	elif x <= 0 and y <= 0:
		pivot = min(-x, -y)
		print(c4 * pivot + c5 * (-y - pivot) + c3 * (-x - pivot))
	elif x >= 0 and y <= 0:
		print(c6 * x + c5 * -y)
	elif x <= 0 and y >= 0:
		print(c3 * -x + c2 * y)


def optimize_costs(c1, c2, c3, c4, c5, c6):
	c1 = min(c1, c2 + c6)
	c2 = min(c2, c1 + c3)
	c3 = min(c3, c2 + c4)
	c4 = min(c4, c3 + c5)
	c5 = min(c5, c4 + c6)
	c6 = min(c6, c5 + c1)
	return (c1, c2, c3, c4, c5, c6)

def main(input_f):
	t = int(input_f())
	for _ in range(t):
		(x, y) = map(int, input_f().split(' '))
		(c1, c2, c3, c4, c5, c6) = map(int, input_f().split(' '))
		for _ in range(10):
			(c1, c2, c3, c4, c5, c6) = optimize_costs(c1, c2, c3, c4, c5, c6)
		if x < 0 and y < 0:
			if x < y:
				print(-y * c4 + (y - x) * c3)
			else:
				print(-x * c4 + (x - y) * c5)
		elif x < 0 and y >= 0:
			print(-x * c3 + y * c2)
		elif x >= 0 and y < 0:
			print(x * c6 - y * c5)
		elif x < y:
			print(x * c1 + (y - x) * c2)
		else:
			print(y * c1 + (x - y) * c6)
main(input)


import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
	(y, x) = map(int, input().split())
	X = [0] + list(map(int, input().split()))
	if x >= 0 and y >= 0:
		(a, b, c) = (X[2], X[6], X[1])
		a = min(a, c + X[3])
		b = min(b, c + X[5])
	elif x > 0 and y < 0:
		(a, b) = (min(X[1] + X[3], X[2]), min(X[4] + X[2], X[3]))
		c = a + b
		y = -y
	elif x <= 0 and y <= 0:
		(a, b, c) = (X[5], X[3], X[4])
		a = min(a, c + X[6])
		b = min(b, c + X[2])
		(x, y) = (-x, -y)
	elif x < 0 and y > 0:
		(a, b) = (min(X[4] + X[6], X[5]), min(X[1] + X[5], X[6]))
		c = a + b
		x = -x
	c = min(a + b, c)
	R = min(x, y) * c
	if x > y:
		R += (x - y) * a
	else:
		R += (y - x) * b
	print(R)


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=300006, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] > k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
		return pCrawl != None and pCrawl.isEndOfWord

class Node:

	def __init__(self, data):
		self.data = data
		self.count = 0
		self.left = None
		self.right = None

class BinaryTrie:

	def __init__(self):
		self.root = Node(0)

	def insert(self, pre_xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = pre_xor & 1 << i
			if val:
				if not self.temp.right:
					self.temp.right = Node(0)
				self.temp = self.temp.right
				self.temp.count += 1
			if not val:
				if not self.temp.left:
					self.temp.left = Node(0)
				self.temp = self.temp.left
				self.temp.count += 1
		self.temp.data = pre_xor

	def query(self, xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = xor & 1 << i
			if not val:
				if self.temp.left and self.temp.left.count > 0:
					self.temp = self.temp.left
				elif self.temp.right:
					self.temp = self.temp.right
			elif self.temp.right and self.temp.right.count > 0:
				self.temp = self.temp.right
			elif self.temp.left:
				self.temp = self.temp.left
			self.temp.count -= 1
		return xor ^ self.temp.data
for ik in range(int(input())):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	d = defaultdict(int)
	c = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
	d[1, 1] = c1
	d[0, 1] = c2
	d[-1, 0] = c3
	d[-1, -1] = c4
	d[0, -1] = c5
	d[1, 0] = c6
	ans = 0
	for j in range(6):
		for i in range(6):
			for j in range(6):
				if i == j:
					continue
				(r, l) = (c[i][0] + c[j][0], c[i][1] + c[j][1])
				co = d[c[i]] + d[c[j]]
				if d[r, l] == 0:
					d[r, l] = co
				else:
					d[r, l] = min(d[r, l], co)
	if x < 0 and y < 0:
		t = min(abs(x), abs(y))
		ans = t * d[-1, -1]
		x += t
		y += t
		ans += d[-1, 0] * abs(x)
		ans += d[0, -1] * abs(y)
	elif x < 0 and y >= 0:
		t = min(abs(x), abs(y))
		ans = t * d[-1, 1]
		x += t
		y -= t
		ans += d[-1, 0] * abs(x)
		ans += d[0, 1] * abs(y)
	elif x >= 0 and y < 0:
		t = min(abs(x), abs(y))
		ans = t * d[1, -1]
		x -= t
		y += t
		ans += d[1, 0] * abs(x)
		ans += d[0, -1] * abs(y)
	elif x >= 0 and y >= 0:
		t = min(abs(x), abs(y))
		ans = t * d[1, 1]
		x -= t
		y -= t
		ans += d[1, 0] * abs(x)
		ans += d[0, 1] * abs(y)
	print(ans)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	for i in range(6):
		c[i] = min(c[i], c[(i + 1) % 6] + c[(i - 1) % 6])
	if x >= 0:
		if y >= x:
			print(x * c[0] + (y - x) * c[1])
		elif y <= 0:
			print(x * c[5] + abs(y) * c[4])
		else:
			print((x - y) * c[5] + y * c[0])
	elif y <= x:
		print((x - y) * c[4] + abs(x) * c[3])
	elif y >= 0:
		print(abs(x) * c[2] + y * c[1])
	else:
		print(abs(y) * c[3] + (y - x) * c[2])


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	(x, y) = MI()
	a = LI()
	d = [0, 0, 0, 0, 0, 0]
	if x == 0 and y == 0:
		print(0)
		continue
	elif x == 0:
		if y > 0:
			d[1] += y
		else:
			d[4] += abs(y)
	elif y == 0:
		if x > 0:
			d[5] += x
		else:
			d[2] += abs(x)
	elif x > 0:
		if y > 0:
			z = abs(x - y)
			if x > y:
				d[5] += z
			else:
				d[1] += z
			d[0] += min(x, y)
		else:
			d[5] += x
			d[4] += abs(y)
	elif y > 0:
		d[2] += abs(x)
		d[1] += y
	else:
		z = abs(abs(x) - abs(y))
		if x < y:
			d[2] += z
		else:
			d[4] += z
		d[3] += abs(max(x, y))
	old = [0, 0, 0, 0, 0, 0]
	while old != a:
		old = a[:]
		for i in range(6):
			a[i] = min(a[i], a[(i - 1) % 6] + a[(i + 1) % 6])
	ans = 0
	for i in range(6):
		ans += d[i] * a[i]
	print(ans)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	(x, y) = li()
	(c1, c2, c3, c4, c5, c6) = li()
	for i in range(30):
		c1 = min(c1, c2 + c6)
		c6 = min(c6, c1 + c5)
		c5 = min(c5, c6 + c4)
		c4 = min(c4, c5 + c3)
		c2 = min(c2, c1 + c3)
		c3 = min(c3, c2 + c4)
	temp = min(abs(x), abs(y))
	if x >= 0 and y >= 0:
		x -= temp
		y -= temp
		ans = temp * c1 + x * c6 + y * c2
	elif x <= 0 and y >= 0:
		x = abs(x)
		ans = x * c3 + y * c2
	elif x >= 0 and y <= 0:
		y = abs(y)
		ans = x * c6 + y * c5
	else:
		x += temp
		y += temp
		x = abs(x)
		y = abs(y)
		ans = temp * c4 + x * c3 + y * c5
	print(ans)


import sys
input = sys.stdin.readline

def solve_fast(x, y, c):
	ans1 = abs(x) * c[6 if x > 0 else 3] + abs(y) * c[2 if y > 0 else 5]
	ans2 = abs(x) * c[1 if x > 0 else 4] + abs(y - x) * c[2 if y - x > 0 else 5]
	ans3 = abs(y) * c[1 if y > 0 else 4] + abs(y - x) * c[3 if y - x > 0 else 6]
	return min(ans1, ans2, ans3)
T = int(input())
for t in range(T):
	(x, y) = map(int, input().split())
	price = [0] + list(map(int, input().split()))
	best_cost = solve_fast(x, y, price)
	print(best_cost)


def cost(d, c1, c2):
	return d * (c1 if d > 0 else -c2)
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	c = [0] + list(map(int, input().split()))
	opt = []
	opt.append(cost(x - y, c[6], c[3]) + cost(y, c[1], c[4]))
	opt.append(cost(x, c[6], c[3]) + cost(y, c[2], c[5]))
	opt.append(cost(x, c[1], c[4]) + cost(y - x, c[2], c[5]))
	print(min(opt))


t = int(input())

def process():
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	ans = 0
	if x > 0 and y > 0:
		d = min(x, y)
		if c[0] < c[5] + c[1]:
			ans += d * c[0]
			x -= d
			y -= d
	if x < 0 and y < 0:
		d = abs(max(x, y))
		if c[3] < c[4] + c[2]:
			ans += d * c[3]
			x += d
			y += d
	if y > 0:
		if c[1] < c[0] + c[2]:
			ans += abs(y) * c[1]
		else:
			ans += abs(y) * (c[0] + c[2])
	if y < 0:
		if c[4] < c[3] + c[5]:
			ans += abs(y) * c[4]
		else:
			ans += abs(y) * (c[3] + c[5])
	if x < 0:
		if c[2] < c[3] + c[1]:
			ans += abs(x) * c[2]
		else:
			ans += abs(x) * (c[3] + c[1])
	if x > 0:
		if c[5] < c[4] + c[0]:
			ans += abs(x) * c[5]
		else:
			ans += abs(x) * (c[4] + c[0])
	print(ans)
for it in range(t):
	process()


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	for i in range(6):
		c[i] = min(c[i], c[int((i - 1) % 6)] + c[int(i + 1) % 6])
	answer = 0
	path = []
	if x >= 0 and y >= 0:
		distance = min(x, y)
		answer += distance * c[0]
		path.append('C1')
		x -= distance
		y -= distance
	if x <= 0 and y <= 0:
		distance = max(x, y)
		distance *= -1
		x += distance
		y += distance
		answer += distance * c[3]
		path.append('C4')
	if x <= 0:
		x *= -1
		answer += c[2] * x
		path.append('C3')
	else:
		answer += c[5] * x
		path.append('C4')
	if y <= 0:
		y *= -1
		answer += c[4] * y
		path.append('C3')
	else:
		answer += c[1] * y
		path.append('C2')
	print(answer)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	c2 = min(c2, c1 + c3)
	c3 = min(c3, c2 + c4)
	c4 = min(c4, c3 + c5)
	c5 = min(c5, c4 + c6)
	c6 = min(c6, c5 + c1)
	c1 = min(c1, c6 + c2)
	if x * y < 0:
		if x < 0:
			print(c3 * -x + c2 * y)
		else:
			print(c6 * x + c5 * -y)
	elif x + y > 0:
		if x > y:
			print(y * c1 + (x - y) * c6)
		else:
			print(x * c1 + (y - x) * c2)
	elif x > y:
		print(-x * c4 + (x - y) * c5)
	else:
		print(-y * c4 + (y - x) * c3)


inn = lambda : int(input())
inm = lambda : map(int, input().split())
ins = lambda : str(input())
ina = lambda : list(map(int, input().split()))

def solve():
	(x, y) = inm()
	c = ina()
	ans = [0, 0, 0]
	ans[0] = (x * c[5] if x >= 0 else -x * c[2]) + (y * c[1] if y >= 0 else -y * c[4])
	ans[1] = ((x - y) * c[5] if x >= y else (y - x) * c[2]) + (y * c[0] if y >= 0 else -y * c[3])
	ans[2] = ((y - x) * c[1] if y >= x else (x - y) * c[4]) + (x * c[0] if x >= 0 else -x * c[3])
	print(min(ans))

def main():
	t = 1
	t = int(input())
	for _ in range(t):
		solve()
main()


for _ in range(int(input())):
	(y, x) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	cost = 0
	if y == 0:
		if x >= 0:
			cost = min(x * (c3 + c1), x * c2)
		else:
			cost = min(abs(x) * (c4 + c6), abs(x) * c5)
	elif y > 0:
		if x > 0:
			cost1 = c6 * y + c2 * x
			if y >= x:
				cost2 = c1 * x + c6 * (y - x)
				cost3 = c1 * y + c5 * (y - x)
			if y < x:
				cost2 = c1 * y + c2 * (x - y)
				cost3 = c1 * x + c3 * (x - y)
			cost = min(cost1, cost2, cost3)
		elif x == 0:
			cost1 = c6 * y
			cost2 = (c1 + c5) * y
			cost = min(cost1, cost2)
		else:
			x = abs(x)
			cost1 = c6 * y + c5 * x
			cost2 = c4 * x + c6 * (x + y)
			cost3 = c1 * y + c5 * (x + y)
			cost = min(cost1, cost2, cost3)
	else:
		y = abs(y)
		if x < 0:
			x = abs(x)
			cost1 = c5 * x + c3 * y
			if y >= x:
				cost2 = c4 * x + c3 * (y - x)
				cost3 = c4 * y + c2 * (y - x)
			else:
				cost2 = c4 * y + c5 * (x - y)
				cost3 = c4 * x + c6 * (x - y)
			cost = min(cost1, cost2, cost3)
		elif x == 0:
			cost1 = c3 * y
			cost2 = (c4 + c2) * y
			cost = min(cost1, cost2)
		else:
			cost1 = c2 * x + c3 * y
			cost2 = c4 * y + c2 * (x + y)
			cost3 = c1 * x + c3 * (x + y)
			cost = min(cost1, cost2, cost3)
	print(cost)


t = int(input())
for you in range(t):
	l = input().split()
	x = int(l[0])
	y = int(l[1])
	l = input().split()
	c1 = int(l[0])
	c2 = int(l[1])
	c3 = int(l[2])
	c4 = int(l[3])
	c5 = int(l[4])
	c6 = int(l[5])
	if x >= 0 and y >= 0:
		mina = c6 * x + c2 * y
		if y <= x:
			mina = min(mina, c1 * y + c6 * (x - y))
			mina = min(mina, c1 * x + c5 * (x - y))
			print(mina)
		else:
			mina = min(mina, c1 * y + c3 * (y - x))
			mina = min(mina, c1 * x + c2 * (y - x))
			print(mina)
		continue
	if x <= 0 and y <= 0:
		(c1, c2, c3, c4, c5, c6) = (c4, c5, c6, c1, c2, c3)
		x = -x
		y = -y
		mina = c6 * x + c2 * y
		if y <= x:
			mina = min(mina, c1 * y + c6 * (x - y))
			mina = min(mina, c1 * x + c5 * (x - y))
			print(mina)
		else:
			mina = min(mina, c1 * y + c3 * (y - x))
			mina = min(mina, c1 * x + c2 * (y - x))
			print(mina)
		continue
	if x <= 0 and y >= 0:
		x = -x
		mina = c3 * x + c2 * y
		mina = min(mina, c1 * y + c3 * (y + x))
		mina = min(mina, c4 * x + c2 * (y + x))
		print(mina)
		continue
	y = -y
	mina = c6 * x + c5 * y
	mina = min(mina, c1 * x + c5 * (y + x))
	mina = min(mina, c4 * y + c6 * (y + x))
	print(mina)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	c11 = min(c[0], c[1] + c[5])
	c_1_1 = min(c[3], c[2] + c[4])
	c01 = min(c[1], c[0] + c[2])
	c10 = min(c[5], c[0] + c[4])
	c0_1 = min(c[4], c[3] + c[5])
	c_10 = min(c[2], c[3] + c[1])
	if x >= 0 and y >= 0:
		m = min(x, y)
		ans = c11 * m
		x -= m
		y -= m
		ans += c10 * x
		ans += c01 * y
	elif x >= 0 and y < 0:
		ans = c10 * x + c0_1 * abs(y)
	elif x < 0 and y >= 0:
		ans = c_10 * abs(x) + c01 * y
	else:
		x = abs(x)
		y = abs(y)
		m = min(x, y)
		ans = c_1_1 * m
		ans += c_10 * (x - m)
		ans += c0_1 * (y - m)
	print(ans)


from collections import Counter, defaultdict, deque
from math import inf

def f(dest, edge):
	(x, y) = dest
	(n1, n2, n3, n4, n5, n6) = edge
	n1 = min(n1, n2 + n6)
	n2 = min(n2, n1 + n3)
	n3 = min(n3, n2 + n4)
	n4 = min(n4, n5 + n3)
	n5 = min(n5, n4 + n6)
	n6 = min(n6, n1 + n5)
	ans = 0
	if x >= 0 and y >= 0:
		d = min(x, y)
		ans += d * n1
		x -= d
		y -= d
		if x > 0:
			ans += n6 * x
		if y > 0:
			ans += n2 * y
	elif x <= 0 and y >= 0:
		x = -x
		ans += y * n2 + x * n3
	elif x <= 0 and y <= 0:
		x = -x
		y = -y
		d = min(x, y)
		ans += d * n4
		x -= d
		y -= d
		if x > 0:
			ans += n3 * x
		if y > 0:
			ans += n5 * y
	elif x >= 0 and y <= 0:
		y = -y
		ans += x * n6
		ans += y * n5
	return ans
t = int(input())
result = []
for test in range(t):
	dest = list(map(int, input().split()))
	edge = list(map(int, input().split()))
	result.append(f(dest, edge))
for test in range(t):
	print(result[test])


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def gift():
	for _ in range(t):
		(tx, ty) = list(map(int, input().split()))
		(c1, c2, c3, c4, c5, c6) = list(map(int, input().split()))
		dic = [[1, 1], [0, 1], [-1, 0], [-1, -1], [0, -1], [1, 0]]
		if tx > 0:
			if ty > 0:
				if tx > ty:
					cost1 = c2 * ty + c6 * tx
					cost2 = (tx - ty) * c5 + c1 * tx
					cost3 = ty * c1 + (tx - ty) * c6
				elif tx < ty:
					cost1 = (ty - tx) * c2 + tx * c1
					cost2 = ty * c2 + tx * c6
					cost3 = ty * c1 + (ty - tx) * c3
				else:
					cost1 = tx * c1
					cost2 = tx * c2 + tx * c6
					cost3 = tx * c2 + tx * c6
			elif ty == 0:
				cost1 = tx * c6
				cost2 = tx * c1 + tx * c5
				cost3 = tx * c1 + tx * c5
			else:
				cost1 = abs(ty) * c5 + tx * c6
				cost2 = c5 * (tx - ty) + c1 * tx
				cost3 = c6 * (tx - ty) + c4 * abs(ty)
		elif tx < 0:
			if ty > 0:
				cost1 = c2 * ty + abs(tx) * c3
				cost2 = (ty - tx) * c2 + abs(tx) * c4
				cost3 = c1 * ty + (ty - tx) * c3
			elif ty == 0:
				cost1 = abs(tx) * c3
				cost2 = abs(tx) * (c2 + c4)
				cost3 = abs(tx) * (c2 + c4)
			elif tx > ty:
				cost1 = c6 * (tx - ty) + -ty * c4
				cost2 = (tx - ty) * c5 + -tx * c4
				cost3 = -ty * c5 + c3 * -tx
			elif tx < ty:
				cost1 = (ty - tx) * c3 + -ty * c4
				cost2 = -tx * c3 + -ty * c5
				cost3 = -tx * c4 + (ty - tx) * c2
			else:
				cost1 = -tx * c4
				cost2 = -tx * (c3 + c5)
				cost3 = -tx * (c3 + c5)
		elif ty > 0:
			cost1 = c2 * ty
			cost2 = (c1 + c3) * ty
			cost3 = (c1 + c3) * ty
		elif ty == 0:
			cost1 = 0
			cost2 = 0
			cost3 = 0
		else:
			cost1 = c5 * -ty
			cost2 = (c4 + c6) * -ty
			cost3 = (c4 + c6) * -ty
		yield min(cost1, cost2, cost3)
t = int(input())
ans = gift()
print(*ans, sep='\n')


import sys
input = sys.stdin.buffer.readline

def solution():
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	path1 = abs(x) * c[0 if x >= 0 else 3] + abs(y - x) * c[1 if y - x >= 0 else 4]
	path2 = abs(y) * c[0 if y >= 0 else 3] + abs(x - y) * c[5 if x - y >= 0 else 2]
	path3 = abs(y) * c[1 if y >= 0 else 4] + abs(x) * c[5 if x >= 0 else 2]
	print(min(path1, path2, path3))
t = int(input())
for _ in range(t):
	solution()


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	cost = list(map(int, input().split()))
	truecost = [0] * 6
	for i in range(6):
		truecost[i] = min(cost[i], cost[(i - 1) % 6] + cost[(i + 1) % 6])
	ans = 0
	if x > 0 and y > 0:
		w = min(x, y)
		ans += w * truecost[0]
		x -= w
		y -= w
	if x < 0 and y < 0:
		w = min(-x, -y)
		ans += w * truecost[3]
		x += w
		y += w
	if x < 0:
		ans += -x * truecost[2]
		x = 0
	if y < 0:
		ans += -y * truecost[4]
		y = 0
	if x > 0:
		ans += x * truecost[5]
		x = 0
	if y > 0:
		ans += y * truecost[1]
	print(ans)


T = int(input())
for t in range(T):
	(x, y) = tuple(map(int, input().split()))
	(c1, c2, c3, c4, c5, c6) = tuple(map(int, input().split()))
	h_p = min(c2, c1 + c3)
	h_n = min(c5, c6 + c4)
	v_p = min(c6, c1 + c5)
	v_n = min(c3, c2 + c4)
	d_p = min(c1, c2 + c6)
	d_n = min(c4, c3 + c5)
	h = h_p if y > 0 else h_n
	v = v_p if x > 0 else v_n
	cost = abs(x * v) + abs(y * h)
	d = d_p if x > 0 else d_n
	h = h_p if x < y else h_n
	cost = min(cost, abs(x * d) + abs(abs(x - y) * h))
	d = d_p if y > 0 else d_n
	v = v_p if y < x else v_n
	cost = min(cost, abs(y * d) + abs(abs(x - y) * v))
	print(cost)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	(x, y) = map(int, input().split())
	cost = list(map(int, input().split()))
	c = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
	ans = 10 ** 36
	for i in range(6):
		for j in range(6):
			(p, q) = c[i]
			(r, s) = c[j]
			if p * s - r * q != 0 and (p * y - q * x) % (p * s - r * q) == 0:
				b = (p * y - q * x) // (p * s - r * q)
				if p and (x - b * r) % p == 0:
					a = (x - b * r) // p
					if a >= 0 and b >= 0:
						ans = min(ans, cost[i] * a + cost[j] * b)
	print(ans)


import itertools
import math
import sys
from collections import defaultdict

def is_debug():
	return '--debug' in sys.argv

def stdin_wrapper():
	data = '3\n-3 1\n1 3 5 7 9 11\n1000000000 1000000000\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n-3 -1\n1 10 1 10 10 10\n'
	for line in data.split('\n'):
		yield line
if not is_debug():

	def stdin_wrapper():
		while True:
			yield sys.stdin.readline()
inputs = stdin_wrapper()

def input_wrapper():
	return next(inputs)

def get_str():
	if is_debug():
		return input_wrapper()
	return input()

def get(_type):
	if _type == str:
		return get_str()
	return _type(input_wrapper())

def get_arr(_type):
	return [_type(x) for x in input_wrapper().split()]

def tuplerize(method):

	def wrap(*args, **kwargs):
		res = method(*args, **kwargs)
		if not isinstance(res, (tuple, list)):
			res = (res,)
		return res
	return wrap

@tuplerize
def solve(target, edges):
	for i in range(-2, len(edges) - 2):
		edges[i + 1] = min(edges[i + 1], edges[i] + edges[i + 2])
	_d = {(1, 1): edges[0], (0, 1): edges[1], (-1, 0): edges[2], (-1, -1): edges[3], (0, -1): edges[4], (1, 0): edges[5]}
	(x, y) = target
	res = 0
	if y > 0 and x > 0:
		_min = min(y, x)
		res += _min * _d[1, 1]
		y -= _min
		x -= _min
	elif y < 0 and x < 0:
		_min = -max(y, x)
		res += _min * _d[-1, -1]
		y += _min
		x += _min
	elif y > 0 and x < 0:
		res += y * _d[0, 1]
		res -= x * _d[-1, 0]
		y = 0
		x = 0
	elif y < 0 and x > 0:
		res += x * _d[1, 0]
		res -= y * _d[0, -1]
		y = 0
		x = 0
	if x == 0 and y != 0:
		if y > 0:
			res += _d[0, 1] * y
		else:
			res -= _d[0, -1] * y
	if y == 0 and x != 0:
		if x > 0:
			res += _d[1, 0] * x
		else:
			res -= _d[-1, 0] * x
	return res
t = get(int)
for _ in range(t):
	target = get_arr(int)
	edges = get_arr(int)
	print(*solve(target, edges))


T = int(input())
for t in range(T):
	(X, Y) = [int(_) for _ in input().split()]
	C = [int(_) for _ in input().split()]
	for _ in range(10):
		for i in range(len(C)):
			prev = i - 1
			if prev < 0:
				prev += 6
			nxt = i + 1
			if nxt >= 6:
				nxt -= 6
			C[i] = min(C[i], C[prev] + C[nxt])
		for i in range(len(C) - 1, -1, -1):
			prev = i - 1
			if prev < 0:
				prev += 6
			nxt = i + 1
			if nxt >= 6:
				nxt -= 6
			C[i] = min(C[i], C[prev] + C[nxt])
	(c1, c2, c3, c4, c5, c6) = (C[0], C[1], C[2], C[3], C[4], C[5])
	if X == 0:
		if Y > 0:
			print(c2 * Y)
		else:
			print(c5 * abs(Y))
	elif X < 0:
		if Y >= 0:
			print(c3 * abs(X) + c2 * Y)
		elif Y <= X:
			print(c4 * abs(X) + c5 * abs(X - Y))
		else:
			print(c3 * abs(X - Y) + c4 * abs(Y))
	elif X > 0:
		if Y >= X:
			print(c1 * X + (Y - X) * c2)
		elif Y <= 0:
			print(c6 * X + abs(Y) * c5)
		else:
			print(c1 * Y + c6 * (X - Y))


import sys, io, os
BUFSIZE = 8192

class FastIO(io.IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = io.BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(io.IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from itertools import combinations
DX = [1, 0, -1, -1, 0, 1]
DY = [1, 1, 0, -1, -1, 0]
T = int(input())
for _ in range(T):
	(X, Y) = map(int, input().split())
	C = list(map(int, input().split()))
	if X == Y == 0:
		print(0)
		continue
	ans = 1 << 62
	for (i1, i2) in combinations(range(6), 2):
		(c1, c2) = (C[i1], C[i2])
		(x1, x2, y1, y2) = (DX[i1], DX[i2], DY[i1], DY[i2])
		numer2 = X * y1 - Y * x1
		denom2 = x2 * y1 - x1 * y2
		if denom2 == 0:
			continue
		n2 = numer2 // denom2
		if n2 < 0:
			continue
		numer1 = X * y2 - Y * x2
		denom1 = x1 * y2 - x2 * y1
		n1 = numer1 // denom1
		if n1 < 0:
			continue
		cost = n1 * c1 + n2 * c2
		if ans > cost:
			ans = cost
	print(ans)


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	(a, b, c, d, e, f) = map(int, input().split())
	c = [f, a, b, c, d, e]
	m = 0
	for i in range(6):
		if c[i] < c[m]:
			m = i
	j = (m + 1) % 6
	while j != m:
		c[j] = min(c[(j + 1) % 6] + c[(j - 1 + 6) % 6], c[j])
		j = (j + 1) % 6
	j = (m - 1 + 6) % 6
	while j != m:
		c[j] = min(c[(j + 1) % 6] + c[(j - 1 + 6) % 6], c[j])
		j = (j - 1 + 6) % 6
	if x == 0 and y >= 0:
		print(y * c[2])
	elif x == 0 and y <= 0:
		print(abs(y) * c[5])
	elif y == 0 and x >= 0:
		print(abs(x) * c[0])
	elif y == 0 and x <= 0:
		print(abs(x) * c[3])
	elif x > 0 and y > 0:
		if y > x:
			print(x * c[1] + (y - x) * c[2])
		else:
			print(y * c[1] + (x - y) * c[0])
	elif x < 0 and y < 0:
		if abs(y) > abs(x):
			print(abs(x) * c[4] + (abs(y) - abs(x)) * c[5])
		else:
			print(abs(y) * c[4] + (abs(x) - abs(y)) * c[3])
	else:
		ans = 0
		if x > 0:
			ans = ans + x * c[0]
		if y > 0:
			ans = ans + y * c[2]
		if x < 0:
			ans = ans + abs(x) * c[3]
		if y < 0:
			ans = ans + abs(y) * c[5]
		print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	(y, x) = RL()
	c = RLL()
	c = [0] + c
	res = float('inf')
	if y >= 0 and x >= 0 and (x <= y):
		res = min(res, (y - x) * c[6] + x * c[1])
		res = min(res, y * c[6] + x * c[2])
		res = min(res, (y - x) * c[5] + y * c[1])
	elif y >= 0 and x >= 0 and (x >= y):
		res = min(res, (x - y) * c[2] + y * c[1])
		res = min(res, y * c[6] + x * c[2])
		res = min(res, (x - y) * c[3] + x * c[1])
	elif y <= 0 and x >= 0:
		y = -y
		res = min(res, (x + y) * c[3] + x * c[1])
		res = min(res, y * c[3] + x * c[2])
		res = min(res, (x + y) * c[2] + y * c[4])
	elif y <= 0 and x <= 0 and (-x <= -y):
		y = -y
		x = -x
		res = min(res, (y - x) * c[2] + y * c[4])
		res = min(res, y * c[3] + x * c[5])
		res = min(res, (y - x) * c[3] + x * c[4])
	elif y <= 0 and x <= 0 and (-x >= -y):
		y = -y
		x = -x
		res = min(res, (x - y) * c[5] + y * c[4])
		res = min(res, y * c[3] + x * c[5])
		res = min(res, (x - y) * c[6] + x * c[4])
	else:
		x = -x
		res = min(res, (x + y) * c[6] + x * c[4])
		res = min(res, y * c[6] + x * c[5])
		res = min(res, (x + y) * c[5] + y * c[1])
	print(res)


for _ in range(int(input())):
	(x, y) = map(int, input().split())
	l = list(map(int, input().split()))
	(c1, c2, c3, c4, c5, c6) = (l[0], l[5], l[4], l[3], l[2], l[1])
	if x == 0 and y == 0:
		print(0)
	elif y == 0:
		if x > 0:
			if c1 + c3 < c2:
				print(x * (c1 + c3))
			else:
				print(x * c2)
		elif c4 + c6 < c5:
			print(abs(x) * (c4 + c6))
		else:
			print(abs(x) * c5)
	elif x == 0:
		if y > 0:
			if c1 + c5 < c6:
				print(y * (c1 + c5))
			else:
				print(c6 * y)
		elif c4 + c2 < c3:
			print(abs(y) * (c4 + c2))
		else:
			print(abs(y) * c3)
	elif x > 0 and y > 0:
		if x > y:
			li = []
			li.append(c2 * x + c6 * y)
			li.append(c1 * x + c3 * (x - y))
			li.append(c1 * y + c2 * (x - y))
			print(min(li))
		else:
			li = []
			li.append(c2 * x + c6 * y)
			li.append(c1 * x + c6 * (y - x))
			li.append(c1 * y + c5 * (y - x))
			print(min(li))
	elif x < 0 and y < 0:
		x = abs(x)
		y = abs(y)
		(c1, c4) = (c4, c1)
		(c2, c5) = (c5, c2)
		(c3, c6) = (c6, c3)
		if x > y:
			li = []
			li.append(c2 * x + c6 * y)
			li.append(c1 * x + c3 * (x - y))
			li.append(c1 * y + c2 * (x - y))
			print(min(li))
		else:
			li = []
			li.append(c2 * x + c6 * y)
			li.append(c1 * x + c6 * (y - x))
			li.append(c1 * y + c5 * (y - x))
			print(min(li))
	elif y > 0 and x < 0:
		x = abs(x)
		li = []
		li.append(c6 * y + x * c5)
		li.append(y * c1 + (x + y) * c5)
		li.append(x * c4 + (y + x) * c6)
		print(min(li))
	else:
		y = abs(y)
		li = []
		li.append(c3 * y + c2 * x)
		li.append(x * c1 + (x + y) * c3)
		li.append(y * c4 + (x + y) * c2)
		print(min(li))


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
n = inp()
for _ in range(n):
	target = inlt()
	l = inlt()
	if target[0] >= 0 and target[1] >= 0:
		path1 = target[0] * l[5] + target[1] * l[1]
		if target[0] > target[1]:
			path2 = target[0] * l[0] + abs(target[0] - target[1]) * l[4]
			path3 = target[1] * l[0] + abs(target[0] - target[1]) * l[5]
		else:
			path2 = target[1] * l[0] + abs(target[1] - target[0]) * l[2]
			path3 = target[0] * l[0] + abs(target[0] - target[1]) * l[1]
		print(min(path1, path2, path3))
	elif target[0] <= 0 and target[1] >= 0:
		path1 = abs(target[0] * l[2]) + abs(target[1] * l[1])
		path2 = abs(target[1] * l[0]) + (abs(target[0]) + abs(target[1])) * l[2]
		path3 = abs(target[0] * l[3]) + (abs(target[0]) + target[1]) * l[1]
		print(min(path1, path2, path3))
	elif target[0] >= 0 and target[1] <= 0:
		path1 = target[0] * l[5] + abs(target[1]) * l[4]
		path2 = target[0] * l[0] + (target[0] + abs(target[1])) * l[4]
		path3 = abs(target[1]) * l[3] + (target[0] + abs(target[1])) * l[5]
		print(min(path1, path2, path3))
	else:
		path1 = abs(target[0]) * l[2] + abs(target[1]) * l[4]
		if target[0] > target[1]:
			path2 = abs(target[0]) * l[3] + abs(target[0] - target[1]) * l[4]
			path3 = abs(target[1]) * l[3] + abs(target[0] - target[1]) * l[5]
		else:
			path2 = abs(target[1]) * l[3] + abs(target[1] - target[0]) * l[2]
			path3 = abs(target[0]) * l[3] + abs(target[0] - target[1]) * l[1]
		print(min(path1, path2, path3))


import sys
sys.setrecursionlimit(10 ** 8)

def solve(curx, cury, cost):
	if curx == tx and cury == ty:
		return cost
	elif curx == tx:
		if ty > cury:
			return cost + c2 * (ty - cury)
		else:
			return cost + c5 * (cury - ty)
	elif cury == ty:
		if tx > curx:
			return cost + c6 * (tx - curx)
		else:
			return cost + c3 * (curx - tx)
	elif tx > curx and ty > cury:
		diff = min(tx - curx, ty - cury)
		return solve(curx + diff, cury + diff, cost + c1 * diff)
	elif tx > curx and ty < cury:
		return cost + c6 * (tx - curx) + c5 * (cury - ty)
	elif tx < curx and ty < cury:
		diff = min(curx - tx, cury - ty)
		return solve(curx - diff, cury - diff, cost + c4 * diff)
	elif tx < curx and ty > cury:
		return cost + c3 * (curx - tx) + c2 * (ty - cury)
for _ in range(int(input())):
	(tx, ty) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	c1 = min(c1, c2 + c6)
	c2 = min(c2, c1 + c3)
	c3 = min(c3, c2 + c4)
	c4 = min(c4, c3 + c5)
	c5 = min(c5, c6 + c4)
	c6 = min(c6, c1 + c5)
	print(solve(0, 0, 0))


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		(X, Y) = [int(x) for x in input().split()]
		(C1, C2, C3, C4, C5, C6) = [int(x) for x in input().split()]
		ans = float('inf')
		if X == 0 and Y == 0:
			print(0)
			continue
		elif X == 0:
			if Y > 0:
				ans = min(ans, C2 * Y)
				ans = min(ans, C3 * Y + C1 * Y)
			else:
				Y = abs(Y)
				ans = min(ans, C5 * Y)
				ans = min(ans, C4 * Y + C6 * Y)
		elif Y == 0:
			if X > 0:
				ans = min(ans, C6 * X)
				ans = min(ans, C1 * X + C5 * X)
			else:
				X = abs(X)
				ans = min(ans, C3 * X)
				ans = min(ans, C2 * X + C4 * X)
		elif X > 0 and Y > 0:
			ans = min(ans, C6 * X + C2 * Y)
			if X == Y:
				ans = min(ans, C1 * X)
			elif X > Y:
				ans = min(ans, C1 * X + C5 * (X - Y))
				ans = min(ans, C1 * Y + C6 * (X - Y))
			else:
				ans = min(ans, C1 * X + C2 * (Y - X))
				ans = min(ans, C1 * Y + C3 * (Y - X))
		elif X > 0 and Y < 0:
			X = abs(X)
			Y = abs(Y)
			ans = min(ans, C6 * X + C5 * Y)
			ans = min(ans, C1 * X + C5 * (Y + X))
			ans = min(ans, C4 * Y + C6 * (Y + X))
		elif X < 0 and Y > 0:
			X = abs(X)
			Y = abs(Y)
			ans = min(ans, C3 * X + C2 * Y)
			ans = min(ans, C4 * X + C2 * (Y + X))
			ans = min(ans, C1 * Y + C3 * (Y + X))
		elif X < 0 and Y < 0:
			X = abs(X)
			Y = abs(Y)
			ans = min(ans, C3 * X + C5 * Y)
			if X == Y:
				ans = min(ans, C4 * X)
			elif X > Y:
				ans = min(ans, C4 * X + C2 * (X - Y))
				ans = min(ans, C4 * Y + C3 * (X - Y))
			else:
				ans = min(ans, C4 * X + C5 * (Y - X))
				ans = min(ans, C4 * Y + C6 * (Y - X))
		print(ans)
main()


import sys, os
import math
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def MI():
	return map(int, input().split())

def out(var):
	sys.stdout.write(str(var) + '\n')
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	l = list(map(int, input().split()))
	(c1, c2, c3, c4, c5, c6) = (l[0], l[5], l[4], l[3], l[2], l[1])
	if x == 0 and y == 0:
		print(0)
	elif y == 0:
		if x > 0:
			if c1 + c3 < c2:
				print(x * (c1 + c3))
			else:
				print(x * c2)
		elif c4 + c6 < c5:
			print(abs(x) * (c4 + c6))
		else:
			print(abs(x) * c5)
	elif x == 0:
		if y > 0:
			if c1 + c5 < c6:
				print(y * (c1 + c5))
			else:
				print(c6 * y)
		elif c4 + c2 < c3:
			print(abs(y) * (c4 + c2))
		else:
			print(abs(y) * c3)
	elif x > 0 and y > 0:
		if x > y:
			li = []
			li.append(c2 * x + c6 * y)
			li.append(c1 * x + c3 * (x - y))
			li.append(c1 * y + c2 * (x - y))
			print(min(li))
		else:
			li = []
			li.append(c2 * x + c6 * y)
			li.append(c1 * x + c6 * (y - x))
			li.append(c1 * y + c5 * (y - x))
			print(min(li))
	elif x < 0 and y < 0:
		x = abs(x)
		y = abs(y)
		(c1, c4) = (c4, c1)
		(c2, c5) = (c5, c2)
		(c3, c6) = (c6, c3)
		if x > y:
			li = []
			li.append(c2 * x + c6 * y)
			li.append(c1 * x + c3 * (x - y))
			li.append(c1 * y + c2 * (x - y))
			print(min(li))
		else:
			li = []
			li.append(c2 * x + c6 * y)
			li.append(c1 * x + c6 * (y - x))
			li.append(c1 * y + c5 * (y - x))
			print(min(li))
	elif y > 0 and x < 0:
		x = abs(x)
		li = []
		li.append(c6 * y + x * c5)
		li.append(y * c1 + (x + y) * c5)
		li.append(x * c4 + (y + x) * c6)
		print(min(li))
	else:
		y = abs(y)
		li = []
		li.append(c3 * y + c2 * x)
		li.append(x * c1 + (x + y) * c3)
		li.append(y * c4 + (x + y) * c2)
		print(min(li))


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split(' '))
	c = [int(val) for val in input().split(' ')]
	if x == 0 and y == 0:
		print(0)
	elif x == 0:
		if y > 0:
			print(min(c[1] * y, (c[0] + c[2]) * y))
		else:
			print(min(c[4] * -y, (c[3] + c[5]) * -y))
	elif y == 0:
		if x > 0:
			print(min(c[5] * x, (c[0] + c[4]) * x))
		else:
			print(min(c[2] * -x, (c[1] + c[3]) * -x))
	elif x > 0:
		if x == y:
			print(min(c[0] * x, (c[1] + c[5]) * x))
		elif y > x:
			print(min(c[0] * x + c[1] * (y - x), c[1] * y + c[5] * x, c[0] * y + c[2] * (y - x)))
		elif y < 0:
			print(min(c[5] * x + c[4] * -y, c[5] * (x - y) + c[3] * -y, c[4] * (x - y) + c[0] * x))
		else:
			print(min(c[5] * (x - y) + c[0] * y, c[1] * y + c[5] * x, c[4] * (x - y) + c[0] * x))
	elif x < 0:
		if x == y:
			print(min(c[3] * -x, (c[2] + c[4]) * -x))
		elif x > y:
			print(min(c[3] * -x + c[4] * (x - y), c[4] * -y + c[2] * -x, c[3] * -y + c[5] * (x - y)))
		elif y > 0:
			print(min(c[2] * -x + c[1] * y, c[2] * (y - x) + c[0] * y, c[1] * (y - x) + c[3] * -x))
		else:
			print(min(c[2] * (y - x) + c[3] * -y, c[4] * -y + c[2] * -x, c[1] * (y - x) + c[3] * -x))


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	if x == y == 0:
		print(0)
		continue
	if x == 0:
		one_line = True
		l = abs(y)
		if y > 0:
			(cs1, cm, cs2) = (c1, c2, c3)
		else:
			(cs1, cm, cs2) = (c4, c5, c6)
	elif y == 0:
		one_line = True
		l = abs(x)
		if x > 0:
			(cs1, cm, cs2) = (c5, c6, c1)
		else:
			(cs1, cm, cs2) = (c2, c3, c4)
	elif x == y:
		one_line = True
		l = abs(x)
		if x > 0:
			(cs1, cm, cs2) = (c6, c1, c2)
		else:
			(cs1, cm, cs2) = (c3, c4, c5)
	else:
		one_line = False
		if x > 0:
			if x < y:
				(cs1, cm1, cm2, cs2) = (c6, c1, c2, c3)
				(a, b) = (x, y - x)
			elif y > 0:
				(cs1, cm1, cm2, cs2) = (c5, c6, c1, c2)
				(a, b) = (x - y, y)
			else:
				(cs1, cm1, cm2, cs2) = (c4, c5, c6, c1)
				(a, b) = (-y, x)
		elif x > y:
			(cs1, cm1, cm2, cs2) = (c3, c4, c5, c6)
			(a, b) = (-x, x - y)
		elif y < 0:
			(cs1, cm1, cm2, cs2) = (c2, c3, c4, c5)
			(a, b) = (y - x, -y)
		else:
			(cs1, cm1, cm2, cs2) = (c1, c2, c3, c4)
			(a, b) = (y, -x)
	if one_line:
		print(min(cm * l, (cs1 + cs2) * l))
	else:
		print(min(cm1 * a + cm2 * b, cm1 * a + (cm1 + cs2) * b, (cs1 + cm2) * a + cm2 * b))


import sys, math, cmath, time
start_time = time.time()

def solve():
	(x, y) = invr()
	c = inlt()
	qq = ww = qw = 0
	ww = ww + x
	qw = qw - x
	qq = qq + y
	ww = ww - y
	while not (qq >= 0 and ww <= 0 and (qw <= 0)):
		(qq, ww, qw) = (-ww, -qw, -qq)
		c = c[1:] + c[:1]
	(c1, c2, c3, c4, c5, c6) = c
	print(min(c1, c2 + c6) * -qw + min(c2, min(c1, c2) + c3) * -ww)

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(1, t + 1):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def counter_elements(a):
	q = dict()
	for i in range(len(a)):
		if a[i] not in q:
			q[a[i]] = 0
		q[a[i]] = q[a[i]] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
	return q

def factorial(n, m=1000000007):
	q = 1
	for i in range(n):
		q = q * (i + 1) % m
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))

def transpose(a):
	(n, m) = (len(a), len(a[0]))
	b = [[0] * n for i in range(m)]
	for i in range(m):
		for j in range(n):
			b[i][j] = a[j][i]
	return b

def power_two(x):
	return x and (not x & x - 1)

def ceil(a, b):
	return -(-a // b)

def seive(n):
	a = [1]
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p ** 2, n + 1, p):
				prime[i] = False
		p = p + 1
	for p in range(2, n + 1):
		if prime[p]:
			a.append(p)
	return a
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


def xyc(x, y):
	an1 = 0
	if x > 0:
		an1 += cos[5] * x
	else:
		an1 += cos[2] * -x
	if y > 0:
		an1 += cos[1] * y
	else:
		an1 += cos[4] * -y
	return an1
t = int(input())
for _ in range(t):
	p = list(map(int, input().split()))
	(x, y) = (p[0], p[1])
	cos = list(map(int, input().split()))
	for a in range(len(cos)):
		if cos[(a + 1) % 6] + cos[(a - 1) % 6] < cos[a]:
			cos[a] = cos[(a + 1) % 6] + cos[(a - 1) % 6]
	an1 = xyc(x, y)
	an2 = 0
	if x * y < 0:
		an2 = an1
	elif x > 0 and x > y:
		an2 += cos[0] * y
		an2 += xyc(x - y, 0)
	elif x > 0:
		an2 += cos[0] * x
		an2 += xyc(0, y - x)
	elif x < 0 and x < y:
		an2 += cos[3] * -y
		an2 += xyc(x - y, 0)
	else:
		an2 += cos[3] * -x
		an2 += xyc(0, y - x)
	print(min(an1, an2))


import sys

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def soln(targetxy, costs):
	assert len(costs) == 6
	finalcost = 999
	for _ in range(6):
		costs[0] = min(costs[0], costs[5] + costs[1])
		costs[1] = min(costs[1], costs[0] + costs[2])
		costs[2] = min(costs[2], costs[3] + costs[1])
		costs[3] = min(costs[3], costs[2] + costs[4])
		costs[4] = min(costs[4], costs[3] + costs[5])
		costs[5] = min(costs[5], costs[4] + costs[0])
	if targetxy[1] < 0:
		targetxy[0] *= -1
		targetxy[1] *= -1
		costs = costs[3:6] + costs[0:3]
	if targetxy[0] < 0:
		diagstep = targetxy[0] * -1 * costs[4] + targetxy[1] * costs[5]
		otherstep1 = targetxy[0] * -1 * costs[4] + targetxy[1] * (costs[0] + costs[4])
		otherstep2 = targetxy[1] * costs[5] + targetxy[0] * -1 * (costs[5] + costs[3])
		finalcost = min(min(diagstep, otherstep1), otherstep2)
	else:
		diagstep = targetxy[0] * costs[1] + targetxy[1] * costs[5]
		common = min(targetxy[0], targetxy[1])
		straightstep = common * costs[0] + (targetxy[0] - common) * costs[1] + (targetxy[1] - common) * costs[5]
		finalcost = min(diagstep, straightstep)
	return finalcost
tests = inp()
for test in range(tests):
	targetxy = inlt()
	costs = inlt()
	targetxy = [targetxy[1], targetxy[0]]
	print(soln(targetxy, costs))


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(x, y) = map(int, input().split())
	lis = list(map(int, input().split()))
	costs = [0] * 6
	costs[0] = min(lis[0], lis[1] + lis[5])
	costs[1] = min(lis[1], lis[0] + lis[2])
	costs[2] = min(lis[2], lis[1] + lis[3])
	costs[3] = min(lis[3], lis[2] + lis[4])
	costs[4] = min(lis[4], lis[3] + lis[5])
	costs[5] = min(lis[5], lis[4] + lis[0])
	ans = 0
	if x >= 0 and y >= 0:
		ans += min(x, y) * costs[0]
		if x >= y:
			ans += (x - y) * costs[5]
		else:
			ans += (y - x) * costs[1]
	elif x <= 0 and y <= 0:
		ans += min(abs(x), abs(y)) * costs[3]
		if x <= y:
			ans += (y - x) * costs[2]
		else:
			ans += (x - y) * costs[4]
	elif x >= 0 and y <= 0:
		ans += x * costs[5]
		ans += abs(y) * costs[4]
	elif x <= 0 and y >= 0:
		ans += abs(x) * costs[2]
		ans += abs(y) * costs[1]
	print(ans)


for _ in range(int(input())):
	(target_x, target_y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	for i in range(100):
		c1 = min(c1, c2 + c6)
		c2 = min(c2, c1 + c3)
		c3 = min(c3, c2 + c4)
		c4 = min(c4, c3 + c5)
		c5 = min(c5, c4 + c6)
		c6 = min(c6, c1 + c5)
	result = 0
	if target_x > 0:
		if target_y > 0:
			target = min(target_x, target_y)
			result += c1 * target
			target_x -= target
			target_y -= target
			result += c2 * target_y
			result += c6 * target_x
		elif target_y < 0:
			result += c5 * -target_y
			result += c6 * target_x
		else:
			result += c6 * target_x
	elif target_x < 0:
		if target_y > 0:
			result += c2 * target_y
			result += c3 * -target_x
		elif target_y < 0:
			target = max(target_x, target_y)
			result += c4 * -target
			target_x -= target
			target_y -= target
			result += c5 * -target_y
			result += c3 * -target_x
		else:
			result += c3 * -target_x
	elif target_y > 0:
		result += c2 * target_y
	elif target_y < 0:
		result += c5 * -target_y
	else:
		result += 0
	print(result)


for _ in range(int(input())):
	(x1, y1) = map(int, input().split())
	cost1 = list(map(int, input().split()))
	p1 = abs(x1) * cost1[0 if x1 >= 0 else 3] + abs(y1 - x1) * cost1[1 if y1 - x1 >= 0 else 4]
	p2 = abs(y1) * cost1[0 if y1 >= 0 else 3] + abs(x1 - y1) * cost1[5 if x1 - y1 >= 0 else 2]
	p3 = abs(y1) * cost1[1 if y1 >= 0 else 4] + abs(x1) * cost1[5 if x1 >= 0 else 2]
	print(min(p1, p2, p3))


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
from bisect import bisect_left as lower_bound, bisect_right as upper_bound

def so():
	return int(input())

def st():
	return input()

def mj():
	return map(int, input().strip().split(' '))

def msj():
	return map(str, input().strip().split(' '))

def le():
	return list(map(int, input().split()))

def lebe():
	return list(map(int, input()))

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()

def joro(L):
	return ''.join(map(str, L))

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def tr(n):
	return n * (n + 1) // 2

def main():
	import math as my
	for i in range(so()):
		(n, q) = mj()
		L = le()
		P = []
		a = abs(n)
		b = abs(q)
		c = abs(n - q)
		if n >= 0:
			ro = 0
		else:
			ro = 3
		if n <= q:
			to = 1
		else:
			to = 4
		lebu = a * L[ro] + c * L[to]
		if q >= 0:
			mo = 0
		else:
			mo = 3
		if n >= q:
			bo = 5
		else:
			bo = 2
		kat = c * L[bo] + b * L[mo]
		if q < 0:
			ty = 4
		else:
			ty = 1
		if n < 0:
			by = 2
		else:
			by = 5
		mimo = a * L[by] + b * L[ty]
		P.append(lebu)
		P.append(kat)
		P.append(mimo)
		print(min(P))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def sol(a1, a2, b1, b2, c1, c2):
	d = -b1 * a2 + b2 * a1
	d1 = c1 * b2 - c2 * b1
	d2 = -c1 * a2 + c2 * a1
	x1 = d1 // d
	x2 = d2 // d
	return (x1, x2)
m = [[1, 1], [0, 1], [-1, 0], [-1, -1], [0, -1], [1, 0]]
for qq in range(int(input())):
	f = [int(s) for s in input().split()]
	cost = [int(s) for s in input().split()]
	ans = 10 ** 30
	for i in range(6):
		for j in range(6):
			if (j - i) % 3 != 0:
				x = sol(m[i][0], m[i][1], m[j][0], m[j][1], f[0], f[1])
				if x[0] >= 0 and x[1] >= 0:
					t = x[0] * cost[i] + x[1] * cost[j]
					ans = min(ans, t)
	print(ans)


q = int(input())
numer = 0
import random

def ope(numer, tablica):
	if numer == 0:
		tablica[0] = min(tablica[0], tablica[1] + tablica[5])
	if numer == 1:
		tablica[1] = min(tablica[1], tablica[0] + tablica[2])
	if numer == 2:
		tablica[2] = min(tablica[2], tablica[1] + tablica[3])
	if numer == 3:
		tablica[3] = min(tablica[3], tablica[2] + tablica[4])
	if numer == 4:
		tablica[4] = min(tablica[4], tablica[3] + tablica[5])
	if numer == 5:
		tablica[5] = min(tablica[5], tablica[4] + tablica[0])
for _ in range(q):
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	for i in range(6):
		ope(numer, c)
	for i in range(600):
		a = random.randint(0, 5)
		ope(a, c)
	if x >= 0:
		dod = 0
		if y >= x:
			wyn = c[0 + dod] * x + c[1 + dod] * (y - x)
		elif y >= 0:
			wyn = (x - y) * c[5 + dod] + y * c[dod]
		else:
			wyn = x * c[5 + dod] - y * c[4 + dod]
	else:
		x *= -1
		y *= -1
		dod = 3
		if y >= x:
			wyn = c[0 - dod] * x + c[1 - dod] * (y - x)
		elif y >= 0:
			wyn = (x - y) * c[5 - dod] + y * c[-dod]
		else:
			wyn = x * c[5 - dod] - y * c[4 - dod]
	print(wyn)


import io
import os
from collections import Counter, defaultdict, deque
from itertools import permutations

def solve(X, Y, C):
	dxdy = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
	dToCost = dict(zip(dxdy, C))
	for d1 in dxdy:
		for d2 in dxdy:
			dx = d1[0] + d2[0]
			dy = d1[1] + d2[1]
			if (dx, dy) in dToCost:
				dToCost[dx, dy] = min(dToCost[dx, dy], dToCost[d1] + dToCost[d2])

	def getMultiple(dx, dy, X, Y):
		a = -1
		if dx != 0:
			a = X // dx
		elif dy != 0:
			a = Y // dy
		else:
			assert False
		if a >= 0 and a * dx == X and (a * dy == Y):
			return a
		return -1
	best = float('inf')
	for (dx, dy) in dxdy:
		a = getMultiple(dx, dy, X, Y)
		if a != -1:
			best = min(best, a * dToCost[dx, dy])
	for d1 in dxdy:
		for d2 in dxdy:
			if d1 == d2:
				continue
			a = -1
			b = -1
			(dx1, dy1) = d1
			(dx2, dy2) = d2
			if dx1 == -dx2 and dy1 == -dy2:
				continue
			if dx1 == 0 or dx2 == 0:
				if dx1 == 0:
					assert dx2
					b = X // dx2
					X2 = X - b * dx2
					Y2 = Y - b * dy2
					a = getMultiple(dx1, dy1, X2, Y2)
				elif dx2 == 0:
					assert dx1
					a = X // dx1
					X2 = X - a * dx1
					Y2 = Y - a * dy1
					b = getMultiple(dx2, dy2, X2, Y2)
				pass
			elif dy1 == 0 or dy2 == 0:
				if dy1 == 0:
					assert dy2
					b = Y // dy2
					X2 = X - b * dx2
					Y2 = Y - b * dy2
					a = getMultiple(dx1, dy1, X2, Y2)
				elif dy2 == 0:
					assert dy1
					a = Y // dy1
					X2 = X - a * dx1
					Y2 = Y - a * dy1
					b = getMultiple(dx2, dy2, X2, Y2)
				pass
			else:
				assert False
			if a >= 0 and b >= 0 and (a * dx1 + b * dx2 == X) and (a * dy1 + b * dy2 == Y):
				best = min(best, a * dToCost[d1] + b * dToCost[d2])
	return best
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(X, Y) = [int(x) for x in input().split()]
	C = [int(x) for x in input().split()]
	ans = solve(X, Y, C)
	if False:
		for X in range(10):
			for Y in range(10):
				solve(X, Y, C)
	print(ans)


def fun(x, y):
	global c1, c2, c3, c4, c5, c6
	val = 0
	if x >= 0:
		val += c6 * x
	else:
		val += c3 * abs(x)
	if y >= 0:
		val += c2 * y
	else:
		val += c5 * abs(y)
	return val

def fun1(x):
	global c1, c2, c3, c4, c5, c6
	val = 0
	if x >= 0:
		val += c1 * x
	else:
		val += c4 * abs(x)
	return val
for _ in range(int(input())):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	ans = []
	tem = 0
	tem += fun(x, y)
	ans.append(tem)
	tem = 0
	tem += fun1(x)
	dis = y - x
	tem += fun(0, dis)
	ans.append(tem)
	tem = 0
	tem += fun1(y)
	dis = x - y
	tem += fun(dis, 0)
	ans.append(tem)
	print(min(ans))


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	(r, c) = map(int, inputi().split())
	costs = [int(a) for a in inputi().split()]
	if r >= 0:
		if c <= 0:
			o1 = costs[4] * -c + costs[5] * r
			o2 = costs[4] * (r - c) + costs[0] * r
			o3 = costs[3] * -c + costs[5] * (r - c)
		elif c <= r:
			o1 = costs[5] * (r - c) + costs[0] * c
			o2 = costs[5] * r + costs[1] * c
			o3 = costs[4] * (r - c) + costs[0] * r
		else:
			o1 = costs[0] * r + costs[1] * (c - r)
			o2 = costs[0] * c + costs[2] * (c - r)
			o3 = costs[5] * r + costs[1] * c
	elif c >= 0:
		o1 = costs[1] * c + costs[2] * -r
		o2 = costs[1] * (c - r) + costs[3] * -r
		o3 = costs[0] * c + costs[2] * (c - r)
	elif c >= r:
		o1 = costs[2] * (c - r) + costs[3] * -c
		o2 = costs[2] * -r + costs[4] * -c
		o3 = costs[1] * (c - r) + costs[3] * -r
	else:
		o1 = costs[3] * -r + costs[4] * (r - c)
		o2 = costs[3] * -c + costs[5] * (r - c)
		o3 = costs[2] * -r + costs[4] * -c
	print(min(o1, o2, o3))

def main():
	t = int(inputi())
	for _ in range(t):
		solve()
main()


t = int(input())
for _ in range(0, t):
	(x, y) = map(int, input().split())
	(c1, c2, c3, c4, c5, c6) = map(int, input().split())
	c11 = min(c1, c2 + c6)
	c22 = min(c2, c3 + c1)
	c33 = min(c3, c4 + c2)
	c44 = min(c4, c5 + c3)
	c55 = min(c5, c6 + c4)
	c66 = min(c6, c1 + c5)
	ss = 0
	if x >= 0 and y >= 0:
		mm = min(x, y)
		ss += mm * c11
		x -= mm
		y -= mm
		if x > 0:
			ss += x * c66
		else:
			ss += y * c22
	elif x <= 0 and y >= 0:
		ss += -1 * x * c33
		ss += y * c22
	elif x <= 0 and y <= 0:
		(x, y) = (-1 * x, -1 * y)
		mm = min(x, y)
		ss += mm * c44
		x -= mm
		y -= mm
		if x > 0:
			ss += x * c33
		else:
			ss += y * c55
	elif x >= 0 and y <= 0:
		ss += x * c66
		ss += -1 * y * c55
	print(ss)


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(inp()):
	(y, x) = inpl()
	c = inpl()
	if x == y == 0:
		print(0)
		continue
	if x < 0 and y <= 0 or (x < 0 and y >= 0):
		(x, y) = (-x, -y)
		for i in range(3):
			(c[i], c[3 + i]) = (c[3 + i], c[i])
	res = INF
	if x >= 0 and y >= 0:
		res = min(res, c[1] * x + c[5] * y)
		if y >= x:
			res = min(res, c[5] * (y - x) + c[0] * x, c[4] * (y - x) + c[0] * y)
		else:
			res = min(res, c[1] * (x - y) + c[0] * y, c[2] * (x - y) + c[0] * x)
	else:
		res = min(res, c[1] * x + c[2] * abs(y), c[0] * x + c[2] * (x + abs(y)), c[3] * abs(y) + c[1] * (x + abs(y)))
	print(res)


import bisect
import heapq
import itertools
import sys
import math
import random
from collections import Counter, deque, defaultdict
from functools import reduce
from operator import xor
from typing import List
mod = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 9)

def lmi():
	return list(map(int, input().split()))

def main():
	t = int(input())
	for _ in range(t):
		(x, y) = lmi()
		C = lmi()
		ret = solve(x, y, C)
		print(ret)

def solve(x: int, y: int, C: List[int]):
	costs = {(-1, -1): min(C[3], C[2] + C[4]), (-1, 0): min(C[2], C[1] + C[3]), (0, -1): min(C[4], C[3] + C[5]), (0, +1): min(C[1], C[0] + C[2]), (+1, 0): min(C[5], C[4] + C[0]), (+1, 1): min(C[0], C[5] + C[1])}
	ans = 0
	if x > 0 and y > 0:
		mn = min(x, y)
		ans += mn * costs[1, 1]
		x -= mn
		y -= mn
	if x < 0 and y < 0:
		mn = min(abs(x), abs(y))
		ans += mn * costs[-1, -1]
		x += mn
		y += mn
	if x > 0:
		ans += costs[1, 0] * abs(x)
	else:
		ans += costs[-1, 0] * abs(x)
	if y > 0:
		ans += costs[0, 1] * abs(y)
	else:
		ans += costs[0, -1] * abs(y)
	return ans
main()


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	UR = 10 ** 9
	UL = 10 ** 9
	R = 10 ** 9
	L = 10 ** 9
	DL = 10 ** 9
	DR = 10 ** 9
	UR = min(c[0], c[1] + c[5])
	UL = min(c[5], c[0] + c[4])
	R = min(c[1], c[2] + c[0])
	L = min(c[4], c[5] + c[3])
	DL = min(c[3], c[4] + c[2])
	DR = min(c[2], c[1] + c[3])
	ans = 0
	if x == 0:
		if y >= 0:
			ans += y * R
		else:
			ans += -y * L
	elif x > 0:
		if y == 0:
			ans += x * UL
		elif y > 0:
			if x >= y:
				ans += y * UR
				ans += (x - y) * UL
			else:
				ans += x * UR
				ans += (y - x) * R
		else:
			ans = x * UL + -y * L
	elif y == 0:
		ans = DR * -x
	elif y > 0:
		ans = R * y + -x * DR
	elif x >= y:
		ans += -x * DL
		ans += -(y - x) * L
	else:
		ans += -y * DL
		ans += -(x - y) * DR
	print(ans)


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	(x, y) = tuple((int(x) for x in stdin.readline().split()))
	lst = list((int(x) for x in stdin.readline().split()))
	flag = True
	while flag:
		flag = False
		for i in range(6):
			if lst[i - 1] + lst[(i + 1) % 6] < lst[i]:
				lst[i] = lst[i - 1] + lst[(i + 1) % 6]
				flag = True
	if x >= 0 and y >= 0:
		if x >= y:
			print(y * lst[0] + (x - y) * lst[5])
		else:
			print(x * lst[0] + (y - x) * lst[1])
	elif x <= 0 and y <= 0:
		if x >= y:
			print(-x * lst[3] + (x - y) * lst[4])
		else:
			print(-y * lst[3] + (y - x) * lst[2])
	elif x >= 0 and y <= 0:
		print(x * lst[5] + -y * lst[4])
	elif x <= 0 and y >= 0:
		print(-x * lst[2] + y * lst[1])


t = int(input())

def neg(i):
	return (i + 3) % 6
for case in range(t):
	(x, y) = map(int, input().split())
	x *= 1
	y *= 1
	C = [int(x) for x in input().split()]
	steps = [0, 0, 0, 0, 0, 0]
	if x < 0:
		steps[2] = -x
	else:
		steps[5] = x
	if y < 0:
		steps[4] = -y
	else:
		steps[1] = y

	def normalize():
		for i in range(3):
			mn = min(steps[i], steps[i + 3])
			steps[i] -= mn
			steps[i + 3] -= mn
	moves = [(0, (5, 1)), (1, (0, 2)), (2, (1, 3)), (3, (2, 4)), (4, (3, 5)), (5, (4, 0))]
	res = 10 ** 100
	while True:
		for (i, (j, k)) in moves:
			if C[i] > C[j] + C[k] - (steps[neg(j)] > 0) * C[neg(j)] - (steps[neg(k)] > 0) * C[neg(k)]:
				mn = min(steps[i], steps[neg(j)], steps[neg(k)])
				steps[i] -= mn
				steps[j] += mn
				steps[k] += mn
			normalize()
			if C[i] > C[j] + C[k] - (steps[neg(j)] > 0) * C[neg(j)]:
				mn = min(steps[i], steps[neg(j)])
				steps[i] -= mn
				steps[j] += mn
				steps[k] += mn
			normalize()
			if C[i] > C[j] + C[k] - (steps[neg(k)] > 0) * C[neg(k)]:
				mn = min(steps[i], steps[neg(k)])
				steps[i] -= mn
				steps[j] += mn
				steps[k] += mn
			normalize()
			if C[i] > C[j] + C[k]:
				mn = steps[i]
				steps[i] -= mn
				steps[j] += mn
				steps[k] += mn
			normalize()
			if C[i] - (steps[neg(i)] > 0) * C[neg(i)] < C[j] + C[k]:
				mn = min(steps[j], steps[k], steps[neg(i)])
				steps[i] += mn
				steps[j] -= mn
				steps[k] -= mn
			normalize()
			if C[i] < C[j] + C[k]:
				mn = min(steps[j], steps[k])
				steps[i] += mn
				steps[j] -= mn
				steps[k] -= mn
			normalize()
		s = sum((s * c for (s, c) in zip(steps, C)))
		if s < res:
			res = s
		else:
			break
	print(res)


t = int(input())
for _ in range(t):
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	for i in range(6):
		c[i] = min(c[i], c[(i - 1) % 6] + c[(i + 1) % 6])
	if x > 0 and y > 0:
		cnt = min(x, y)
		print(cnt * c[0] + (x - cnt) * c[5] + (y - cnt) * c[1])
	elif x < 0 and y < 0:
		(x, y) = (-x, -y)
		cnt = min(x, y)
		print(cnt * c[3] + (x - cnt) * c[2] + (y - cnt) * c[4])
	else:
		ans = 0
		if x < 0:
			ans += -x * c[2]
		else:
			ans += x * c[5]
		if y < 0:
			ans += -y * c[4]
		else:
			ans += y * c[1]
		print(ans)


def main():
	n = int(input())
	for i in range(n):
		(x, y) = map(int, input().split())
		(c1, c2, c3, c4, c5, c6) = map(int, input().split())
		answer = 0
		if x > 0:
			answer += x * c6
		else:
			answer += -x * c3
		if y > 0:
			answer += y * c2
		else:
			answer += -y * c5
		m = min(x, y)
		ma = max(x, y)
		if x > 0 or y > 0:
			if m > 0:
				answer = min(answer, m * c1 + (x - m) * c6 + (y - m) * c2)
			answer = min(answer, ma * c1 + (ma - x) * c3 + (ma - y) * c5)
		if x < 0 or y < 0:
			if ma < 0:
				answer = min(answer, -ma * c4 + (ma - x) * c3 + (ma - y) * c5)
			answer = min(answer, -m * c4 + (x - m) * c6 + (y - m) * c2)
		print(answer)
main()


T = int(input())
for _ in range(T):
	(r, c) = map(int, input().split())
	(UR, R, D, LD, L, U) = map(int, input().split())
	(UR, R, D, LD, L, U) = (min(UR, U + R), min(R, UR + D), min(D, R + LD), min(LD, L + D), min(L, U + LD), min(U, L + UR))
	cc = 0
	if r > 0 and c > 0:
		mn = min(r, c)
		cc += mn * UR
		(r, c) = (r - mn, c - mn)
	if r < 0 and c < 0:
		mn = min(-r, -c)
		cc += mn * LD
		(r, c) = (r + mn, c + mn)
	if r >= 0:
		cc += U * r
	else:
		cc += D * -r
	if c >= 0:
		cc += R * c
	else:
		cc += L * -c
	print(cc)


import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
allAns = []
t = int(input())
for _ in range(t):
	(x, y) = [int(z) for z in input().split()]
	(c1, c2, c3, c4, c5, c6) = [int(z) for z in input().split()]
	ans = float('inf')
	t = y
	s = x - t
	ans2 = 0
	if t >= 0:
		ans2 += c1 * abs(t)
	else:
		ans2 += c4 * abs(t)
	if s >= 0:
		ans2 += c6 * abs(s)
	else:
		ans2 += c3 * abs(s)
	ans = min(ans, ans2)
	s = x
	t = y
	ans2 = 0
	if s >= 0:
		ans2 += c6 * abs(s)
	else:
		ans2 += c3 * abs(s)
	if t >= 0:
		ans2 += c2 * abs(t)
	else:
		ans2 += c5 * abs(t)
	ans = min(ans, ans2)
	t = x
	s = y - t
	ans2 = 0
	if s >= 0:
		ans2 += c2 * abs(s)
	else:
		ans2 += c5 * abs(s)
	if t >= 0:
		ans2 += c1 * abs(t)
	else:
		ans2 += c4 * abs(t)
	ans = min(ans, ans2)
	allAns.append(ans)
multiLineArrayPrint(allAns)


t = int(input())
for i in range(t):
	(x, y) = list(map(int, input().split()))
	c = list(map(int, input().split()))
	turns = [(1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1), (1, 0)]
	true_paths = [None, None, None, None, None, None]
	min_in_c = min(c)
	for j in range(6):
		if c[j] == min_in_c:
			true_paths[j] = c[j]
			for g in range(6):
				sum_x = turns[j][0] + turns[g][0]
				sum_y = turns[j][1] + turns[g][1]
				for k in range(6):
					if turns[k][0] == sum_x and turns[k][1] == sum_y:
						sum1 = c[k]
						sum2 = c[j] + c[g]
						true_paths[k] = min(sum1, sum2)
	for j in range(6):
		if true_paths[j] is None:
			true_paths[j] = c[j]
	changes = True
	while changes:
		changes = False
		for j in range(6):
			for g in range(6):
				sum_x = turns[j][0] + turns[g][0]
				sum_y = turns[j][1] + turns[g][1]
				for k in range(6):
					if turns[k][0] == sum_x and turns[k][1] == sum_y:
						if true_paths[j] + true_paths[g] < true_paths[k]:
							true_paths[k] = true_paths[j] + true_paths[g]
							changes = True
	ans = 0
	if x > 0 and y > 0:
		if true_paths[0] < true_paths[1] + true_paths[5]:
			ans += min(x, y) * true_paths[0]
			min_a = min(x, y)
			x -= min_a
			y -= min_a
	elif x < 0 and y < 0:
		if true_paths[3] < true_paths[2] + true_paths[4]:
			ans += min(-x, -y) * true_paths[3]
			min_a = min(-x, -y)
			x += min_a
			y += min_a
	if x < 0:
		ans += true_paths[2] * -x
	if x > 0:
		ans += true_paths[5] * x
	if y < 0:
		ans += true_paths[4] * -y
	if y > 0:
		ans += true_paths[1] * y
	print(ans)


for _ in range(int(input())):
	(x, y) = list(map(int, input().split()))
	c = list(map(int, input().split()))
	for i in range(50):
		c[0] = min(c[0], c[5] + c[1])
		c[1] = min(c[1], c[0] + c[2])
		c[2] = min(c[2], c[1] + c[3])
		c[3] = min(c[3], c[2] + c[4])
		c[4] = min(c[4], c[3] + c[5])
		c[5] = min(c[5], c[4] + c[0])
	ans = 10 ** 30
	if x >= 0 and y >= 0:
		ans = min(ans, c[1] * y + c[5] * x)
		mn = min(x, y)
		cost = c[0] * mn
		x -= mn
		y -= mn
		if x >= 0:
			cost += c[5] * x
		if y >= 0:
			cost += c[1] * y
		ans = min(ans, cost)
	elif x <= 0 and y >= 0:
		x = abs(x)
		ans = min(ans, c[2] * x + c[1] * y)
	elif x <= 0 and y <= 0:
		x = abs(x)
		y = abs(y)
		ans = min(ans, y * c[4] + c[2] * x)
		mn = min(x, y)
		cost = mn * c[3]
		x -= mn
		y -= mn
		if x >= 0:
			cost += x * c[2]
		if y >= 0:
			cost += y * c[4]
		ans = min(ans, cost)
	else:
		y = abs(y)
		ans = min(ans, x * c[5] + c[4] * y)
	print(ans)


def main():
	for _ in range(int(input())):
		(x, y) = map(int, input().split())
		c = list(map(int, input().split()))
		r1 = abs(x) * c[5 if x >= 0 else 2] + abs(y) * c[1 if y >= 0 else 4]
		r2 = abs(x) * c[0 if x >= 0 else 3] + abs(y - x) * c[1 if y >= x else 4]
		r3 = abs(y) * c[0 if y >= 0 else 3] + abs(y - x) * c[5 if x >= y else 2]
		print(min([r1, r2, r3]))
main()