#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
const ll INF = (1<<31)-1;
ll sum[maxn];
ll n,w;
int main()
{
	while(scanf("%lld%lld",&n,&w)!=EOF)
	{
		memset(sum,0,sizeof(sum));
		ll MAX = -INF;
		ll MIN = INF;
		for(int i=1;i<=n;i++)
		{
			ll tmp;
			scanf("%lld",&tmp);
			sum[i]=tmp+sum[i-1];
			cout<<sum[i]<<" ";
		}
		cout<<endl;
		for(int i=1;i<=n;i++)
		{
			MAX = max(sum[i],MAX);
			MIN = min(sum[i],MIN);
		}
		if(MIN>=0 && MAX>=0)
		{
			if(w-MAX+1>0)
			{
				cout<<w-MAX+1<<endl;
			}
			else
			{
				cout<<0<<endl;
			}
		}
		else if(MIN<0 && MAX>=0)
		{
			if(w-MAX+1+MIN>0)
			{
				cout<<w-MAX+1+MIN<<endl;
			}
			else
			{
				cout<<0<<endl;
			}
		}
		else if(MIN<0 && MAX<0)
		{
			if(w+MIN>0)
			{
				cout<<w+MIN+1<<endl;
				break;
			}
			else if(MIN == MAX)
			{
				cout<<0<<endl;
				break;
			}
			else
			{
				cout<<0<<endl;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
const ll INF = (1<<31)-1;
ll sum[maxn];
ll n,w;
int main()
{
	while(scanf("%lld%lld",&n,&w)!=EOF)
	{
		memset(sum,0,sizeof(sum));
		ll MAX = -INF;
		ll MIN = INF;
		for(int i=1;i<=n;i++)
		{
			ll tmp;
			scanf("%lld",&tmp);
			sum[i]=tmp+sum[i-1];
			//cout<<sum[i]<<" ";
		}
		for(int i=1;i<=n;i++)
		{
			MAX = max(sum[i],MAX);
			MIN = min(sum[i],MIN);
		}
		if(MIN<=w-MAX)
		{
			cout<<w-MAX-MIN+1<<endl;
		}
        else
		{
			cout<<0<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
const ll INF = (1<<31)-1;
ll sum[maxn];
ll n,w;
int main()
{
	while(scanf("%lld%lld",&n,&w)!=EOF)
	{
		memset(sum,0,sizeof(sum));
		ll MAX = -INF;
		ll MIN = INF;
		for(int i=1;i<=n;i++)
		{
			ll tmp;
			scanf("%lld",&tmp);
			sum[i]=tmp+sum[i-1];
		}
		for(int i=1;i<=n;i++)
		{
			MAX = max(sum[i],MAX);
			MIN = min(sum[i],MIN);
		}
		if(MIN>=0 && MAX>=0)
		{
			if(w-MAX+1>0)
			{
				cout<<w-MAX+1<<endl;
			}
			else
			{
				cout<<0<<endl;
			}
		}
		else if(MIN<0 && MAX>=0)
		{
			if(w-MAX+1+MIN>0)
			{
				cout<<w-MAX+1+MIN<<endl;
			}
			else
			{
				cout<<0<<endl;
			}
		}
		else if(MIN<0 && MAX<0)
		{
			cout<<0<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
const ll INF = (1<<31)-1;
ll sum[maxn];
ll n,w;
int main()
{
	while(scanf("%lld%lld",&n,&w)!=EOF)
	{
		memset(sum,0,sizeof(sum));
		ll MAX = -INF;
		ll MIN = INF;
		for(int i=1;i<=n;i++)
		{
			ll tmp;
			scanf("%lld",&tmp);
			sum[i]=tmp+sum[i-1];
			//cout<<sum[i]<<" ";
		}
		cout<<endl;
		for(int i=1;i<=n;i++)
		{
			MAX = max(sum[i],MAX);
			MIN = min(sum[i],MIN);
		}
		if(MIN>=0 && MAX>=0)
		{
			if(w-MAX+1>0)
			{
				cout<<w-MAX+1<<endl;
			}
			else
			{
				cout<<0<<endl;
			}
		}
		else if(MIN<0 && MAX>=0)
		{
			if(w-MAX+1+MIN>0)
			{
				cout<<w-MAX+1+MIN<<endl;
			}
			else
			{
				cout<<0<<endl;
			}
		}
		else if(MIN<0 && MAX<0)
		{
			if(w+MIN>0)
			{
				cout<<w+MIN+1<<endl;
			}
			else
			{
				cout<<0<<endl;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
const ll INF = (1<<31)-1;
ll sum[maxn];
ll n,w;
int main()
{
	while(scanf("%lld%lld",&n,&w)!=EOF)
	{
		memset(sum,0,sizeof(sum));
		ll MAX = -INF;
		ll MIN = INF;
		for(int i=1;i<=n;i++)
		{
			ll tmp;
			scanf("%lld",&tmp);
			sum[i]=tmp+sum[i-1];
		}
		for(int i=1;i<=n;i++)
		{
			MAX = max(sum[i],MAX);
			MIN = min(sum[i],MIN);
		}
		if(MIN>=0 && MAX>=0)
		{
			cout<<w-MAX+1<<endl;
		}
		else if(MIN<0 && MAX>=0)
		{
			cout<<w-MAX+1+MIN<<endl;
		}
		else if(MIN<0 && MAX<0)
		{
			cout<<0<<endl;
		}
	}
	return 0;
}


@file:Suppress("NOTHING_TO_INLINE", "EXPERIMENTAL_FEATURE_WARNING", "OVERRIDE_BY_INLINE")
@file:OptIn(ExperimentalStdlibApi::class)

import java.io.PrintWriter
import kotlin.math.*
import kotlin.random.Random
import kotlin.collections.sort as _sort
import kotlin.collections.sortDescending as _sortDescending
import kotlin.io.println as iprintln

/** @author Spheniscine */
fun main() { _writer.solve(); _writer.flush() }
fun PrintWriter.solve() {
//    val startTime = System.nanoTime()

    val numCases = readInt()
    case@ for(case in 1..numCases) {
        //print("Case #$case: ")

        val x = readInt()
        val y = readInt()

        val c = readIntArray(6)

        var ans = Long.MAX_VALUE

        for(d1 in 0 until 5) for(d2 in d1+1 until 6) if(d2 - d1 != 3) {
            val M = arrayOf(
                intArrayOf(dx[d1], dx[d2], x),
                intArrayOf(dy[d1], dy[d2], y)
            )

            if(M[0][0] == 0) M.swap(0, 1)
            val m = -M[1][0] / M[0][0]
            for(j in 0 until 3) M[1][j] += m * M[0][j]

            val b = M[1][2] / M[1][1]
            if(b < 0) continue
            val a = (M[0][2] - b * M[0][1]) / M[0][0]
            if(a < 0) continue

            val res = a.toLong() * c[d1] + b.toLong() * c[d2]
            ans = min(ans, res)
        }

        println(ans)
    }

//    iprintln("Time: ${(System.nanoTime() - startTime) / 1000000} ms")
}

fun <E> Array<E>.swap(i: Int, j: Int) { val t = this[i]; this[i] = this[j]; this[j] = t }
inline fun IntArray.changeEach(f: (i: Int, v: Int) -> Int) { for(i in indices) this[i] = f(i, this[i]) }

val dx = intArrayOf(1, 0, -1, -1, 0, 1)
val dy = intArrayOf(1, 1, 0, -1, -1, 0)

/** IO */
//@JvmField val ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null
//const val PATH = "src/main/resources/"
//@JvmField val INPUT = File(PATH + "input.txt").inputStream()
//@JvmField val OUTPUT = File(PATH + "output.txt").outputStream()
@JvmField val INPUT = System.`in`
@JvmField val OUTPUT = System.out

const val _BUFFER_SIZE = 1 shl 16
@JvmField val _buffer = ByteArray(_BUFFER_SIZE)
@JvmField var _bufferPt = 0
@JvmField var _bytesRead = 0

tailrec fun readChar(): Char {
    if(_bufferPt == _bytesRead) {
        _bufferPt = 0
        _bytesRead = INPUT.read(_buffer, 0, _BUFFER_SIZE)
    }
    return if(_bytesRead < 0) Char.MIN_VALUE
    else {
        val c = _buffer[_bufferPt++].toChar()
        if (c == '\r') readChar() else c
    }
}

/** @param skipNext Whether to skip the next character (usually whitespace), defaults to true */
fun readCharArray(n: Int, skipNext: Boolean = true): CharArray {
    val res = CharArray(n) { readChar() }
    if(skipNext) readChar()
    return res
}

fun readLine(): String? {
    var c = readChar()
    return if(c == Char.MIN_VALUE) null
    else buildString {
        while(c != '\n' && c != Char.MIN_VALUE) {
            append(c)
            c = readChar()
        }
    }
}
fun readLn() = readLine()!!

fun read() = buildString {
    var c = readChar()
    while(c <= ' ') {
        if(c == Char.MIN_VALUE) return@buildString
        c = readChar()
    }
    do {
        append(c)
        c = readChar()
    } while(c > ' ')
}
fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readLines(n: Int) = List(n) { readLn() }
fun readInts(n: Int) = List(n) { read().toInt() }
fun readIntArray(n: Int) = IntArray(n) { read().toInt() }
fun readDoubles(n: Int) = List(n) { read().toDouble() }
fun readDoubleArray(n: Int) = DoubleArray(n) { read().toDouble() }
fun readLongs(n: Int) = List(n) { read().toLong() }
fun readLongArray(n: Int) = LongArray(n) { read().toLong() }

@JvmField val _writer = PrintWriter(OUTPUT, false)

/**  sort overrides to avoid quicksort attacks */

@JvmField var _random: Random? = null
val random get() = _random ?: Random(0x594E215C123 * System.nanoTime()).also { _random = it }

inline fun <A, T> _mergeSort(a0: A, n: Int, tmp0: A, get: A.(Int) -> T, set: A.(Int, T) -> Unit, cmp: (T, T) -> Int) {
    var a = a0
    var tmp = tmp0
    var len = 1
    while(len < n) {
        var l = 0
        while(true) {
            val m = l + len
            if(m >= n) break
            val r = min(n, m + len)
            var i = l
            var j = m
            for(k in l until r) {
                if(i != m && (j == r || cmp(a.get(i), a.get(j)) <= 0)) {
                    tmp.set(k, a.get(i++))
                } else tmp.set(k, a.get(j++))
            }
            l = r
        }
        for(i in l until n) tmp.set(i, a.get(i))
        val t = a; a = tmp; tmp = t
        len += len
    }
    if(a !== a0) for(i in 0 until n) a0.set(i, tmp0.get(i))
}

inline fun IntArray.sortWith(cmp: (Int, Int) -> Int) { _mergeSort(this, size, IntArray(size), IntArray::get, IntArray::set, cmp) }
inline fun <T: Comparable<T>> IntArray.sortBy(func: (Int) -> T) { sortWith { a, b -> func(a).compareTo(func(b)) } }
inline fun <T: Comparable<T>> IntArray.sortByDescending(func: (Int) -> T) { sortWith { a, b -> func(b).compareTo(func(a)) } }
fun IntArray.sort() { sortBy { it } }
fun IntArray.sortDescending() { sortByDescending { it } }

inline fun LongArray.sortWith(cmp: (Long, Long) -> Int) { _mergeSort(this, size, LongArray(size), LongArray::get, LongArray::set, cmp) }
inline fun <T: Comparable<T>> LongArray.sortBy(func: (Long) -> T) { sortWith { a, b -> func(a).compareTo(func(b)) } }
inline fun <T: Comparable<T>> LongArray.sortByDescending(func: (Long) -> T) { sortWith { a, b -> func(b).compareTo(func(a)) } }
fun LongArray.sort() { sortBy { it } }
fun LongArray.sortDescending() { sortByDescending { it } }

inline fun DoubleArray.sortWith(cmp: (Double, Double) -> Int) { _mergeSort(this, size, DoubleArray(size), DoubleArray::get, DoubleArray::set, cmp) }
inline fun <T: Comparable<T>> DoubleArray.sortBy(func: (Double) -> T) { sortWith { a, b -> func(a).compareTo(func(b)) } }
inline fun <T: Comparable<T>> DoubleArray.sortByDescending(func: (Double) -> T) { sortWith { a, b -> func(b).compareTo(func(a)) } }
fun DoubleArray.sort() { sortBy { it } }
fun DoubleArray.sortDescending() { sortByDescending { it } }

inline fun CharArray.sort() { _sort() }
inline fun CharArray.sortDescending() { _sortDescending() }

inline fun <T : Comparable<T>> Array<out T>.sort() = _sort()
inline fun <T : Comparable<T>> Array<out T>.sortDescending() = _sortDescending()
inline fun <T : Comparable<T>> MutableList<out T>.sort() = _sort()
inline fun <T : Comparable<T>> MutableList<out T>.sortDescending() = _sortDescending()

// import preserving junk function
@Suppress("NonAsciiCharacters") fun 雪花飄飄北風嘯嘯天地一片蒼茫() { iprintln(max(1, 2)) }

fun IntArray.sumLong() = sumOf { it.toLong() }

fun IntArray.sortedIndices() = IntArray(size) { it }.also { it.sortBy(::get) }
fun IntArray.sortedIndicesDescending() = IntArray(size) { it }.also { it.sortByDescending(::get) }
fun LongArray.sortedIndices() = IntArray(size) { it }.also { it.sortBy(::get) }
fun LongArray.sortedIndicesDescending() = IntArray(size) { it }.also { it.sortByDescending(::get) }
fun DoubleArray.sortedIndices() = IntArray(size) { it }.also { it.sortBy(::get) }
fun DoubleArray.sortedIndicesDescending() = IntArray(size) { it }.also { it.sortByDescending(::get) }
fun <T: Comparable<T>> Array<T>.sortedIndices() = IntArray(size) { it }.also { it.sortBy(::get) }
fun <T: Comparable<T>> Array<T>.sortedIndicesDescending() = IntArray(size) { it }.also { it.sortByDescending(::get) }
fun <T: Comparable<T>> List<T>.sortedIndices() = IntArray(size) { it }.also { it.sortBy(::get) }
fun <T: Comparable<T>> List<T>.sortedIndicesDescending() = IntArray(size) { it }.also { it.sortByDescending(::get) }

// max/min Kotlin 1.6 -> 1.4 shim
fun IntArray.max() = maxOf { it }
fun IntArray.min() = minOf { it }
fun LongArray.max() = maxOf { it }
fun LongArray.min() = minOf { it }
fun CharArray.max() = maxOf { it }
fun CharArray.min() = minOf { it }
fun <T : Comparable<T>> Iterable<T>.max() = maxOf { it }
fun <T : Comparable<T>> Iterable<T>.min() = minOf { it }
fun <T : Comparable<T>> Sequence<T>.max() = maxOf { it }
fun <T : Comparable<T>> Sequence<T>.min() = minOf { it }


import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.math.max
import kotlin.math.min

fun main() {
    val jin = BufferedReader(InputStreamReader(System.`in`))
    for (c in 1..jin.readLine().toInt()) {
        val (x, y) = jin.readLine().split(" ").map { it.toLong() }
        val costs = jin.readLine().split(" ").map { it.toLong() }.toLongArray()
        for (j in 0..5) {
            costs[j] = min(costs[j], costs[(j + 1) % 6] + costs[(j + 5) % 6])
        }
        var answer = max(y * costs[1], -y * costs[4]) + max(x * costs[5], -x * costs[2])
        answer = min(answer, max(x * costs[0], -x * costs[3]) + max((y - x) * costs[1], (x - y) * costs[4]))
        answer = min(answer, max((x - y) * costs[5], (y - x) * costs[2]) + max(y * costs[0], -y * costs[3]))
        println(answer)
    }
}


import kotlin.math.abs
import kotlin.math.min

fun readInt() = readLine()!!.toInt()
fun readLongs() = readLine()!!.split(' ').map { it.toLong() }

fun main() {
    repeat(readInt()) {
        val (x, y) = readLongs()
        val (c1, c2, c3, c4, c5, c6) = readLongs()
        val m = min(abs(x), abs(y))
        val best = when {
            x >= 0 && y >= 0 -> min(c1*m + (y - m)*c2 + (x - m)*c6, c2*y + c6*x)
            x <= 0 && y <= 0 -> min(c4*m - (y - m)*c5 - (x - m)*c3, -c5*y - c3*x)
            x >= 0 && y <= 0 -> x*c6 - y*c5
            x <= 0 && y >= 0 -> -x*c3 + y*c2
            else -> throw Error("Huh?")
        }

        println(best)
    }
}

private operator fun <E> List<E>.component6(): E = this[5];


import kotlin.math.min

fun readInt() = readLine()!!.toInt()
fun readLongs() = readLine()!!.split(' ').map { it.toLong() }

fun main() {
    repeat(readInt()) {
        val (x, y) = readLongs()
        val (c1, c2, c3, c4, c5, c6) = readLongs()

        fun goX(v: Long) = if (v >= 0) v*c6 else -v*c3
        fun goY(v: Long) = if (v >= 0) v*c2 else -v*c5

        var best = goX(x) + goY(y)
        best = min(best, if (x >= 0) c1*x + goY(y-x) else -c4*x + goY(y-x))
        best = min(best, if (y >= 0) c1*y + goX(x-y) else -c4*y + goX(x-y))

        println(best)
    }
}

private operator fun <E> List<E>.component6(): E = get(5)


import java.util.Scanner;

public class D {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        long[] c = new long[6];
        long[] c_new = new long[6];
        while(T-->0) {
            long y = in.nextLong();
            long x = in.nextLong();
            for(int i=0;i<6;i++) {
                c_new[i] = c[i] = in.nextLong();
            }

            c_new[0] = Math.min(c[0], c[1] + c[5]);
            c_new[3] = Math.min(c[3], c[2] + c[4]);

            c_new[1] = Math.min(c[1], c[0] + c[2]);
            c_new[4] = Math.min(c[4], c[3] + c[5]);

            c_new[2] = Math.min(c[2], c[1] + c[3]);
            c_new[5] = Math.min(c[5], c[0] + c[4]);

//            for(int i=0;i<6;i++) {
//                System.out.println(c_new[i]);
//            }

            long ans = 0;
            if(x*y <= 0) {
                //System.out.println("-->" + ans);
                if(x >= 0) {
                    ans += c_new[1] * x;
                    //System.out.println("-->" + ans);
                }
                else {
                    ans += c_new[4] * -x;
                }
                if(y >= 0) {
                    ans += c_new[5] * y;
                    //System.out.println("-->" + ans);
                }
                else {
                    ans += c_new[2] * -y;
                    //System.out.println("-->" + ans);
                }
                //System.out.println("-->" + ans);
            }
            else if(x>0) {
                ans += c_new[0] * Math.min(x ,y);
                if(y>x) {
                    ans += c_new[5] * (y-x);
                } else {
                    ans += c_new[1] * (x-y);
                }
            }
            else {
                x = -x;
                y = -y;
                ans += c_new[3] * Math.min(x ,y);
                if(y>x) {
                    ans += c_new[2] * (y-x);
                } else {
                    ans += c_new[4] * (x-y);
                }
            }
            System.out.println(ans);
        }
    }
}


import java.util.*;
import java.io.*;

public class Main
{
    public static long MIN(long a,long b)
    {
        if(a<b)
            return a;
        return b;
    }
    public static int MAX(int a,int b)
    {
        if(a>b)
            return a;
        return b;
    }
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = Integer.parseInt(br.readLine());
        while(t-->0)
        {
            String[] inp = br.readLine().split(" ");
            long x = Long.parseLong(inp[0]);
            long y = Long.parseLong(inp[1]);
            String[] inp2 = br.readLine().split(" ");
            long[] c = new long[6];
            for(int i=0;i<6;i++)
                c[i] = Long.parseLong(inp2[i]);
            long k = x-y;
            long c1=0;
            c1+=Math.abs(k*c[1]);
            if(x<0)
                c1+=Math.abs(x*c[3]);
            else
                c1+=x*c[0];
            long c2 = 0;
            if(y>0)
                c2+=y*c[1];
            else
                c2+=Math.abs(y*c[4]);
            if(x<0)
                c2+=Math.abs(x*c[2]);
            else
                c2+=x*c[5];
            long c3=0;
            c3+=Math.abs(k*c[2]);
            if(k<x)
                c3+=(x-k)*c[0];
            else
                c3+=(k-x)*c[3];
            long res = 0;
            res = MIN(MIN(c1,c2),c3);
            bw.write(res+"\n");
        }
        bw.flush();
    }
}
class pair{
    int x,y;
    pair(int x,int y)
    {
        this.x = x;
        this.y = y;
    }
}


import java.util.*;
import java.io.*;

public class Main
{
    public static long MIN(long a,long b)
    {
        if(a<b)
            return a;
        return b;
    }
    public static int MAX(int a,int b)
    {
        if(a>b)
            return a;
        return b;
    }
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = Integer.parseInt(br.readLine());
        while(t-->0)
        {
            String[] inp = br.readLine().split(" ");
            long x = Long.parseLong(inp[0]);
            long y = Long.parseLong(inp[1]);
            String[] inp2 = br.readLine().split(" ");
            long[] c = new long[6];
            for(int i=0;i<6;i++)
                c[i] = Long.parseLong(inp2[i]);
            long k = x-y;
            long c1=0;
            c1+=Math.abs(k*c[1]);
            if(x<0)
                c1+=Math.abs(x*c[3]);
            else
                c1+=x*c[0];
            long c2 = 0;
            if(y>0)
                c2+=y*c[1];
            else
                c2+=Math.abs(y*c[4]);
            if(x<0)
                c2+=Math.abs(x*c[2]);
            else
                c2+=x*c[5];
            long c3=0;
            if(k<0)
                c3+=Math.abs(k*c[2]);
            else
                c3+=k*c[5];
            if(k<x)
                c3+=(x-k)*c[0];
            else
                c3+=(k-x)*c[3];
            long res = 0;
            res = MIN(MIN(c1,c2),c3);
            bw.write(res+"\n");
        }
        bw.flush();
    }
}
class pair{
    int x,y;
    pair(int x,int y)
    {
        this.x = x;
        this.y = y;
    }
}


import java.util.*;
import java.io.*;

public class Main
{
    public static long MIN(long a,long b)
    {
        if(a<b)
            return a;
        return b;
    }
    public static int MAX(int a,int b)
    {
        if(a>b)
            return a;
        return b;
    }
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = Integer.parseInt(br.readLine());
        while(t-->0)
        {
            String[] inp = br.readLine().split(" ");
            int x = Integer.parseInt(inp[0]);
            int y = Integer.parseInt(inp[1]);
            String[] inp2 = br.readLine().split(" ");
            long[] c = new long[6];
            for(int i=0;i<6;i++)
                c[i] = Long.parseLong(inp2[i]);
            long k = x-y;
            long c1=0;
            c1+=Math.abs(k*c[1]);
            if(x<0)
                c1+=Math.abs(x*c[3]);
            else
                c1+=x*c[0];
            long c2 = 0;
            if(y>0)
                c2+=y*c[1];
            else
                c2+=Math.abs(y*c[4]);
            if(x<0)
                c2+=Math.abs(x*c[2]);
            else
                c2+=x*c[0];
            long c3=0;
            c3+=Math.abs(k*c[2]);
            if(k<x)
                c3+=(x-k)*c[0];
            else
                c3+=(k-x)*c[3];
            long res = 0;
            res = MIN(MIN(c1,c2),c3);
            bw.write(res+"\n");
        }
        bw.flush();
    }
}
class pair{
    int x,y;
    pair(int x,int y)
    {
        this.x = x;
        this.y = y;
    }
}


import java.util.*;
import java.io.*;

public class Main
{
    public static long MIN(long a,long b)
    {
        if(a<b)
            return a;
        return b;
    }
    public static int MAX(int a,int b)
    {
        if(a>b)
            return a;
        return b;
    }
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = Integer.parseInt(br.readLine());
        while(t-->0)
        {
            String[] inp = br.readLine().split(" ");
            long x = Long.parseLong(inp[0]);
            long y = Long.parseLong(inp[1]);
            String[] inp2 = br.readLine().split(" ");
            long[] c = new long[6];
            for(int i=0;i<6;i++)
                c[i] = Long.parseLong(inp2[i]);
            long k = x-y;
            long c1=0;
            if(k<0)
                c1+=Math.abs(k*c[1]);
            else
                c1+=k*c[4];
            if(x<0)
                c1+=Math.abs(x*c[3]);
            else
                c1+=x*c[0];
            long c2 = 0;
            if(y>0)
                c2+=y*c[1];
            else
                c2+=Math.abs(y*c[4]);
            if(x<0)
                c2+=Math.abs(x*c[2]);
            else
                c2+=x*c[5];
            long c3=0;
            if(k<0)
                c3+=Math.abs(k*c[2]);
            else
                c3+=k*c[5];
            if(k<x)
                c3+=(x-k)*c[0];
            else
                c3+=(k-x)*c[3];
            long res = 0;
            res = MIN(MIN(c1,c2),c3);
            bw.write(res+"\n");
        }
        bw.flush();
    }
}
class pair{
    int x,y;
    pair(int x,int y)
    {
        this.x = x;
        this.y = y;
    }
}


import java.util.*;
public class cf676_2_4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-- > 0) {
			long x = sc.nextLong(),y= sc.nextLong();
			int c[] = new int[6];
			for(int i=0;i<6;i++) {
				c[i] = sc.nextInt();
			}
			long ans=0;
			if(x>=0 && y<=0) {
				y=y*(-1);
				ans += x*Math.min(c[5],c[0]+c[4]);
				ans += y*Math.min(c[4],c[5]+c[3]);
			}
			else if(x<=0 && y>=0) {
				x=x*(-1);
				ans += x*Math.min(c[2], c[1]+c[3]);
				ans += y*Math.min(c[1], c[0]+c[2]);
			}
			else if(x>=0 && y>=0) {
				long min = Math.min(x,y);
				ans += min*Math.min(c[0], c[1]+c[5]);
				x -= min;
				y -= min;
				if(y!=0) {
					ans += y*Math.min(c[1], c[2]+c[0]);
				}
				if(x!=0) {
					ans += x*Math.min(c[5], c[0]+c[4]);
				}
			}
			else if(x<=0 && y<=0) {
				x = x*(-1);
				y = y*(-1);
				long min = Math.min(x,y);
				ans += min*Math.min(c[3], c[4]+c[2]);
				x -= min;
				y -= min;
				if(y!=0) {
					ans += y*Math.min(c[4], c[5]+c[3]);
				}
				if(x!=0) {
					ans += x*Math.min(c[2], c[3]+c[1]);
				}
			}
			System.out.println(ans);
		}
		sc.close();
	}

}


import java.util.*;
public class cf676_2_4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-- > 0) {
			long x = sc.nextLong(),y= sc.nextLong();
			int c[] = new int[6];
			for(int i=0;i<6;i++) {
				c[i] = sc.nextInt();
			}
			long ans=0;
			if(x>=0 && y<=0) {
				y=y*(-1);
				ans += x*Math.min(c[5],c[0]+c[4]);
				ans += y*Math.min(c[4],c[5]+c[3]);
			}
			else if(x<=0 && y>=0) {
				x=x*(-1);
				ans += x*Math.min(c[2], c[1]+c[3]);
				ans += y*Math.min(c[1], c[0]+c[2]);
			}
			else if(x>=0 && y>=0) {
				long min = Math.min(x,y);
				ans += min*Math.min(c[0], c[1]+c[5]);
				x -= min;
				y -= min;
				if(y!=0) {
					ans += y*Math.min(c[1], c[2]+c[3]);
				}
				if(x!=0) {
					ans += x*Math.min(c[5], c[0]+c[4]);
				}
			}
			else if(x<=0 && y<=0) {
				x = x*(-1);
				y = y*(-1);
				long min = Math.min(x,y);
				ans += min*Math.min(c[3], c[4]+c[2]);
				x -= min;
				y -= min;
				if(y!=0) {
					ans += y*Math.min(c[4], c[5]+c[3]);
				}
				if(x!=0) {
					ans += x*Math.min(c[2], c[3]+c[1]);
				}
			}
			System.out.println(ans);
		}
		sc.close();
	}

}


// package com.company;
import java.util.*;
import java.lang.*;
import java.io.*;
//****Use Integer Wrapper Class for Arrays.sort()****
public class FR4 {
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    public static void main(String[] Args)throws Exception{
        FastReader scan=new FastReader(System.in);
        int t=1;
        t=scan.nextInt();
        while(t-->0){
            long x=scan.nextInt();
            long y=scan.nextInt();
            long[] c=new long[6];
            for(int i=0;i<6;i++){
                c[i]=scan.nextInt();
            }
            long ans=Long.MAX_VALUE;
            if(x>=0&&y>=0){
                long tx=x;
                long ty=y;
                long temp=0;
                temp=c[0]*Math.min(tx,ty);
                long min=Math.min(tx,ty);
                long max=Math.max(tx,ty);
                tx-=min;
                ty-=min;
                temp+=tx*c[5];
                temp+=ty*c[1];
                ans=Math.min(temp,ans);
                temp=0;
                tx=x;
                ty=y;
                ans=Math.min(tx*c[5]+ty*c[1],ans);
                temp+=max*c[0];
                tx=Math.abs(tx-max);
                ty=Math.abs(ty-max);
                temp+=tx*c[2];
                temp+=ty*c[4];
                ans=Math.min(ans,temp);
            }
            if(x<=0&&y<=0){
                long tx=Math.abs(x);
                long ty=Math.abs(y);
                long temp=0;
                temp=c[3]*Math.min(tx,ty);
                long min=Math.min(tx,ty);
                long max=Math.max(tx,ty);
                tx-=min;
                ty-=min;
                temp+=tx*c[2];
                temp+=ty*c[4];
                ans=Math.min(temp,ans);
                temp=0;
                tx=Math.abs(x);
                ty=Math.abs(y);
                ans=Math.min(tx*c[2]+ty*c[4],ans);
                temp+=max*c[3];
                tx=Math.abs(tx-max);
                ty=Math.abs(ty-max);
                temp+=tx*c[5];
                temp+=ty*c[1];
                ans=Math.min(ans,temp);
            }
            if(x>=0&&y<=0){
                long tx=x;
                long ty=Math.abs(y);
                long temp=0;
                temp=tx*c[0];
                ty+=tx;
                temp+=ty*c[4];
                ans=Math.min(ans,temp);
                tx=x;
                ty=Math.abs(y);
                temp=0;
                ans=Math.min(ans,tx*c[5]+ty*c[4]);
                temp+=ty*c[3];
                tx+=ty;
                temp+=c[5]*tx;
                ans=Math.min(ans,temp);
            }if(x<=0&&y>=0){
                long tx=Math.abs(x);
                long ty=y;
                long temp=0;
                temp=ty*c[0];
                tx+=ty;
                temp+=tx*c[2];
                ans=Math.min(ans,temp);
                tx=Math.abs(x);
                ty=y;
                temp=0;
                ans=Math.min(ans,tx*c[2]+ty*c[1]);
                temp+=tx*c[3];
                ty+=tx;
                temp+=c[1]*ty;
                ans=Math.min(ans,temp);
            }
            out.println(ans);
        }
        out.flush();
        out.close();
    }
    static class FastReader {

        byte[] buf = new byte[2048];
        int index, total;
        InputStream in;

        FastReader(InputStream is) {
            in = is;
        }

        int scan() throws IOException {
            if (index >= total) {
                index = 0;
                total = in.read(buf);
                if (total <= 0) {
                    return -1;
                }
            }
            return buf[index++];
        }

        String next() throws IOException {
            int c;
            for (c = scan(); c <= 32; c = scan()) ;
            StringBuilder sb = new StringBuilder();
            for (; c > 32; c = scan()) {
                sb.append((char) c);
            }
            return sb.toString();
        }

        int nextInt() throws IOException {
            int c, val = 0;
            for (c = scan(); c <= 32; c = scan()) ;
            boolean neg = c == '-';
            if (c == '-' || c == '+') {
                c = scan();
            }
            for (; c >= '0' && c <= '9'; c = scan()) {
                val = (val << 3) + (val << 1) + (c & 15);
            }
            return neg ? -val : val;
        }
        long nextLong() throws IOException {
            int c;
            long val = 0;
            for (c = scan(); c <= 32; c = scan()) ;
            boolean neg = c == '-';
            if (c == '-' || c == '+') {
                c = scan();
            }
            for (; c >= '0' && c <= '9'; c = scan()) {
                val = (val << 3) + (val << 1) + (c & 15);
            }
            return neg ? -val : val;
        }
    }
}


import java.io.*;
import java.util.*;

/*
polyakoff
*/

public class Main {

    static FastReader in;
    static PrintWriter out;
    static Random rand = new Random();
    static final int oo = (int) 1e9 + 10;
    static final long OO = (long) 2e18 + 20;
    static final int MOD = (int) 1e9 + 7;

    static void solve() {
        long x = in.nextInt();
        long y = in.nextInt();

        long[] c = new long[6];
        for (int i = 0; i < 6; i++) {
            c[i] = in.nextInt();
        }

        c[0] = Math.min(c[0], c[1] + c[5]);
        c[1] = Math.min(c[1], c[0] + c[2]);
        c[2] = Math.min(c[2], c[1] + c[3]);
        c[3] = Math.min(c[3], c[2] + c[4]);
        c[4] = Math.min(c[4], c[3] + c[5]);
        c[5] = Math.min(c[5], c[4] + c[0]);

        long ans = 0;
        if (x >= 0 && y >= 0) {
            ans = Math.min(x, y) * c[0];
            if (x > y) {
                ans += (x - y) * c[5];
            } else {
                ans += (y - x) * c[1];
            }
        } else if (x <= 0 && y >= 0) {
            ans = (-x) * c[2] + y * c[1];
        } else if (x <= 0 && y <= 0) {
            ans = Math.min(-x, -y) * c[3];
            if (x < y) {
                ans += (y - x) * c[2];
            } else {
                ans += (x - y) * c[4];
            }
        } else if (x >= 0 && y <= 0) {
            ans = x * c[5] + (-y) * c[4];
        }

        out.println(ans);

    }

    public static void main(String[] args) {
        in = new FastReader();
        out = new PrintWriter(System.out);
//        fileInputOutput();

        int T = 1;
        T = in.nextInt();
        while (T-- > 0)
            solve();

        out.flush();
        out.close();
    }

    static void fileInputOutput() {
        try {
            in = new FastReader("input.txt");
            out = new PrintWriter(new FileOutputStream("output.txt"));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    static void runInThread() {
        Thread thread = new Thread(null, () -> {
            int T = 1;
//            T = in.nextInt();
            while (T-- > 0)
                solve();
        }, "thread1", 1 << 28);
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        FastReader() {
            this(System.in);
        }
        FastReader(String file) throws FileNotFoundException {
            this(new FileInputStream(file));
        }
        FastReader(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String next() {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(nextLine());
            }
            return st.nextToken();
        }
        String nextLine() {
            String line;
            try {
                line = br.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return line;
        }
    }
}


import java.util.*;
import java.io.*;
import java.math.*;

public class Main {
    static PrintWriter out;
    static Reader in;
    public static void main(String[] args) throws IOException {
        //out = new PrintWriter(new File("out.txt"));
        //PrintWriter out = new PrintWriter(System.out);
        //in = new Reader(new FileInputStream("in.txt"));
        //Reader in = new Reader();
        input_output();
        Main solver = new Main();
        solver.solve();
        out.flush();
        out.close();

    }

    static int INF = (int)1e9-1;
    static int maxn = (int)1e5+5;
    static int mod = 998_244_353;
    static int n, m, q, k, t;

    void solve() throws IOException{
        t = in.nextInt();

        while (t --> 0) {
            int x = in.nextInt(),
                y = in.nextInt();

            long[][] cost = new long[3][3];
            cost[2][2] = in.nextLong();
            cost[1][2] = in.nextLong();
            cost[0][1] = in.nextLong();
            cost[0][0] = in.nextLong();
            cost[1][0] = in.nextLong();
            cost[2][1] = in.nextLong();

            cost[2][2] = Math.min(cost[2][2], cost[2][1]+cost[1][2]);
            cost[0][0] = Math.min(cost[0][0], cost[0][1]+cost[1][0]);

            cost[2][1] = Math.min(cost[2][1], cost[2][2]+cost[1][0]);
            cost[1][0] = Math.min(cost[1][0], cost[0][0]+cost[2][1]);
            cost[2][1] = Math.min(cost[2][1], cost[2][2]+cost[1][0]);
            cost[1][0] = Math.min(cost[1][0], cost[0][0]+cost[2][1]);

            cost[1][2] = Math.min(cost[1][2], cost[2][2]+cost[0][1]);
            cost[0][1] = Math.min(cost[0][1], cost[0][0]+cost[1][2]);
            cost[1][2] = Math.min(cost[1][2], cost[2][2]+cost[0][1]);
            cost[0][1] = Math.min(cost[0][1], cost[0][0]+cost[1][2]);

            //repeat.
            cost[2][2] = Math.min(cost[2][2], cost[2][1]+cost[1][2]);
            cost[0][0] = Math.min(cost[0][0], cost[0][1]+cost[1][0]);

            cost[2][1] = Math.min(cost[2][1], cost[2][2]+cost[1][0]);
            cost[1][0] = Math.min(cost[1][0], cost[0][0]+cost[2][1]);
            cost[2][1] = Math.min(cost[2][1], cost[2][2]+cost[1][0]);
            cost[1][0] = Math.min(cost[1][0], cost[0][0]+cost[2][1]);

            cost[1][2] = Math.min(cost[1][2], cost[2][2]+cost[0][1]);
            cost[0][1] = Math.min(cost[0][1], cost[0][0]+cost[1][2]);
            cost[1][2] = Math.min(cost[1][2], cost[2][2]+cost[0][1]);
            cost[0][1] = Math.min(cost[0][1], cost[0][0]+cost[1][2]);

            //repeat.
            cost[2][2] = Math.min(cost[2][2], cost[2][1]+cost[1][2]);
            cost[0][0] = Math.min(cost[0][0], cost[0][1]+cost[1][0]);

            cost[2][1] = Math.min(cost[2][1], cost[2][2]+cost[1][0]);
            cost[1][0] = Math.min(cost[1][0], cost[0][0]+cost[2][1]);
            cost[2][1] = Math.min(cost[2][1], cost[2][2]+cost[1][0]);
            cost[1][0] = Math.min(cost[1][0], cost[0][0]+cost[2][1]);

            cost[1][2] = Math.min(cost[1][2], cost[2][2]+cost[0][1]);
            cost[0][1] = Math.min(cost[0][1], cost[0][0]+cost[1][2]);
            cost[1][2] = Math.min(cost[1][2], cost[2][2]+cost[0][1]);
            cost[0][1] = Math.min(cost[0][1], cost[0][0]+cost[1][2]);

            long ans = 0;
            int min = Math.min(Math.abs(x), Math.abs(y));
            if (x > 0 && y > 0) {
                ans += min*cost[2][2];
                x-=min;
                y-=min;
            } else if (x < 0 && y < 0) {
                ans += min*cost[0][0];
                x+=min;
                y+=min;
            }
            if (x < 0) ans += cost[0][1]*Math.abs(x);
            else ans += cost[2][1]*x;
            if (y < 0) ans += cost[1][0]*Math.abs(y);
            else ans += cost[1][2]*y;
            out.println(ans);
        }
    }

    //<>

    static class Reader {

        private InputStream mIs;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public Reader() {
            this(System.in);
        }

        public Reader(InputStream is) {
            mIs = is;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();

        }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = mIs.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }
    static void input_output() throws IOException {
        File f = new File("in.txt");
        if(f.exists() && !f.isDirectory()) {
            in = new Reader(new FileInputStream("in.txt"));
        } else in = new Reader();
        f = new File("out.txt");
        if(f.exists() && !f.isDirectory()) {
            out = new PrintWriter(new File("out.txt"));
        } else out = new PrintWriter(System.out);
    }
}


	import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
import java.util.StringTokenizer;

	public class COVID {

		static long mod = (int) 1e9 + 7;
		static PrintWriter pw;
			static int n,m;

			static ArrayList<Integer>[] adjList;

	static int INF=(int)1e8;
	public static void main(String[] args) throws Exception {
				Scanner sc = new Scanner(System.in);
				PrintWriter pw = new PrintWriter(System.out);
				int t=sc.nextInt();
				while(t-->0) {
					long x=sc.nextLong(),y=sc.nextLong();
					long c[]=sc.nextLongArr(6);

					for(int i=0;i<c.length;i++) {
						c[i]=Math.min(c[i], c[(i+1)%c.length]+c[(i-1+c.length)%c.length]);
					}

//					pw.println(Arrays.toString(c));
					long ans=0;
					if(x>=0) {
						ans+=(x*c[5]);
					}else {
						ans+=-(x*c[2]);
					}
					if(y>=0) {
						ans+=y*c[1];
					}else {
						ans+=-y*c[4];
					}

					if(x>=0) {
					long firstOpt=x*c[0];
					if(y>=x) {
						firstOpt+=(Math.abs(y-x)*c[1]);
					}else {
						firstOpt+=(Math.abs(y-x)*c[4]);

					}

					long secOpt=x*c[5];
					if(y>=0)
						secOpt+=Math.abs(y)*c[1];
					else

						secOpt+=Math.abs(y)*c[4];
					ans=Math.min(firstOpt, secOpt);
					}else {
							long firstOpt=-x*c[3];
							if(y>=x) {
								firstOpt+=Math.abs(y-x)*c[1];
							}else {
								firstOpt+=Math.abs(y-x)*c[4];
							}

							long secOpt=-x*c[2];
							if(y>=0)secOpt+=Math.abs(y)*c[1];
							else secOpt+=Math.abs(y)*c[4];
							ans=Math.min(firstOpt,secOpt);
					}
					pw.println(ans);

				}

				pw.flush();
			}

			static class longPair implements Comparable<longPair> {
			long x, y;

			public longPair(long a, long b) {
				x = a;
				y = b;
			}

			public int compareTo(longPair p) {
				return (p.x == x) ? ((p.y == y) ? 0 : (y > p.y) ? 1 : -1) : (x > p.x) ? 1 : -1;
			}
		}

		static class Pair implements Comparable<Pair> {
			int x;
			int y;

			public Pair(int a, int b) {
				this.x = a;
				y = b;
			}

			public int compareTo(Pair o) {
				return (x == o.x) ? ((y > o.y) ? 1 : (y == o.y) ? 0 : -1) : ((x > o.x) ? 1 : -1);
			}

			@Override

			public int hashCode() {
				final int prime = 31;
				int result = 1;
				result = prime * result + x;
				result = prime * result + y;
				return result;
			}

			@Override

			public boolean equals(Object obj) {
				if (this == obj)
					return true;
				if (obj == null)
					return false;
				if (getClass() != obj.getClass())
					return false;
				Pair other = (Pair) obj;
				if (x != other.x)
					return false;
				if (y != other.y)
					return false;
				return true;
			}

		}

		static class Scanner {
			StringTokenizer st;
			BufferedReader br;

			public Scanner(InputStream s) {
				br = new BufferedReader(new InputStreamReader(s));
			}

			public Scanner(String s) throws FileNotFoundException {
				br = new BufferedReader(new FileReader(s));
			}

			public long[] nextLongArr(int n) throws IOException {
				long[] arr = new long[n];
				for (int i = 0; i < n; i++)
					arr[i] = nextLong();
				return arr;
			}

			public int[] nextIntArr(int n) throws IOException {
				int[] arr = new int[n];
				for (int i = 0; i < n; i++)
					arr[i] = nextInt();
				return arr;
			}

			public String next() throws IOException {
				while (st == null || !st.hasMoreTokens())
					st = new StringTokenizer(br.readLine(), " ,");
				return st.nextToken();
			}

			public int nextInt() throws IOException {
				return Integer.parseInt(next());
			}

			public long nextLong() throws IOException {
				return Long.parseLong(next());
			}

			public String nextLine() throws IOException {
				return br.readLine();
			}

			public double nextDouble() throws IOException {
				String x = next();
				StringBuilder sb = new StringBuilder("0");
				double res = 0, f = 1;
				boolean dec = false, neg = false;
				int start = 0;
				if (x.charAt(0) == '-') {
					neg = true;
					start++;
				}
				for (int i = start; i < x.length(); i++) {
					if (x.charAt(i) == '.') {
						res = Long.parseLong(sb.toString());
						sb = new StringBuilder("0");
						dec = true;
					} else {
						sb.append(x.charAt(i));
						if (dec)
							f *= 10;
					}
					if (sb.length() == 18) {
						res += Long.parseLong(sb.toString()) / f;
						sb = new StringBuilder("0");
					}
				}
				res += Long.parseLong(sb.toString()) / f;
				return res * (neg ? -1 : 1);
			}

			public boolean ready() throws IOException {
				return br.ready();
			}

		}

		public static void shuffle(int[] a) {
			int n = a.length;
			for (int i = 0; i < n; i++) {
				int r = i + (int) (Math.random() * (n - i));
				int tmp = a[i];
				a[i] = a[r];
				a[r] = tmp;
			}
		}

	}


	import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
import java.util.StringTokenizer;

	public class COVID {

		static long mod = (int) 1e9 + 7;
		static PrintWriter pw;
			static int n,m;

			static ArrayList<Integer>[] adjList;

	static int INF=(int)1e8;
	public static void main(String[] args) throws Exception {
				Scanner sc = new Scanner(System.in);
				PrintWriter pw = new PrintWriter(System.out);
				int t=sc.nextInt();
				while(t-->0) {
					long x=sc.nextLong(),y=sc.nextLong();
					long c[]=sc.nextLongArr(6);

					for(int i=0;i<c.length;i++) {
						c[i]=Math.min(c[i], c[(i+1)%c.length]+c[(i-1+c.length)%c.length]);
					}

//					pw.println(Arrays.toString(c));
					long ans=0;

					 ans=(long)1e18;
					if(x>=0) {
						if(y>=0) {
						long d=Math.min(x, y);
						long firstOpt=d*c[0];
						firstOpt+=(y-d)*c[1];
						firstOpt+=(x-d)*c[5];
						ans= firstOpt;
						}else {
							ans= x*c[5]-y*c[4];
						}

						}else {
							if(y>=0) {
								ans=-x*c[2]+y*c[1];
							}else {
								y*=-1;
								x*=-1;
								long d=Math.min(x, y);
								ans=c[3]*d;
								ans+=(y-d)*c[4];
								ans+=(x-d)*c[2];
							}
						}

					pw.println(ans);

				}

				pw.flush();
			}

			static class longPair implements Comparable<longPair> {
			long x, y;

			public longPair(long a, long b) {
				x = a;
				y = b;
			}

			public int compareTo(longPair p) {
				return (p.x == x) ? ((p.y == y) ? 0 : (y > p.y) ? 1 : -1) : (x > p.x) ? 1 : -1;
			}
		}

		static class Pair implements Comparable<Pair> {
			int x;
			int y;

			public Pair(int a, int b) {
				this.x = a;
				y = b;
			}

			public int compareTo(Pair o) {
				return (x == o.x) ? ((y > o.y) ? 1 : (y == o.y) ? 0 : -1) : ((x > o.x) ? 1 : -1);
			}

			@Override

			public int hashCode() {
				final int prime = 31;
				int result = 1;
				result = prime * result + x;
				result = prime * result + y;
				return result;
			}

			@Override

			public boolean equals(Object obj) {
				if (this == obj)
					return true;
				if (obj == null)
					return false;
				if (getClass() != obj.getClass())
					return false;
				Pair other = (Pair) obj;
				if (x != other.x)
					return false;
				if (y != other.y)
					return false;
				return true;
			}

		}

		static class Scanner {
			StringTokenizer st;
			BufferedReader br;

			public Scanner(InputStream s) {
				br = new BufferedReader(new InputStreamReader(s));
			}

			public Scanner(String s) throws FileNotFoundException {
				br = new BufferedReader(new FileReader(s));
			}

			public long[] nextLongArr(int n) throws IOException {
				long[] arr = new long[n];
				for (int i = 0; i < n; i++)
					arr[i] = nextLong();
				return arr;
			}

			public int[] nextIntArr(int n) throws IOException {
				int[] arr = new int[n];
				for (int i = 0; i < n; i++)
					arr[i] = nextInt();
				return arr;
			}

			public String next() throws IOException {
				while (st == null || !st.hasMoreTokens())
					st = new StringTokenizer(br.readLine(), " ,");
				return st.nextToken();
			}

			public int nextInt() throws IOException {
				return Integer.parseInt(next());
			}

			public long nextLong() throws IOException {
				return Long.parseLong(next());
			}

			public String nextLine() throws IOException {
				return br.readLine();
			}

			public double nextDouble() throws IOException {
				String x = next();
				StringBuilder sb = new StringBuilder("0");
				double res = 0, f = 1;
				boolean dec = false, neg = false;
				int start = 0;
				if (x.charAt(0) == '-') {
					neg = true;
					start++;
				}
				for (int i = start; i < x.length(); i++) {
					if (x.charAt(i) == '.') {
						res = Long.parseLong(sb.toString());
						sb = new StringBuilder("0");
						dec = true;
					} else {
						sb.append(x.charAt(i));
						if (dec)
							f *= 10;
					}
					if (sb.length() == 18) {
						res += Long.parseLong(sb.toString()) / f;
						sb = new StringBuilder("0");
					}
				}
				res += Long.parseLong(sb.toString()) / f;
				return res * (neg ? -1 : 1);
			}

			public boolean ready() throws IOException {
				return br.ready();
			}

		}

		public static void shuffle(int[] a) {
			int n = a.length;
			for (int i = 0; i < n; i++) {
				int r = i + (int) (Math.random() * (n - i));
				int tmp = a[i];
				a[i] = a[r];
				a[r] = tmp;
			}
		}

	}


import java.util.*;

public class d {
	final public static int[] DX = {1,0,-1,-1,0,1};
	final public static int[] DY = {1,1,0,-1,-1,0};

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int nC = stdin.nextInt();
		for (int loop=0; loop<nC; loop++) {

			int x = stdin.nextInt();
			int y = stdin.nextInt();
			long[] costs = new long[6];
			for (int i=0; i<6; i++)
				costs[i] = stdin.nextLong();

			long res = 2000000000000000000L;

			for (int i=0; i<6; i++) {
				for (int j=i+1; j<6; j++) {
					if ( i == (j+3)%6 ) continue;

					long den = DX[i]*DY[j] - DY[i]*DX[j];
					long numA = x*DY[j] - y*DX[j];
					long numB = DX[i]*y - DY[i]*x;
					long a = numA/den;
					long b = numB/den;

					if (a < 0 || b < 0) continue;

					long tmp = a*costs[i] + b*costs[j];
					res = Math.min(res, tmp);

				}
			}
			System.out.println(res);

		}
	}
}


import java.util.*;
import java.io.*;

public class Main{
	public static void main(String[] args) throws IOException{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		for(int q = pint(in); q > 0; q--) {
			long[] c = new long[6];
			StringTokenizer st = new StringTokenizer(in.readLine());
			long x = pint(st), y =  pint(st);
			st = new StringTokenizer(in.readLine());
			for(int i = 0; i < 6; i++) {
				c[i] = pint(st);
			}
			long a = x, b = -y;
			long min = val(a,b,c[5],c[4]);
			a = -y; b = x + a;
			min = Math.min(val(a,b,c[3],c[5]), min);;
			a = x; b = y;
			min = Math.min(val(a,b,c[5],c[1]), min);
			a = y; b = x - a;
			min = Math.min(val(a,b,c[0],c[5]), min);

			a = -x; b = -y - a;
			min = Math.min(val(a,b,c[3],c[4]), min);
			a = -x; b = -y;
			min = Math.min(val(a,b,c[2],c[4]), min);
			a = x; b = a - y;
			min = Math.min(val(a,b,c[0],c[4]), min);

			a = -y; b = -x - a;
			min = Math.min(val(a,b,c[3],c[2]), min);
			a = -x; b = y + a;
			min = Math.min(val(a,b,c[3],c[1]), min);

			a = -x; b = y;
			min = Math.min(val(a,b,c[2],c[1]), min);
			a = y; b = a - x;
			min = Math.min(val(a,b,c[0],c[2]), min);

			a = x; b = y - a;
			min = Math.min(val(a,b,c[0],c[1]), min);
			System.out.println(min);

		}
	}
    static int pint(BufferedReader in) throws IOException {return Integer.parseInt(in.readLine());}
    static int pint(StringTokenizer st) {return Integer.parseInt(st.nextToken());}
    static long val(long a, long b, long ca, long cb) {
    	if(a < 0 || b < 0) {return Long.MAX_VALUE;}
    	return a * ca + b * cb;
    }
}


import java.util.*;
import java.io.*;

public class Main{
	public static void main(String[] args) throws IOException{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		for(int q = pint(in); q > 0; q--) {
			long[] c = new long[6];
			StringTokenizer st = new StringTokenizer(in.readLine());
			long x = pint(st), y =  pint(st);
			st = new StringTokenizer(in.readLine());
			for(int i = 0; i < 6; i++) {
				c[i] = pint(st);
			}
			long a = x, b = -y;
			long min = val(a,b,c[5],c[4]);
			a = -y; b = x + a;
			min = Math.min(val(a,b,c[3],c[5]), min);;
			a = x; b = y;
			min = Math.min(val(a,b,c[5],c[1]), min);
			a = y; b = x - a;
			min = Math.min(val(a,b,c[0],c[5]), min);

			a = -x; b = -y - a;
			min = Math.min(val(a,b,c[3],c[4]), min);
			a = -x; b = -y;
			min = Math.min(val(a,b,c[2],c[4]), min);
			a = x; b = a - x;
			min = Math.min(val(a,b,c[0],c[4]), min);

			a = -y; b = - x - a;
			min = Math.min(val(a,b,c[3],c[2]), min);
			a = -x; b = y + a;
			min = Math.min(val(a,b,c[3],c[1]), min);

			a = -x; b = y;
			min = Math.min(val(a,b,c[2],c[1]), min);
			a = y; b = a - x;
			min = Math.min(val(a,b,c[0],c[2]), min);

			a = x; b = y - a;
			min = Math.min(val(a,b,c[0],c[1]), min);
			System.out.println(min);

		}
	}
    static int pint(BufferedReader in) throws IOException {return Integer.parseInt(in.readLine());}
    static int pint(StringTokenizer st) {return Integer.parseInt(st.nextToken());}
    static long val(long a, long b, long ca, long cb) {
    	if(a < 0 || b < 0) {return Long.MAX_VALUE;}
    	return a * ca + b * cb;
    }
}


import java.util.*;
import java.io.*;

public class Main
{
	static class Hexagons
	{
		public static void solve()
		{
			LLMReader rd = new LLMReader();
			PrintWriter pw = new PrintWriter(System.out);
			int x = rd.nextInt();
			int y = rd.nextInt();
			long[] c = new long[8];
			for(int i = 1; i < 7; i++)
			{
				c[i] = rd.nextLong();
			}
			c[0] = c[6];
			c[7] = c[0];
			for(int i = 1; i < 7; i++)
			{
				c[i] = Math.min(c[i - 1] + c[i + 1], c[i]);
			}
			long ans = Long.MAX_VALUE;
			if(x >= 0)
			{
				if(y >= x)
				{
					ans = Math.min(ans, (c[1]*x) + c[2]*(y-x));
				}
				if(y <= x && y >= 0)
				{
					ans = Math.min(ans, (c[1]*y) + c[6]*(x-y));
				}
				if(y <= 0)
				{
					ans = Math.min(ans, (c[6]*x) + c[5]*(-y));
				}
				if(y <= x)
				{
					ans = Math.min(ans, (c[1]*x) + c[5]*(x-y));
				}
				if(y >= 0)
				{
					ans = Math.min(ans, (c[6]*x) + c[2]*y);
				}
			} else
			{
				if(y >= 0)
				{
					ans = Math.min(ans, (c[3]*(-x)) + c[2]*y);
				}
				if(y >= x && y <= 0)
				{
					ans = Math.min(ans, (c[4]*(-y)) + c[3]*(y-x));
				}
				if(y <= x)
				{
					ans = Math.min(ans, (c[4]*(-x)) + c[5]*(x-y));
				}
				if(y <= 0)
				{
					ans = Math.min(ans, (c[3]*(-x)) + c[5]*(-y));
				}
				if(y >= x)
				{
					ans = Math.min(ans, (c[4]*(-x)) + c[2]*(y-x));
				}
			}
			pw.println(ans);
			pw.flush();
		}
	}

	public static void main(String[] args)
	{
		// stolen from llm, llm orz
		LLMReader llm = new LLMReader();
		int t = llm.nextInt();
		while(t-- > 0) Hexagons.solve();
	}

	static class LLMReader
	{
		BufferedReader br;
		StringTokenizer st;

		public LLMReader()
		{
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		public LLMReader(String s) throws FileNotFoundException
		{
			br = new BufferedReader(new FileReader(new File(s)));
		}

		String next()
		{
			while (st == null || !st.hasMoreElements())
			{
				try
				{
					st = new StringTokenizer(br.readLine());
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt()
		{
			return Integer.parseInt(next());
		}

		long nextLong()
		{
			return Long.parseLong(next());
		}

		double nextDouble()
		{
			return Double.parseDouble(next());
		}

		String nextLine()
		{
			String str = "";
			try
			{
				str = br.readLine();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
	}
}


import java.util.*;
import java.io.*;

public class Main
{
	static class Hexagons
	{
		public void solve(Scanner rd)
		{
			PrintWriter pw = new PrintWriter(System.out);
			long x = rd.nextLong();
			long y = rd.nextLong();
			long[] c = new long[8];
			for(int i = 1; i < 7; i++)
			{
				c[i] = rd.nextLong();
			}
			c[0] = c[6];
			c[7] = c[0];
			for(int i = 1; i < 7; i++)
			{
				c[i] = Math.min(c[i - 1] + c[i + 1], c[i]);
			}
			long ans = Long.MAX_VALUE;
			if(x >= 0)
			{
				if(y >= x)
				{
					ans = Math.min(ans, (c[1]*x) + c[2]*(y-x));
				}
				if(y <= x && y >= 0)
				{
					ans = Math.min(ans, (c[1]*y) + c[6]*(x-y));
				}
				if(y <= 0)
				{
					ans = Math.min(ans, (c[6]*x) + c[5]*(-y));
				}
				if(y <= x)
				{
					ans = Math.min(ans, (c[1]*x) + c[5]*(x-y));
				}
				if(y >= 0)
				{
					ans = Math.min(ans, (c[6]*x) + c[2]*y);
				}
			} else
			{
				if(y >= 0)
				{
					ans = Math.min(ans, (c[3]*(-x)) + c[2]*y);
				}
				if(y >= x && y <= 0)
				{
					ans = Math.min(ans, (c[4]*(-y)) + c[3]*(y-x));
				}
				if(y <= x)
				{
					ans = Math.min(ans, (c[4]*(-x)) + c[5]*(x-y));
				}
				if(y <= 0)
				{
					ans = Math.min(ans, (c[3]*(-x)) + c[5]*(-y));
				}
				if(y >= x)
				{
					ans = Math.min(ans, (c[4]*(-x)) + c[2]*(y-x));
				}
			}
			pw.println(ans);
			pw.flush();
		}
	}

	public static void main(String[] args) throws IOException
	{
		Scanner rd = new Scanner(System.in);
		int t = rd.nextInt();
		Hexagons hex = new Hexagons();
		while(t-- > 0) hex.solve(rd);
	}

}


import java.util.*;
import java.io.*;

public class Main
{
	static class Hexagons
	{
		public static void solve()
		{
			Scanner rd = new Scanner(System.in);
			PrintWriter pw = new PrintWriter(System.out);
			int x = rd.nextInt();
			int y = rd.nextInt();
			long[] c = new long[8];
			for(int i = 1; i < 7; i++)
			{
				c[i] = rd.nextLong();
			}
			c[0] = c[6];
			c[7] = c[0];
			for(int i = 1; i < 7; i++)
			{
				c[i] = Math.min(c[i - 1] + c[i + 1], c[i]);
			}
			long ans = Long.MAX_VALUE;
			if(x >= 0)
			{
				if(y >= x)
				{
					ans = Math.min(ans, (c[1]*x) + c[2]*(y-x));
				}
				if(y <= x && y >= 0)
				{
					ans = Math.min(ans, (c[1]*y) + c[6]*(x-y));
				}
				if(y <= 0)
				{
					ans = Math.min(ans, (c[6]*x) + c[5]*(-y));
				}
				if(y <= x)
				{
					ans = Math.min(ans, (c[1]*x) + c[5]*(x-y));
				}
				if(y >= 0)
				{
					ans = Math.min(ans, (c[6]*x) + c[2]*y);
				}
			} else
			{
				if(y >= 0)
				{
					ans = Math.min(ans, (c[3]*(-x)) + c[2]*y);
				}
				if(y >= x && y <= 0)
				{
					ans = Math.min(ans, (c[4]*(-y)) + c[3]*(y-x));
				}
				if(y <= x)
				{
					ans = Math.min(ans, (c[4]*(-x)) + c[5]*(x-y));
				}
				if(y <= 0)
				{
					ans = Math.min(ans, (c[3]*(-x)) + c[5]*(-y));
				}
				if(y >= x)
				{
					ans = Math.min(ans, (c[4]*(-x)) + c[2]*(y-x));
				}
			}
			pw.println(ans);
			pw.flush();
		}
	}

	public static void main(String[] args) throws IOException
	{
		// stolen from llm, llm orz
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		while(t-- > 0) Hexagons.solve();
	}

}


import java.util.*;
import java.io.*;

public class Main
{
	static class Hexagons
	{
		public void solve()
		{
			Scanner rd = new Scanner(System.in);
			PrintWriter pw = new PrintWriter(System.out);
			long x = rd.nextLong();
			long y = rd.nextLong();
			long[] c = new long[8];
			for(int i = 1; i < 7; i++)
			{
				c[i] = rd.nextLong();
			}
			c[0] = c[6];
			c[7] = c[0];
			for(int i = 1; i < 7; i++)
			{
				c[i] = Math.min(c[i - 1] + c[i + 1], c[i]);
			}
			long ans = Long.MAX_VALUE;
			if(x >= 0)
			{
				if(y >= x)
				{
					ans = Math.min(ans, (c[1]*x) + c[2]*(y-x));
				}
				if(y <= x && y >= 0)
				{
					ans = Math.min(ans, (c[1]*y) + c[6]*(x-y));
				}
				if(y <= 0)
				{
					ans = Math.min(ans, (c[6]*x) + c[5]*(-y));
				}
				if(y <= x)
				{
					ans = Math.min(ans, (c[1]*x) + c[5]*(x-y));
				}
				if(y >= 0)
				{
					ans = Math.min(ans, (c[6]*x) + c[2]*y);
				}
			} else
			{
				if(y >= 0)
				{
					ans = Math.min(ans, (c[3]*(-x)) + c[2]*y);
				}
				if(y >= x && y <= 0)
				{
					ans = Math.min(ans, (c[4]*(-y)) + c[3]*(y-x));
				}
				if(y <= x)
				{
					ans = Math.min(ans, (c[4]*(-x)) + c[5]*(x-y));
				}
				if(y <= 0)
				{
					ans = Math.min(ans, (c[3]*(-x)) + c[5]*(-y));
				}
				if(y >= x)
				{
					ans = Math.min(ans, (c[4]*(-x)) + c[2]*(y-x));
				}
			}
			pw.println(ans);
			pw.flush();
		}
	}

	public static void main(String[] args) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		Hexagons hex = new Hexagons();
		while(t-- > 0) hex.solve();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main
{
	public static void main(String[] args) {
		new Thread(null, new Runnable() {
			public void run() {
                solve();
            }
        }, "1", 1 << 26).start();
	}
	static void solve () {
		FastReader fr =new FastReader();	PrintWriter op =new PrintWriter(System.out);

 		int t =fr.nextInt() ,i ;

 		long x ,y ,c[] =new long[7] ,j ,k ,cst ;

 		while (t-- > 0) {
 			x =fr.nextLong() ;

 			y =fr.nextLong() ;

 			cst =0l ;

 			for (i =1 ; i<7 ; ++i)	c[i] =fr.nextLong() ;

 			if (x*y >= 0) {
 				if (x>0 || y>0) {
 					j =Math.min(c[1] , c[2]+c[6]) ;

 					k =Math.min(x,y) ;

 					cst += (j * k) ;

 					x -= k ;

 					y -= k ;

 					cst += (x * Math.min(c[6] , c[1]+c[5])) ;

 					cst += (y * Math.min(c[2] , c[1]+c[3])) ;
 				}
 				else {
 					j =Math.min(c[4] , c[3]+c[5]) ;

 					x *= -1 ;	y *= -1 ;

 					k =Math.min(x,y) ;

 					cst += (j * k) ;

 					x -= k ;

 					y -= k ;

 					cst += (x * Math.min(c[3] , c[4]+c[2])) ;

 					cst += (y * Math.min(c[5] , c[4]+c[6])) ;
 				}
 			}
 			else {
 				if (x <= 0) {
 					j =Math.min(c[4]+c[2] , c[3]) ;

 					cst += (j * Math.abs(x)) ;
 				}
 				else {
 					j =Math.min(c[1]+c[5] , c[6]) ;

 					cst += (j * x) ;
 				}

 				if (y <= 0) {
 					j =Math.min(c[4]+c[6] , c[5]) ;

 					cst += (j * Math.abs(y)) ;
 				}
 				else {
 					j =Math.min(c[1]+c[3] , c[2]) ;

 					cst += (j * y) ;
 				}
 			}

 			op.println(cst) ;
 		}
		op.flush();	op.close();
	}
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br =new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st==null || (!st.hasMoreElements()))
			{
				try
				{
					st =new StringTokenizer(br.readLine());
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}

			}
			return st.nextToken();
		}

		String nextLine() {
			String str ="";

			try
			{
				str =br.readLine();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}

			return str;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next()) ;
		}
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main
{
	public static void main(String[] args) {
		new Thread(null, new Runnable() {
			public void run() {
                solve();
            }
        }, "1", 1 << 26).start();
	}
	static void solve () {
		FastReader fr =new FastReader();	PrintWriter op =new PrintWriter(System.out);

 		int t =fr.nextInt() ,i ;

 		long x ,y ,c[] =new long[7] ,j ,k ,cst ;

 		while (t-- > 0) {
 			x =fr.nextLong() ;

 			y =fr.nextLong() ;

 			cst =0l ;

 			for (i =1 ; i<7 ; ++i)	c[i] =fr.nextLong() ;

 			if (x*y >= 0) {
 				if (x>=0 || y>=0) {
 					j =Math.min(c[1] , c[2]+c[6]) ;

 					k =Math.min(x,y) ;

 					cst += (j * k) ;

 					x -= k ;

 					y -= k ;

 					cst += (x * Math.min(c[6] , c[1]+c[5])) ;

 					cst += (y * Math.min(c[2] , c[1]+c[3])) ;
 				}
 				else {
 					j =Math.min(c[4] , c[3]+c[5]) ;

 					x *= -1 ;	y *= -1 ;

 					k =Math.min(x,y) ;

 					cst += (j * k) ;

 					x -= k ;

 					y -= k ;

 					cst += (x * Math.min(c[3] , c[4]+c[2])) ;

 					cst += (y * Math.min(c[5] , c[4]+c[6])) ;
 				}
 			}
 			else {
 				if (x <= 0) {
 					j =Math.min(c[4]+c[2] , c[3]) ;

 					cst += (j * Math.abs(x)) ;
 				}
 				else {
 					j =Math.min(c[1]+c[5] , c[6]) ;

 					cst += (j * x) ;
 				}

 				if (y <= 0) {
 					j =Math.min(c[4]+c[6] , c[5]) ;

 					cst += (j * Math.abs(y)) ;
 				}
 				else {
 					j =Math.min(c[1]+c[3] , c[2]) ;

 					cst += (j * y) ;
 				}
 			}

 			op.println(cst) ;
 		}
		op.flush();	op.close();
	}
	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br =new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st==null || (!st.hasMoreElements()))
			{
				try
				{
					st =new StringTokenizer(br.readLine());
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}

			}
			return st.nextToken();
		}

		String nextLine() {
			String str ="";

			try
			{
				str =br.readLine();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}

			return str;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next()) ;
		}
	}
}


import java.util.*;
import java.io.*;

public class D1421 {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter pw = new PrintWriter(System.out);
		int t = sc.nextInt();
		while (t-- > 0) {
			int x = sc.nextInt();
			int y = sc.nextInt();
			int[] c = sc.nextIntArr(6);
			Integer[] idx = new Integer[6];
			for (int i = 0; i < 6; i++) {
				idx[i] = i;
			}
			Arrays.sort(idx, (u, v) -> c[u] - c[v]);
			for (int i = 0; i < 6; i++) {
				int cur = idx[i];
				int next = (cur + 1) % 6;
				int prev = (cur - 1 + 6) % 6;
				c[cur] = Math.min(c[cur], c[prev] + c[next]);
			}
			int[][] m = { { 1, 1 }, { 0, 1 }, { -1, 0 }, { -1, -1 }, { 0, -1 }, { 1, 0 } };
			long ans = Long.MAX_VALUE;
			for (int i = 0; i < 6; i++) {
				long x1 = m[i][0];
				long y1 = m[i][1];
				if (x == 0) {
					if (y == 0) {
						ans = 0;
					} else {
						if (x1 == 0) {
							long steps = y * y1;
							if (steps > 0) {
								ans = Math.min(ans, steps * c[i]);
							}
						}
					}
				} else {
					if (y == 0) {
						if (y1 == 0) {
							long steps = x * x1;
							if (steps > 0) {
								ans = Math.min(ans, steps * c[i]);
							}
						}
					} else {
						if (y1 != 0 && x1 != 0) {
							long steps1 = x * x1;
							long steps2 = y * y1;
							if (steps1 == steps2 && steps1 > 0) {
								ans = Math.min(ans, steps1 * c[i]);
							}
						}
					}
				}
			}
			for (int i = 0; i < 6; i++) {
				for (int j = 0; j < 6; j++) {
					if (j == (i + 3) % 6) {
						continue;
					}
					long x1 = m[i][0];
					long y1 = m[i][1];
					long x2 = m[j][0];
					long y2 = m[j][1];
					long delta = x1 * y2 - y1 * x2;
					if (delta == 0) {
						continue;
					}
					long deltaa = x * y2 - y * x2;
					long deltab = x1 * y - y1 * x;
					long a = deltaa / delta;
					long b = deltab / delta;
					if (a >= 0 && b >= 0)
						ans = Math.min(a * c[i] + b * c[j], ans);
				}
			}
			pw.println(ans);

		}
		pw.close();
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public Scanner(FileReader f) {
			br = new BufferedReader(f);
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		public int[] nextIntArr(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = Integer.parseInt(next());
			}
			return arr;
		}

	}

}


import java.util.Scanner;
import java.util.stream.Stream;

public class CF_676_d {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();

        int a, b;
        int c1,c2,c3,c4,c5,c6;
        for (int i = 0; i < t; i++) {
            a = sc.nextInt();
            b = sc.nextInt();

            c1 = sc.nextInt();
            c2 = sc.nextInt();
            c3 = sc.nextInt();
            c4 = sc.nextInt();
            c5 = sc.nextInt();
            c6 = sc.nextInt();

            long diag1 = Stream.of(c5+c1+c5, c6+c4+c6, c6+c5).mapToInt(v -> v).min().orElse(0);
            long diag2 = Stream.of(c3+c1+c3, c2+c4+c2, c2+c3).mapToInt(v -> v).min().orElse(0);
            long diag3 = Math.min(c2+c6, c1);
            long diag4 = Math.min(c5+c3, c4);

            long top = Math.min(c2, c1 + c3);
            long bottom = Math.min(c5, c6+c4);
            long left = Math.min(c3, c4+c2);
            long right = Math.min(c6, c5+c1);

            int xa = Math.abs(a);
            int xb = Math.abs(b);

            int diag = Math.min(xa, xb);

            long DT;
            if (a > 0 && b > 0) {
                DT = diag3;
            } else if (a > 0 && b < 0) {
                DT = diag1;
            } else if (a < 0 && b > 0) {
                DT = diag2;
            } else {
                DT = diag4;
            }

            long res = DT * diag;

            a = a > 0 ? a - diag : a + diag;
            b = b > 0 ? b - diag : b + diag;

            if (a != 0) {
                if (a > 0) {
                    res += right * a;
                } else {
                    res += left * Math.abs(a);
                }
            }

            if (b != 0) {
                if (b > 0) {
                    res += top * b;
                } else {
                    res += bottom * Math.abs(b);
                }
            }

            System.out.println(res);
        }
    }
}


import java.util.Scanner;
import java.util.stream.Stream;

public class CF_676_d {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();

        long a, b;
        long c1,c2,c3,c4,c5,c6;
        for (int i = 0; i < t; i++) {
            a = sc.nextLong();
            b = sc.nextLong();

            c1 = sc.nextLong();
            c2 = sc.nextLong();
            c3 = sc.nextLong();
            c4 = sc.nextLong();
            c5 = sc.nextLong();
            c6 = sc.nextLong();

            long diag1 = Stream.of(c5+c1+c5, c6+c4+c6, c6+c5).mapToLong(v -> v).min().orElse(0);
            long diag2 = Stream.of(c3+c1+c3, c2+c4+c2, c2+c3).mapToLong(v -> v).min().orElse(0);
            long diag3 = Math.min(c2+c6, c1);
            long diag4 = Math.min(c5+c3, c4);

            long top = Math.min(c2, c1 + c3);
            long bottom = Math.min(c5, c6+c4);
            long left = Math.min(c3, c4+c2);
            long right = Math.min(c6, c5+c1);

            long xa = Math.abs(a);
            long xb = Math.abs(b);

            long diag = Math.min(xa, xb);

            long DT;
            if (a > 0 && b > 0) {
                DT = diag3;
            } else if (a > 0 && b < 0) {
                DT = diag1;
            } else if (a < 0 && b > 0) {
                DT = diag2;
            } else {
                DT = diag4;
            }

            long res = DT * diag;

            a = a > 0 ? a - diag : a + diag;
            b = b > 0 ? b - diag : b + diag;

            if (a != 0) {
                if (a > 0) {
                    res += right * a;
                } else {
                    res += left * Math.abs(a);
                }
            }

            if (b != 0) {
                if (b > 0) {
                    res += top * b;
                } else {
                    res += bottom * Math.abs(b);
                }
            }

            System.out.println(res);
        }
    }
}


import java.util.Scanner;
import java.util.stream.Stream;

public class CF_676_d {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();

        int a, b;
        int c1,c2,c3,c4,c5,c6;
        for (int i = 0; i < t; i++) {
            a = sc.nextInt();
            b = sc.nextInt();

            c1 = sc.nextInt();
            c2 = sc.nextInt();
            c3 = sc.nextInt();
            c4 = sc.nextInt();
            c5 = sc.nextInt();
            c6 = sc.nextInt();

            long diag1 = Stream.of(c5+c1+c5, c6+c4+c6, c6+c5).mapToInt(v -> v).min().orElse(0);
            long diag2 = Stream.of(c3+c1+c3, c2+c4+c2, c2+c3).mapToInt(v -> v).min().orElse(0);
            long diag3 = Math.min(c2+c6, c1);
            long diag4 = Math.min(c5+c3, c4);

            long top = Math.min(c2, c1 + c3);
            long bottom = Math.min(c5, c6+c4);
            long left = Math.min(c3, c4+c2);
            long right = Math.min(c6, c5+c1);

            int xa = Math.abs(a);
            int xb = Math.abs(b);

            int diag = Math.min(xa, xb);

            long DT;
            if (a > 0 && b > 0) {
                DT = diag3;
            } else if (a > 0 && b < 0) {
                DT = diag1;
            } else if (a < 0 && b > 0) {
                DT = diag2;
            } else {
                DT = diag4;
            }

            long res = DT * diag;

            a = a > 0 ? a - diag : a + diag;
            b = b > 0 ? b - diag : b + diag;

            if (a != 0) {
                if (a > 0) {
                    res += right * a;
                } else {
                    res += left * Math.abs(a);
                }
            }

            if (b != 0) {
                if (b > 0) {
                    res += top * b;
                } else {
                    res += bottom * Math.abs(xb);
                }
            }

            System.out.println(res);
        }
    }
}


import java.util.*;
import java.io.*;

public class Hexagons {

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FastReader t = new FastReader();
		PrintWriter o = new PrintWriter(System.out);
		int test = t.nextInt();

		while (test-- > 0) {
			long x = t.nextLong();
			long y = t.nextLong();
			long[] a = new long[6];

			for (int i = 0; i < 6; ++i)
				a[i] = t.nextLong();

			long c1 = Math.min(a[0], a[5] + a[1]);
			long c2 = Math.min(a[1], a[0] + a[2]);
			long c3 = Math.min(a[2], a[1] + a[3]);
			long c4 = Math.min(a[3], a[2] + a[4]);
			long c5 = Math.min(a[4], a[3] + a[5]);
			long c6 = Math.min(a[5], a[4] + a[0]);
			long ans = 0;

			if (x == 0 || y == 0) {
				if (x == 0) {
					if (y > 0) {
						ans = y * c2;
					} else {
						ans = (-y) * c5;
					}
				} else {
					if (x > 0) {
						ans = x * c6;
					} else {
						ans = (-x) * c3;
					}
				}
			} else if (x > 0 && y > 0) {
				long min = Math.min(x, y);
				ans = min * c1;
				x -= min;
				y -= min;

				if (x > 0)
					ans += x * c6;
				else
					ans += y * c2;
			} else if (x < 0 && y < 0) {
				long max = Math.max(x, y);
				ans = (-max) * c4;
				x -= max;
				y -= max;

				if (x != 0)
					ans += (-x) * c3;
				else
					ans += (-y) * c5;
			} else if (x > 0 && y < 0) {
				ans = x * c6 - y * c5;
			} else {
				ans = -x * c3 + y * c2;
			}

			o.println(ans);
		}

		o.flush();
		o.close();
	}
}


import java.util.*;
import java.io.*;

public class Main {
    static Scanner sc;
    static BufferedReader br;
    static PrintWriter out;

    public static void main(String[] args) throws Exception{
        sc = new Scanner(System.in);
      //  br=new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);

        int t = 1;
        t = sc.nextInt();
        while (t-- > 0) {
            forEachTestCase();
        }

        //br.close();
        sc.close();
        out.close();
    }

    /*
    *
    *
            i Read Wonsei code and logic in comment.
            so this code's logic same as Wonsei.
            * thanks to Wonsei.
            * this is not my logic.
    *
            * thanks to  StrAnge.R asking question in comment
    * */

    static void forEachTestCase() throws  Exception{
        long x=sc.nextLong();
        long y=sc.nextLong();
        long cost[]=new long[6];
        for (int i = 0; i <6 ; i++) {
            cost[i]=sc.nextLong();
        }
        long n1=Math.min(cost[0],cost[1]+cost[5]);
        long n2=Math.min(cost[1],cost[2]+cost[0]);
        long n3=Math.min(cost[2],cost[3]+cost[1]);
        long n4=Math.min(cost[3],cost[4]+cost[2]);
        long n5=Math.min(cost[4],cost[5]+cost[3]);
        long n6=Math.min(cost[5],cost[0]+cost[4]);

        long ans=0;
        if(x>=0 && y>=0){
            long min=Math.min(x,y);
            ans+=n1*min;
            x-=min;
            y-=min;
            if(x>0){
                ans+=n6*x;
            }
            if(y>0){
                ans+=n2*y;
            }
        }
        else if(x>=0 && y<=0){
            ans+=n6*x+n5*Math.abs(y);
        }
        else if(x<=0 && y>=0){
            ans+=n3*Math.abs(x)+n2*y;
        }
        else if(x<=0 && y<=0){
            long min=Math.min(-x,-y);
            ans+=n4*min;
            x+=min;
            y+=min;
            if(-x>0){
                ans+=n3*-x;
            }
            if(-y>0){
                ans+=n5*-y;
            }
        }
        out.println(ans);
    }

    static void remove(TreeMap<Integer,Integer> tm,int key){
        int fre=tm.get(key);
        if(fre==1){
            tm.remove(key);
            return;
        }
        tm.put(key,fre-1);
    }
    static long mod = 998244353;

    static long add(long a, long b) {
        a %= mod;
        b %= mod;
        return (a + b) % mod;
    }

    static long mul(long a, long b) {
        a %= mod;
        b %= mod;
        return (a * b) % mod;
    }

    static long sub(long a, long b) {
        a %= mod;
        b %= mod;
        return (((a - b) % mod) + mod) % mod;
    }

    static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
    static int gcd(int a,int b){
        if(b==0)return a;
        return gcd(b,a%b);
    }

    static long modPow(long a, long b) {
        long ans = 1;
        while (b > 0) {
            if (b % 2 == 1) {
                ans = mul(ans, a);
            }
            a = mul(a, a);
            b >>= 1;
        }
        return ans;
    }

    static long modInv(long a) {
        return modPow(a, mod - 2);
    }

    static class DSU{
        int n,par[],size[],connectedComponent;
        DSU(int n){
            this.n=n;
            par=new int[n];
            size=new int[n];
            connectedComponent=n;
            for(int i=0;i<n;i++){
                par[i]=i;
                size[i]=1;
            }
        }
        int root(int u){
            if(u==par[u]){
                return u;
            }
            return par[u]=root(par[u]);
        }
        boolean isConnected(int u,int v){
            u=root(u);
            v=root(v);
            if(u==v)return true;
            return false;
        }
        void union(int u,int v){
            u=root(u);
            v=root(v);
            if(u!=v){
                if(size[u]<size[v]){
                    int temp=u;
                    u=v;
                    v=temp;
                }
                size[u]+=size[v];
                par[v]=u;
                connectedComponent--;
            }
        }
        boolean oneComponent(){
            if(connectedComponent==1)return true;
            return false;
        }
    }
}


import java.util.*;
import java.io.*;

public class Main {
    static Scanner sc;
    static BufferedReader br;
    static PrintWriter out;

    public static void main(String[] args) throws Exception{
        sc = new Scanner(System.in);
      //  br=new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);

        int t = 1;
        t = sc.nextInt();
        while (t-- > 0) {
            forEachTestCase();
        }

        //br.close();
        sc.close();
        out.close();
    }

    /*
    *
    *
    *
    *
    *         |/
    *        - -
             /|

           This six directions are allowed.
           *
           * and then i Read Wonsei code and logic in comment.
           * and implement myself.
    *
            * and look from comment of StrAnge.R
    * */

    static void forEachTestCase() throws  Exception{
        long x=sc.nextLong();
        long y=sc.nextLong();
        long cost[]=new long[6];
        for (int i = 0; i <6 ; i++) {
            cost[i]=sc.nextLong();
        }
        long ans=0;
        if(x>=0 && y>=0){
            long min=Math.min(x,y);
            ans+=Math.min(cost[0],cost[1]+cost[5])*min;
            x-=min;
            y-=min;
            if(x>0){
                ans+=cost[5]*x;
            }
            if(y>0){
                ans+=cost[1]*y;
            }
        }
        else if(x>=0 && y<=0){
            ans+=cost[5]*x+cost[4]*Math.abs(y);
        }
        else if(x<=0 && y>=0){
            ans+=cost[2]*Math.abs(x)+cost[1]*y;
        }
        else if(x<=0 && y<=0){
            long min=Math.min(-x,-y);
            ans+=Math.min(cost[3],cost[2]+cost[4])*min;
            x+=min;
            y+=min;
            if(-x>0){
                ans+=cost[2]*-x;
            }
            if(-y>0){
                ans+=cost[4]*-y;
            }
        }
        out.println(ans);
    }

    static void remove(TreeMap<Integer,Integer> tm,int key){
        int fre=tm.get(key);
        if(fre==1){
            tm.remove(key);
            return;
        }
        tm.put(key,fre-1);
    }
    static long mod = 998244353;

    static long add(long a, long b) {
        a %= mod;
        b %= mod;
        return (a + b) % mod;
    }

    static long mul(long a, long b) {
        a %= mod;
        b %= mod;
        return (a * b) % mod;
    }

    static long sub(long a, long b) {
        a %= mod;
        b %= mod;
        return (((a - b) % mod) + mod) % mod;
    }

    static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }
    static int gcd(int a,int b){
        if(b==0)return a;
        return gcd(b,a%b);
    }

    static long modPow(long a, long b) {
        long ans = 1;
        while (b > 0) {
            if (b % 2 == 1) {
                ans = mul(ans, a);
            }
            a = mul(a, a);
            b >>= 1;
        }
        return ans;
    }

    static long modInv(long a) {
        return modPow(a, mod - 2);
    }

    static class DSU{
        int n,par[],size[],connectedComponent;
        DSU(int n){
            this.n=n;
            par=new int[n];
            size=new int[n];
            connectedComponent=n;
            for(int i=0;i<n;i++){
                par[i]=i;
                size[i]=1;
            }
        }
        int root(int u){
            if(u==par[u]){
                return u;
            }
            return par[u]=root(par[u]);
        }
        boolean isConnected(int u,int v){
            u=root(u);
            v=root(v);
            if(u==v)return true;
            return false;
        }
        void union(int u,int v){
            u=root(u);
            v=root(v);
            if(u!=v){
                if(size[u]<size[v]){
                    int temp=u;
                    u=v;
                    v=temp;
                }
                size[u]+=size[v];
                par[v]=u;
                connectedComponent--;
            }
        }
        boolean oneComponent(){
            if(connectedComponent==1)return true;
            return false;
        }
    }
}