import java.io.*;
import java.util.*;
import static java.lang.Math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class MarketingScheme {
    public static void main(String[] args) throws IOException {
        // InputStream inputStream = new FileInputStream("input.txt");
        // OutputStream outputStream = new FileOutputStream("output.txt");
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(in.nextInt(), in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(1e9) + 7;
        long fact[];
        int depth[];
        int parentTable[][];
        int degree[];
        ArrayList<Integer> leaves;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int diam = 0;

        public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
            while(testNumber-->0){
                long x = in.nextLong();
                long y = in.nextLong();
                long c[] = new long[6];
                for(int i=0;i<6;i++)
                    c[i] = in.nextLong();
                for(int i=0;i<c.length;i++) {
                    c[i]=min(c[i], c[(i+1)%c.length]+c[(i-1+c.length)%c.length]);
                }
                long ans = 0;
                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];
                    }
                }

                out.println(ans);
            }
        }

        public int lower(ArrayList<Integer> a , int x){
            if(a.size() == 0)
                return 0;
            if(a.get(a.size()-1) < x)
                return a.size();
            int l = 0;
            int r = a.size()-1;
            while(l<=r){
                int mid = (l+r)>>1;
                if(a.get(mid)>=x && (mid<1 || a.get(mid-1)<x))
                    return mid;
                else if(a.get(mid) < x)
                    l = mid+1;
                else
                    r = mid-1;
            }
            return -1;
        }

        public Node left(Node x){
            if(x == null || x.empty)
                return x;
            Node y = left(x.left);
            x.left = y;
            return y;
        }

        public Node right(Node x){
            if(x == null || x.empty)
                return x;
            Node y = right(x.right);
            x.right = y;
            return y;
        }

        class Node{
            int val;
            Node left;
            Node right;
            boolean empty;
            public Node(int val){
                this.val = val;
                left = null;
                right = null;
                empty = true;
            }
        }

        public Node add(int a[] , int start , int end){
            if(start>end)
                return null;
            int mid = (start+end)>>1;
            Node root = new Node(a[mid]);
            root.left = add(a , start , mid-1);
            root.right = add(a ,mid+1 , end);
            return root;
        }

        public void dfs(ArrayList<ArrayList<int[]>> a , int index , int parent , int val , int visited[]){
            if(index == a.size()-1){
                max = Math.max(max , val);
                return;
            }
            visited[index] = 1;
            for(int i=0;i<a.get(index).size();i++){
                int u = index;
                int v = a.get(index).get(i)[0];
                int speed = a.get(index).get(i)[1];
                if(visited[v] == 1){
                    if(v == a.size()-1){
                        max = Math.max(max , Math.min(speed , val));
                    }
                    continue;
                }
                dfs(a , v , u , Math.min(val , speed) , visited);
            }
        }

        class Pair{
            int u;
            int v;
            int speed;
            public Pair(int u , int v , int speed){
                this.u = u;
                this.v = v;
                this.speed = speed;
            }
        }

        public void add(HashMap<Integer , ArrayList<Integer>> m , int x , int y){
            if(!m.containsKey(x))
                m.put(x , new ArrayList<>());
            ArrayList<Integer> a = m.get(x);
            a.add(y);
        }

        public void dfs(ArrayList<ArrayList<Integer>> a , int index , int parent , int size[]){
            size[index] = 1;
            depth[index] = 1+depth[parent];
            for(int i=0;i<a.get(index).size();i++){
                if(a.get(index).get(i) == parent)
                    continue;
                dfs(a , a.get(index).get(i) , index , size);
                size[index] += size[a.get(index).get(i)];
            }
        }

        public int distance(ArrayList<ArrayList<Integer>> a , int u , int v){
            return depth[u]+depth[v] - 2*depth[lca(a , u , v)];
        }

        public int lca(ArrayList<ArrayList<Integer>> a , int u , int v){
            if(depth[v]<depth[u]){
                int x = u;
                u = v;
                v = x;
            }
            int diff = depth[v] - depth[u];
            for(int i=0;i<parentTable[v].length;i++){
                // checking whether the ith bit is set in the diff variable
                if(((diff>>i)&1) == 1)
                    v = parentTable[v][i];
            }
            if(v == u)
                return v;
            for(int i=parentTable[v].length-1;i>=0;i--){
                if(parentTable[u][i] != parentTable[v][i]){
                    v = parentTable[v][i];
                    u = parentTable[u][i];
                }
            }
            return parentTable[u][0];
        }

        public int[][] multiply(int a[][] , int b[][]){
            int c[][] = new int[a.length][b[0].length];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b[0].length;j++){
                    for(int k=0;k<b.length;k++)
                        c[i][j] += a[i][k]*b[k][j];
                }
            }
            return c;
        }

        public int[][] multiply(int a[][] , int b[][] , int mod){
            int c[][] = new int[a.length][b[0].length];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b[0].length;j++){
                    for(int k=0;k<b.length;k++){
                        c[i][j] += a[i][k]*b[k][j];
                        c[i][j]%=mod;
                    }
                }
            }
            return c;
        }

        public int[][] pow(int a[][] , long b){
            int res[][] = new int[a.length][a[0].length];
            for(int i=0;i<a.length;i++)
                res[i][i] = 1;
            while(b>0){
                if((b&1) == 1)
                    res = multiply(res , a , 10);
                a = multiply(a , a , 10);
                b>>=1;
            }
            return res;
        }

        // for the min max problems
        public void build(int lookup[][] , int arr[], int n) {
            for (int i = 0; i < n; i++)
                lookup[i][0] = arr[i];
            for (int j = 1; (1 << j) <= n; j++) {
                for (int i = 0; (i + (1 << j) - 1) < n; i++) {
                    if (lookup[i][j - 1] > lookup[i + (1 << (j - 1))][j - 1])
                        lookup[i][j] = lookup[i][j - 1];
                    else
                        lookup[i][j] = lookup[i + (1 << (j - 1))][j - 1];
                }
            }
        }
        public int query(int lookup[][] , int L, int R) {
            int j = (int)(Math.log(R - L + 1)/Math.log(2));
            if (lookup[L][j] >= lookup[R - (1 << j) + 1][j])
                return lookup[L][j];
            else
                return lookup[R - (1 << j) + 1][j];
        }

        // for printing purposes
        public void print1d(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print1d(long a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print2d(int a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            // out.println();
        }
        public void print2d(long a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            // out.println();
        }

        public void sieve(int a[]){
            a[0] = a[1] = 1;
            int i;
            for(i=2;i*i<=a.length;i++){
                if(a[i] != 0)
                    continue;
                a[i] = i;
                for(int k = (i)*(i);k<a.length;k+=i){
                    if(a[k] != 0)
                        continue;
                    a[k] = i;
                }
            }
        }

        public long nCrPFermet(int n , int r , long p){
            if(r==0)
                return 1l;
            // long fact[] = new long[n+1];
            // fact[0] = 1;
            // for(int i=1;i<=n;i++)
            //     fact[i] = (i*fact[i-1])%p;
            long modInverseR = pow(fact[r] , p-2 , p);
            long modInverseNR = pow(fact[n-r] , p-2 , p);
            long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
            return w;
        }
        public long pow(long a, long b, long m) {
            a %= m;
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a % m;
                a = a * a % m;
                b >>= 1;
            }
            return res;
        }
        public long pow(long a, long b) {
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a;
                a = a * a;
                b >>= 1;
            }
            return res;
        }
        public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
            if (start > end) {
                return;
            }
            int mid = (start + end) / 2;
            a.add(mid);
            sortedArrayToBST(a, start, mid - 1);
            sortedArrayToBST(a, mid + 1, end);
        }
        class Combine{
            int value;
            int delete;
            Combine(int val , int delete){
                this.value = val;
                this.delete = delete;
            }
        }
        class Sort2 implements Comparator<Combine>{
            public int compare(Combine a , Combine b){
                if(a.value > b.value)
                    return 1;
                else if(a.value == b.value && a.delete>b.delete)
                    return 1;
                else if(a.value == b.value && a.delete == b.delete)
                    return 0;
                return -1;
            }
        }
        public int lowerLastBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>=x)
                return -1;
            if(a.get(r)<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid-1)<x)
                    return mid-1;
                else if(a.get(mid)>=x)
                    r = mid-1;
                else if(a.get(mid)<x && a.get(mid+1)>=x)
                    return mid;
                else if(a.get(mid)<x && a.get(mid+1)<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int lowerLastBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>=x)
                return -1;
            if(a[r]<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid-1]<x)
                    return mid-1;
                else if(a[mid]>=x)
                    r = mid-1;
                else if(a[mid]<x && a[mid+1]>=x)
                    return mid;
                else if(a[mid]<x && a[mid+1]<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>x)
                return l;
            if(a[r]<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid+1]>x)
                    return mid+1;
                else if(a[mid]<=x)
                    l = mid+1;
                else if(a[mid]>x && a[mid-1]<=x)
                    return mid;
                else if(a[mid]>x && a[mid-1]>x)
                    r = mid-1;
            }
            return mid;
        }
        public long log(float number , int base){
            return (long) Math.ceil((Math.log(number) / Math.log(base)) + 1e-9);
        }
        public long gcd(long a , long b){
            if(a<b){
                long c = b;
                b = a;
                a = c;
            }
            while(b!=0){
                long c = a;
                a = b;
                b = c%a;
            }
            return a;
        }
        public long[] gcdEx(long p, long q) {
            if (q == 0)
                return new long[] { p, 1, 0 };
            long[] vals = gcdEx(q, p % q);
            long d = vals[0];
            long a = vals[2];
            long b = vals[1] - (p / q) * vals[2];
            // 0->gcd 1->xValue 2->yValue
            return new long[] { d, a, b };
        }
        public void sievePhi(int a[]){
            a[0] = 0;
            a[1] = 1;
            for(int i=2;i<a.length;i++)
                a[i] = i-1;
            for(int i=2;i<a.length;i++)
                for(int j = 2*i;j<a.length;j+=i)
                    a[j] -= a[i];
        }
        public void lcmSum(long a[]){
            int sievePhi[] = new int[(int)1e6 + 1];
            sievePhi(sievePhi);
            a[0] = 0;
            for(int i=1;i<a.length;i++)
                for(int j = i;j<a.length;j+=i)
                    a[j] += (long)i*sievePhi[i];
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

    }
}


import java.io.*;
import java.util.*;
import static java.lang.Math.*;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class MarketingScheme {
    public static void main(String[] args) throws IOException {
        // InputStream inputStream = new FileInputStream("input.txt");
        // OutputStream outputStream = new FileOutputStream("output.txt");
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(in.nextInt(), in, out);
        out.close();
    }
    static class TaskA {
        long mod = (long)(1e9) + 7;
        long fact[];
        int depth[];
        int parentTable[][];
        int degree[];
        ArrayList<Integer> leaves;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int diam = 0;

        public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException {
            while(testNumber-->0){
                long x = in.nextLong();
                long y = in.nextLong();
                long c[] = new long[6];
                for(int i=0;i<6;i++)
                    c[i] = in.nextLong();
                long ans = 0;
                long ox = 0;
                long oy = 0;
                if(x!=0){
                    if(x>0){
                        if(y>0){
                            // c1
                            long direct = c[0]*x;
                            long indirect = c[1]*x + c[5]*x;
                            ans += min(direct , indirect);
                            ox+=x;
                            oy+=y;
                        }
                        else{
                            // c6
                            long direct = c[5]*x;
                            long indirect = c[4]*x + c[0]*x;
                            ans += min(direct , indirect);
                            ox+=x;
                        }
                    }
                    else if(x<0){
                        if(y>0){
                            // c3
                            long direct = c[2]*(-x);
                            long indirect = c[1]*(-x) + c[3]*(-x);
                            ans += min(direct , indirect);
                            ox+=x;
                        }
                        else{
                            // c4
                            long direct = c[3]*(-x);
                            long indirect = c[4]*(-x) + c[2]*(-x);
                            ans += min(direct , indirect);
                            ox+=x;
                            oy+=y;
                        }
                    }
                }
                // out.println(ox + " " + oy + " " + x + " " + y + " " + ans);
                if(oy == y);
                else if(y>0){
                    long direct = c[1]*y;
                    long indirect = c[0]*y + c[2]*y;
                    ans += min(direct , indirect);
                }
                else if(y<0){
                    long direct = c[4]*(-y);
                    long indirect = c[5]*(-y) + c[3]*(-y);
                    ans += min(direct , indirect);
                }
                out.println(ans);
            }
        }

        public int lower(ArrayList<Integer> a , int x){
            if(a.size() == 0)
                return 0;
            if(a.get(a.size()-1) < x)
                return a.size();
            int l = 0;
            int r = a.size()-1;
            while(l<=r){
                int mid = (l+r)>>1;
                if(a.get(mid)>=x && (mid<1 || a.get(mid-1)<x))
                    return mid;
                else if(a.get(mid) < x)
                    l = mid+1;
                else
                    r = mid-1;
            }
            return -1;
        }

        public Node left(Node x){
            if(x == null || x.empty)
                return x;
            Node y = left(x.left);
            x.left = y;
            return y;
        }

        public Node right(Node x){
            if(x == null || x.empty)
                return x;
            Node y = right(x.right);
            x.right = y;
            return y;
        }

        class Node{
            int val;
            Node left;
            Node right;
            boolean empty;
            public Node(int val){
                this.val = val;
                left = null;
                right = null;
                empty = true;
            }
        }

        public Node add(int a[] , int start , int end){
            if(start>end)
                return null;
            int mid = (start+end)>>1;
            Node root = new Node(a[mid]);
            root.left = add(a , start , mid-1);
            root.right = add(a ,mid+1 , end);
            return root;
        }

        public void dfs(ArrayList<ArrayList<int[]>> a , int index , int parent , int val , int visited[]){
            if(index == a.size()-1){
                max = Math.max(max , val);
                return;
            }
            visited[index] = 1;
            for(int i=0;i<a.get(index).size();i++){
                int u = index;
                int v = a.get(index).get(i)[0];
                int speed = a.get(index).get(i)[1];
                if(visited[v] == 1){
                    if(v == a.size()-1){
                        max = Math.max(max , Math.min(speed , val));
                    }
                    continue;
                }
                dfs(a , v , u , Math.min(val , speed) , visited);
            }
        }

        class Pair{
            int u;
            int v;
            int speed;
            public Pair(int u , int v , int speed){
                this.u = u;
                this.v = v;
                this.speed = speed;
            }
        }

        public void add(HashMap<Integer , ArrayList<Integer>> m , int x , int y){
            if(!m.containsKey(x))
                m.put(x , new ArrayList<>());
            ArrayList<Integer> a = m.get(x);
            a.add(y);
        }

        public void dfs(ArrayList<ArrayList<Integer>> a , int index , int parent , int size[]){
            size[index] = 1;
            depth[index] = 1+depth[parent];
            for(int i=0;i<a.get(index).size();i++){
                if(a.get(index).get(i) == parent)
                    continue;
                dfs(a , a.get(index).get(i) , index , size);
                size[index] += size[a.get(index).get(i)];
            }
        }

        public int distance(ArrayList<ArrayList<Integer>> a , int u , int v){
            return depth[u]+depth[v] - 2*depth[lca(a , u , v)];
        }

        public int lca(ArrayList<ArrayList<Integer>> a , int u , int v){
            if(depth[v]<depth[u]){
                int x = u;
                u = v;
                v = x;
            }
            int diff = depth[v] - depth[u];
            for(int i=0;i<parentTable[v].length;i++){
                // checking whether the ith bit is set in the diff variable
                if(((diff>>i)&1) == 1)
                    v = parentTable[v][i];
            }
            if(v == u)
                return v;
            for(int i=parentTable[v].length-1;i>=0;i--){
                if(parentTable[u][i] != parentTable[v][i]){
                    v = parentTable[v][i];
                    u = parentTable[u][i];
                }
            }
            return parentTable[u][0];
        }

        public int[][] multiply(int a[][] , int b[][]){
            int c[][] = new int[a.length][b[0].length];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b[0].length;j++){
                    for(int k=0;k<b.length;k++)
                        c[i][j] += a[i][k]*b[k][j];
                }
            }
            return c;
        }

        public int[][] multiply(int a[][] , int b[][] , int mod){
            int c[][] = new int[a.length][b[0].length];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<b[0].length;j++){
                    for(int k=0;k<b.length;k++){
                        c[i][j] += a[i][k]*b[k][j];
                        c[i][j]%=mod;
                    }
                }
            }
            return c;
        }

        public int[][] pow(int a[][] , long b){
            int res[][] = new int[a.length][a[0].length];
            for(int i=0;i<a.length;i++)
                res[i][i] = 1;
            while(b>0){
                if((b&1) == 1)
                    res = multiply(res , a , 10);
                a = multiply(a , a , 10);
                b>>=1;
            }
            return res;
        }

        // for the min max problems
        public void build(int lookup[][] , int arr[], int n) {
            for (int i = 0; i < n; i++)
                lookup[i][0] = arr[i];
            for (int j = 1; (1 << j) <= n; j++) {
                for (int i = 0; (i + (1 << j) - 1) < n; i++) {
                    if (lookup[i][j - 1] > lookup[i + (1 << (j - 1))][j - 1])
                        lookup[i][j] = lookup[i][j - 1];
                    else
                        lookup[i][j] = lookup[i + (1 << (j - 1))][j - 1];
                }
            }
        }
        public int query(int lookup[][] , int L, int R) {
            int j = (int)(Math.log(R - L + 1)/Math.log(2));
            if (lookup[L][j] >= lookup[R - (1 << j) + 1][j])
                return lookup[L][j];
            else
                return lookup[R - (1 << j) + 1][j];
        }

        // for printing purposes
        public void print1d(int a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print1d(long a[] , PrintWriter out){
            for(int i=0;i<a.length;i++)
                out.print(a[i] + " ");
            out.println();
        }
        public void print2d(int a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            // out.println();
        }
        public void print2d(long a[][] , PrintWriter out){
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++)
                    out.print(a[i][j] + " ");
                out.println();
            }
            // out.println();
        }

        public void sieve(int a[]){
            a[0] = a[1] = 1;
            int i;
            for(i=2;i*i<=a.length;i++){
                if(a[i] != 0)
                    continue;
                a[i] = i;
                for(int k = (i)*(i);k<a.length;k+=i){
                    if(a[k] != 0)
                        continue;
                    a[k] = i;
                }
            }
        }

        public long nCrPFermet(int n , int r , long p){
            if(r==0)
                return 1l;
            // long fact[] = new long[n+1];
            // fact[0] = 1;
            // for(int i=1;i<=n;i++)
            //     fact[i] = (i*fact[i-1])%p;
            long modInverseR = pow(fact[r] , p-2 , p);
            long modInverseNR = pow(fact[n-r] , p-2 , p);
            long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p;
            return w;
        }
        public long pow(long a, long b, long m) {
            a %= m;
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a % m;
                a = a * a % m;
                b >>= 1;
            }
            return res;
        }
        public long pow(long a, long b) {
            long res = 1;
            while (b > 0) {
                long x = b&1;
                if (x == 1)
                    res = res * a;
                a = a * a;
                b >>= 1;
            }
            return res;
        }
        public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) {
            if (start > end) {
                return;
            }
            int mid = (start + end) / 2;
            a.add(mid);
            sortedArrayToBST(a, start, mid - 1);
            sortedArrayToBST(a, mid + 1, end);
        }
        class Combine{
            int value;
            int delete;
            Combine(int val , int delete){
                this.value = val;
                this.delete = delete;
            }
        }
        class Sort2 implements Comparator<Combine>{
            public int compare(Combine a , Combine b){
                if(a.value > b.value)
                    return 1;
                else if(a.value == b.value && a.delete>b.delete)
                    return 1;
                else if(a.value == b.value && a.delete == b.delete)
                    return 0;
                return -1;
            }
        }
        public int lowerLastBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>=x)
                return -1;
            if(a.get(r)<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid-1)<x)
                    return mid-1;
                else if(a.get(mid)>=x)
                    r = mid-1;
                else if(a.get(mid)<x && a.get(mid+1)>=x)
                    return mid;
                else if(a.get(mid)<x && a.get(mid+1)<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(ArrayList<Integer> a , int x){
            int l = 0;
            int r = a.size()-1;
            if(a.get(l)>x)
                return l;
            if(a.get(r)<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a.get(mid) == x && a.get(mid+1)>x)
                    return mid+1;
                else if(a.get(mid)<=x)
                    l = mid+1;
                else if(a.get(mid)>x && a.get(mid-1)<=x)
                    return mid;
                else if(a.get(mid)>x && a.get(mid-1)>x)
                    r = mid-1;
            }
            return mid;
        }
        public int lowerLastBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>=x)
                return -1;
            if(a[r]<x)
                return r;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid-1]<x)
                    return mid-1;
                else if(a[mid]>=x)
                    r = mid-1;
                else if(a[mid]<x && a[mid+1]>=x)
                    return mid;
                else if(a[mid]<x && a[mid+1]<x)
                    l = mid+1;
            }
            return mid;
        }
        public int upperFirstBound(long a[] , long x){
            int l = 0;
            int r = a.length-1;
            if(a[l]>x)
                return l;
            if(a[r]<=x)
                return r+1;
            int mid = -1;
            while(l<=r){
                mid = (l+r)/2;
                if(a[mid] == x && a[mid+1]>x)
                    return mid+1;
                else if(a[mid]<=x)
                    l = mid+1;
                else if(a[mid]>x && a[mid-1]<=x)
                    return mid;
                else if(a[mid]>x && a[mid-1]>x)
                    r = mid-1;
            }
            return mid;
        }
        public long log(float number , int base){
            return (long) Math.ceil((Math.log(number) / Math.log(base)) + 1e-9);
        }
        public long gcd(long a , long b){
            if(a<b){
                long c = b;
                b = a;
                a = c;
            }
            while(b!=0){
                long c = a;
                a = b;
                b = c%a;
            }
            return a;
        }
        public long[] gcdEx(long p, long q) {
            if (q == 0)
                return new long[] { p, 1, 0 };
            long[] vals = gcdEx(q, p % q);
            long d = vals[0];
            long a = vals[2];
            long b = vals[1] - (p / q) * vals[2];
            // 0->gcd 1->xValue 2->yValue
            return new long[] { d, a, b };
        }
        public void sievePhi(int a[]){
            a[0] = 0;
            a[1] = 1;
            for(int i=2;i<a.length;i++)
                a[i] = i-1;
            for(int i=2;i<a.length;i++)
                for(int j = 2*i;j<a.length;j+=i)
                    a[j] -= a[i];
        }
        public void lcmSum(long a[]){
            int sievePhi[] = new int[(int)1e6 + 1];
            sievePhi(sievePhi);
            a[0] = 0;
            for(int i=1;i<a.length;i++)
                for(int j = i;j<a.length;j+=i)
                    a[j] += (long)i*sievePhi[i];
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

    }
}


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Stack;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class D {
  int USE_FILE = 1;
  int HAS_TESTCASES = 1;
  String INPUT = "1 2 6 7";
  void solve(int caseNr) {
    int x = in.nextInt();
    int y = in.nextInt();
    long c1 = in.nextLong();
    long c2 = in.nextLong();
    long c3 = in.nextLong();
    long c4 = in.nextLong();
    long c5 = in.nextLong();
    long c6 = in.nextLong();
    long[] costs = new long[] { c1, c2, c3, c4, c5, c6 };
    long[] moves = new long[6 + 1];
    if (x < 0 && y < 0) {
      int c = -max(x, y);
      moves[4] = c;
      x += c;
      y += c;
    }
    if (x > 0 && y > 0) {
      int c = max(x, y);
      moves[1] = c;
      x -= c;
      y -= c;
    }
    if (x < 0)
      moves[3] = -x;
    if (x > 0)
      moves[6] = x;
    if (y < 0)
      moves[5] = -y;
    if (y > 0)
      moves[2] = y;
    long answer = 0;
    answer += min(c1 * moves[1], (c2 + c6) * moves[1]);
    answer += min(c4 * moves[4], (c3 + c5) * moves[4]);
    answer += min(c3 * moves[3], (c2 + c4) * moves[3]);
    answer += min(c6 * moves[6], (c1 + c5) * moves[6]);
    answer += min(c5 * moves[5], (c4 + c6) * moves[5]);
    answer += min(c2 * moves[2], (c1 + c3) * moves[2]);
    System.out.println(answer);
  }
  private int inf = Integer.MAX_VALUE;
  private int MOD = (int) 1e9 + 7;
  static boolean LOCAL = java.lang.System.getProperty("ONLINE_JUDGE") == null;
  static Scanner in;
  static PrintWriter output_writer = new PrintWriter(java.lang.System.out);
  static class System {
    static PrintWriter out = null;
  }
  void setup() throws Exception {
    System.out = output_writer;
    if (LOCAL && USE_FILE == 0) {
      java.lang.System.setIn(new ByteArrayInputStream(INPUT.getBytes()));
      in = new Scanner(java.lang.System.in);
    }
    if (LOCAL && USE_FILE > 0) {
      String filename = String.format("in/cf%d.txt", USE_FILE);
      File inputFile = new File(filename);
      in = new Scanner(inputFile);
      System.out.printf("[%s]\n", filename);
    }
    if (!LOCAL) {
      in = new Scanner(java.lang.System.in);
    }
    long startTime = java.lang.System.currentTimeMillis();
    int t = HAS_TESTCASES > 0 ? in.nextInt() : 1;
    for (int i = 1; i <= t; i++) {
      solve(i);
    }
    in.close();
    if (LOCAL) {
      System.out.printf("[%dms]\n", java.lang.System.currentTimeMillis() - startTime);
    }
    output_writer.flush();
  }
  public static void main(String[] args) throws Exception {
    new D().setup();
  }
}


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class D {
  int USE_FILE = 1;
  int HAS_TESTCASES = 1;
  String INPUT = "1 2 6 7";
  void solve(int caseNr) {
    int x = in.nextInt();
    int y = in.nextInt();
    long c1 = in.nextLong();
    long c2 = in.nextLong();
    long c3 = in.nextLong();
    long c4 = in.nextLong();
    long c5 = in.nextLong();
    long c6 = in.nextLong();
    long[] moves = new long[6 + 1];
    if (x < 0 && y < 0) {
      int c = -max(x, y);
      moves[4] = c;
      x += c;
      y += c;
    }
    if (x > 0 && y > 0) {
      int c = min(x, y);
      moves[1] = c;
      x -= c;
      y -= c;
    }
    if (x < 0)
      moves[3] = -x;
    if (x > 0)
      moves[6] = x;
    if (y < 0)
      moves[5] = -y;
    if (y > 0)
      moves[2] = y;
    long answer = 0;
    answer += min(c1 * moves[1], (c2 + c6) * moves[1]);
    answer += min(c4 * moves[4], (c3 + c5) * moves[4]);
    answer += min(c3 * moves[3], (c2 + c4) * moves[3]);
    answer += min(c6 * moves[6], (c1 + c5) * moves[6]);
    answer += min(c5 * moves[5], (c4 + c6) * moves[5]);
    answer += min(c2 * moves[2], (c1 + c3) * moves[2]);
    System.out.println(answer);
  }
  static boolean LOCAL = java.lang.System.getProperty("ONLINE_JUDGE") == null;
  static Scanner in;
  static PrintWriter output_writer = new PrintWriter(java.lang.System.out);
  static class System {
    static PrintWriter out = null;
  }
  void setup() throws Exception {
    System.out = output_writer;
    if (LOCAL && USE_FILE == 0) {
      java.lang.System.setIn(new ByteArrayInputStream(INPUT.getBytes()));
      in = new Scanner(java.lang.System.in);
    }
    if (LOCAL && USE_FILE > 0) {
      String filename = String.format("in/cf%d.txt", USE_FILE);
      File inputFile = new File(filename);
      in = new Scanner(inputFile);
      System.out.printf("[%s]\n", filename);
    }
    if (!LOCAL) {
      in = new Scanner(java.lang.System.in);
    }
    long startTime = java.lang.System.currentTimeMillis();
    int t = HAS_TESTCASES > 0 ? in.nextInt() : 1;
    for (int i = 1; i <= t; i++) {
      solve(i);
    }
    in.close();
    if (LOCAL) {
      System.out.printf("[%dms]\n", java.lang.System.currentTimeMillis() - startTime);
    }
    output_writer.flush();
  }
  public static void main(String[] args) throws Exception {
    new D().setup();
  }
}


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Stack;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class D {
  int USE_FILE = 1;
  int HAS_TESTCASES = 1;
  String INPUT = "1 2 6 7";
  void solve(int caseNr) {
    int x = in.nextInt();
    int y = in.nextInt();
    long c1 = in.nextLong();
    long c2 = in.nextLong();
    long c3 = in.nextLong();
    long c4 = in.nextLong();
    long c5 = in.nextLong();
    long c6 = in.nextLong();
    long[] costs = new long[] { c1, c2, c3, c4, c5, c6 };
    long[] moves = new long[6 + 1];
    if (x < 0 && y < 0) {
      int c = -max(x, y);
      moves[4] = c;
      x += c;
      y += c;
    }
    if (x > 0 && y > 0) {
      int c = min(x, y);
      moves[1] = c;
      x -= c;
      y -= c;
    }
    if (x < 0)
      moves[3] = -x;
    if (x > 0)
      moves[6] = x;
    if (y < 0)
      moves[5] = -y;
    if (y > 0)
      moves[2] = y;
    long answer = 0;
    answer += min(c1 * moves[1], (c2 + c6) * moves[1]);
    answer += min(c4 * moves[4], (c3 + c5) * moves[4]);
    answer += min(c3 * moves[3], (c2 + c4) * moves[3]);
    answer += min(c6 * moves[6], (c1 + c5) * moves[6]);
    answer += min(c5 * moves[5], (c4 + c6) * moves[5]);
    answer += min(c2 * moves[2], (c1 + c3) * moves[2]);
    System.out.println(answer);
  }
  private int inf = Integer.MAX_VALUE;
  private int MOD = (int) 1e9 + 7;
  static boolean LOCAL = java.lang.System.getProperty("ONLINE_JUDGE") == null;
  static Scanner in;
  static PrintWriter output_writer = new PrintWriter(java.lang.System.out);
  static class System {
    static PrintWriter out = null;
  }
  void setup() throws Exception {
    System.out = output_writer;
    if (LOCAL && USE_FILE == 0) {
      java.lang.System.setIn(new ByteArrayInputStream(INPUT.getBytes()));
      in = new Scanner(java.lang.System.in);
    }
    if (LOCAL && USE_FILE > 0) {
      String filename = String.format("in/cf%d.txt", USE_FILE);
      File inputFile = new File(filename);
      in = new Scanner(inputFile);
      System.out.printf("[%s]\n", filename);
    }
    if (!LOCAL) {
      in = new Scanner(java.lang.System.in);
    }
    long startTime = java.lang.System.currentTimeMillis();
    int t = HAS_TESTCASES > 0 ? in.nextInt() : 1;
    for (int i = 1; i <= t; i++) {
      solve(i);
    }
    in.close();
    if (LOCAL) {
      System.out.printf("[%dms]\n", java.lang.System.currentTimeMillis() - startTime);
    }
    output_writer.flush();
  }
  public static void main(String[] args) throws Exception {
    new D().setup();
  }
}


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Stack;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class D {
  int USE_FILE = 1;
  int HAS_TESTCASES = 1;
  String INPUT = "1 2 6 7";
  void solve(int caseNr) {
    int x = in.nextInt();
    int y = in.nextInt();
    long c1 = in.nextLong();
    long c2 = in.nextLong();
    long c3 = in.nextLong();
    long c4 = in.nextLong();
    long c5 = in.nextLong();
    long c6 = in.nextLong();
    if (caseNr != 3)
      return;
    long[] costs = new long[] { c1, c2, c3, c4, c5, c6 };
    long[] moves = new long[6 + 1];
    if (x < 0 && y < 0) {
      int c = -max(x, y);
      moves[4] = c;
      x += c;
      y += c;
    }
    if (x > 0 && y > 0) {
      int c = min(x, y);
      moves[1] = c;
      x -= c;
      y -= c;
    }
    if (x < 0)
      moves[3] = -x;
    if (x > 0)
      moves[6] = x;
    if (y < 0)
      moves[5] = -y;
    if (y > 0)
      moves[2] = y;
    long answer = 0;
    answer += min(c1 * moves[1], (c2 + c6) * moves[1]);
    answer += min(c4 * moves[4], (c3 + c5) * moves[4]);
    answer += min(c3 * moves[3], (c2 + c4) * moves[3]);
    answer += min(c6 * moves[6], (c1 + c5) * moves[6]);
    answer += min(c5 * moves[5], (c4 + c6) * moves[5]);
    answer += min(c2 * moves[2], (c1 + c3) * moves[2]);
    System.out.println(answer);
  }
  private int inf = Integer.MAX_VALUE;
  private int MOD = (int) 1e9 + 7;
  static boolean LOCAL = java.lang.System.getProperty("ONLINE_JUDGE") == null;
  static Scanner in;
  static PrintWriter output_writer = new PrintWriter(java.lang.System.out);
  static class System {
    static PrintWriter out = null;
  }
  void setup() throws Exception {
    System.out = output_writer;
    if (LOCAL && USE_FILE == 0) {
      java.lang.System.setIn(new ByteArrayInputStream(INPUT.getBytes()));
      in = new Scanner(java.lang.System.in);
    }
    if (LOCAL && USE_FILE > 0) {
      String filename = String.format("in/cf%d.txt", USE_FILE);
      File inputFile = new File(filename);
      in = new Scanner(inputFile);
      System.out.printf("[%s]\n", filename);
    }
    if (!LOCAL) {
      in = new Scanner(java.lang.System.in);
    }
    long startTime = java.lang.System.currentTimeMillis();
    int t = HAS_TESTCASES > 0 ? in.nextInt() : 1;
    for (int i = 1; i <= t; i++) {
      solve(i);
    }
    in.close();
    if (LOCAL) {
      System.out.printf("[%dms]\n", java.lang.System.currentTimeMillis() - startTime);
    }
    output_writer.flush();
  }
  public static void main(String[] args) throws Exception {
    new D().setup();
  }
}


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

public class D {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int t = sc.nextInt();
		while(t-- > 0) {
			long x = sc.nextInt();
			long y = sc.nextInt();
			long[] c = new long[6];
			for(int i = 0; i < 6; i++) c[i] = sc.nextLong();
			if(c[0] > c[5] + c[1]) c[0] = c[5] + c[1];
			if(c[1] > c[0] + c[2]) c[1] = c[0] + c[2];
			if(c[2] > c[1] + c[3]) c[2] = c[1] + c[3];
			if(c[3] > c[2] + c[4]) c[3] = c[2] + c[4];
			if(c[4] > c[3] + c[5]) c[4] = c[3] + c[5];
			if(c[5] > c[4] + c[0]) c[5] = c[4] + c[0];
			long[] ds;
			if(x >= 0) {
				if(y <= 0) {
					ds = new long[] {0,0,0,0,-y,x};
				}
				else if(x >= y) {
					ds = new long[] {y,0,0,0,0,x-y};
				}
				else {
					ds = new long[] {x,y-x,0,0,0,0};
				}
			}
			else {
				if(y >= 0) {
					ds = new long[] {0,y,-x,0,0,0};
				}
				else if(x <= y) {
					ds = new long[] {0,0,y-x,-y,0,0};
				}
				else {
					ds = new long[] {0,0,0,-x,x-y,0};
				}
			}
			long res = 0;
			for(int i = 0; i < 6; i++) {
				res += c[i]*ds[i];
			}
			System.out.println(res);
		}

	}

	static class FastScanner {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public FastScanner() {
            reader = new BufferedReader(new InputStreamReader(System.in), 32768);
            tokenizer = null;
        }
        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
        	return Long.parseLong(next());
        }
        public double nextDouble() {
        	return Double.parseDouble(next());
        }
        public String nextLine() {
        	try {
        		return reader.readLine();
        	} catch(IOException e) {
        		throw new RuntimeException(e);
        	}
        }
    }

}


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

public class Solution {

    public static boolean useInFile = false;
    public static boolean useOutFile = false;

    public static void main(String args[]) throws IOException {
        InOut inout = new InOut();
        Resolver resolver = new Resolver(inout);
        resolver.solve();
        inout.flush();
    }

    private static class Resolver {

        final long LONG_INF = (long) 1e18;
        final int INF = (int) (1e9 + 7);
        final int MOD = 998244353;
        long f[], inv[];
        InOut inout;

        Resolver(InOut inout) {
            this.inout = inout;
        }

        void initF(int n, int mod) {
            f = new long[n + 1];
            f[1] = 1;
            for (int i = 2; i <= n; i++) {
                f[i] = (f[i - 1] * i) % mod;
            }
        }

        void initInv(int n, int mod) {
            inv = new long[n + 1];
            inv[n] = pow(f[n], mod - 2, mod);
            for (int i = inv.length - 2; i >= 0; i--) {
                inv[i] = inv[i + 1] * (i + 1) % mod;
            }
        }

        long cmn(int n, int m, int mod) {
            return f[n] * inv[m] % mod * inv[n - m] % mod;
        }

        int d[] = {0, -1, 0, 1, 0};

        boolean legal(int r, int c, int n, int m) {
            return r >= 0 && r < n && c >= 0 && c < m;
        }

        void dfs(int f, int p, boolean vis[]) {
            List<Integer> es = adj[f];
            vis[f] = true;
            for (int i = 0; i < es.size(); i++) {
                int t = es.get(i);
                if (vis[t]) {
                    continue;
                }
                dfs(t, f, vis);
            }
        }

        void solve() throws IOException {
            int tt = 1;
            boolean hvt = true;
            if (hvt) {
                tt = nextInt();
//                tt = Integer.parseInt(nextLine());
            }
//            initF(2001, INF);
//            initInv(2001, INF);

            for (int cs = 1; cs <= tt; cs++) {
                long rs = 0;
//                int n = nextInt();
                long x = nextInt();
                long y = nextInt();
                long c[] = new long[7];
                for (int i = 0; i < 6; i++) {
                    c[i + 1] = nextInt();
                }
                long perx = 0;
                long pery = 0;
                long absx = Math.abs(x);
                long absy = Math.abs(y);
                if (x <= 0 && y >= 0) {
                    perx = Math.min(c[3], c[2] + c[4]);
                    pery = Math.min(c[2], c[1] + c[3]);
                    rs = perx * absx + pery * absy;
                } else if (x <= 0) {
                    perx = Math.min(c[4], c[5] + c[3]);
                    if (absx <= absy) {
                        pery = Math.min(c[5], c[6] + c[4]);
                        rs = perx * absx + pery * (absy - absx);
                    } else {
                        pery = Math.min(c[2], c[1] + c[3]);
                        long min1 = perx * absx + pery * (absx + absx - absy);
                        long min2 = perx * absy;
                        perx = Math.min(c[3], c[2] + c[4]);
                        min2 += perx * (absx - absy);
                        rs = Math.min(min1, min2);
                    }
                } else if (y <= 0) {
                    perx = Math.min(c[6], c[1] + c[5]);
                    pery = Math.min(c[5], c[6] + c[4]);
                    rs = perx * absx + pery * absy;
                } else {
                    perx = Math.min(c[1], c[6] + c[2]);
                    if (absx <= absy) {
                        pery = Math.min(c[2], c[1] + c[3]);
                        rs = perx * absx + pery * (absy - absx);
                    } else {
                        pery = Math.min(c[5], c[6] + c[4]);
                        long min1 = perx * absx + pery * (absx + absx - absy);
                        long min2 = perx * absy;
                        perx = Math.min(c[6], c[1] + c[5]);
                        min2 += perx * (absx - absy);
                        rs = Math.min(min1, min2);
                    }
                }

                format("%d", rs);
//                format("Case #%d: %d", cs, rs);

                if (cs < tt) {
                    format("\n");
//                    format(" ");
                }
//                flush();
            }

            int testtest = 0;
        }

        private void updateSegTree(int n, long l, SegmentTree lft) {
            long lazy;
            lazy = 1;
            for (int j = 1; j <= l; j++) {
                lazy = (lazy + cmn((int) l, j, INF)) % INF;
                lft.modify(1, j, j, lazy);
            }
            lft.modify(1, (int) (l + 1), n, lazy);
        }

        String next() throws IOException {
            return inout.next();
        }

        String next(int n) throws IOException {
            return inout.next(n);
        }

        String nextLine() throws IOException {
            return inout.nextLine();
        }

        int nextInt() throws IOException {
            return inout.nextInt();
        }

        long nextLong(int n) throws IOException {
            return inout.nextLong(n);
        }

        int[] anInt(int i, int j) throws IOException {
            int a[] = new int[j + 1];
            for (int k = i; k <= j; k++) {
                a[k] = nextInt();
            }
            return a;
        }

        long[] anLong(int i, int j, int len) throws IOException {
            long a[] = new long[j + 1];
            for (int k = i; k <= j; k++) {
                a[k] = nextLong(len);
            }
            return a;
        }

        void print(String s, boolean nextLine) {
            inout.print(s, nextLine);
        }

        void format(String format, Object... obj) {
            inout.format(format, obj);
        }

        void flush() {
            inout.flush();
        }

        void swap(long a[], int i, int j) {
            a[i] ^= a[j];
            a[j] ^= a[i];
            a[i] ^= a[j];
        }

        int getP(int x, int p[]) {
            if (p[x] == 0 || p[x] == x) {
                return x;
            }
            return p[x] = getP(p[x], p);
        }

        void union(int x, int y, int p[]) {
            if (x < y) {
                p[y] = x;
            } else {
                p[x] = y;
            }
        }

        boolean topSort() {
            int n = adj2.length - 1;
            int d[] = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                for (int j = 0; j < adj2[i].size(); j++) {
                    d[adj2[i].get(j)[0]]++;
                }
            }
            List<Integer> list = new ArrayList<>();
            for (int i = 1; i <= n; i++) {
                if (d[i] == 0) {
                    list.add(i);
                }
            }
            for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < adj2[list.get(i)].size(); j++) {
                    int t = adj2[list.get(i)].get(j)[0];
                    d[t]--;
                    if (d[t] == 0) {
                        list.add(t);
                    }
                }
            }
            return list.size() == n;
        }

        class SegmentTreeNode {
            long defaultVal = 0;
            int l, r;
            long val = defaultVal, lazy = defaultVal;

            SegmentTreeNode(int l, int r) {
                this.l = l;
                this.r = r;
            }
        }

        class SegmentTree {
            SegmentTreeNode tree[];
            long inf = Long.MIN_VALUE;
            long c[];

            SegmentTree(int n) {
                assert n > 0;
                tree = new SegmentTreeNode[n * 3 + 1];
            }

            void setAn(long cn[]) {
                c = cn;
            }

            SegmentTree build(int k, int l, int r) {
                if (l > r) {
                    return this;
                }
                if (null == tree[k]) {
                    tree[k] = new SegmentTreeNode(l, r);
                }
                if (l == r) {
                    return this;
                }
                int mid = (l + r) >> 1;
                build(k << 1, l, mid);
                build(k << 1 | 1, mid + 1, r);
                return this;
            }

            void pushDown(int k) {
                if (tree[k].l == tree[k].r) {
                    return;
                }
                long lazy = tree[k].lazy;
                tree[k << 1].val = ((c[tree[k << 1].l] - c[tree[k << 1].r + 1] + MOD) % MOD * lazy) % MOD;
                tree[k << 1].lazy = lazy;
                tree[k << 1 | 1].val = ((c[tree[k << 1 | 1].l] - c[tree[k << 1 | 1].r + 1] + MOD) % MOD * lazy) % MOD;
                tree[k << 1 | 1].lazy = lazy;
                tree[k].lazy = 0;
            }

            void modify(int k, int l, int r, long val) {
                if (tree[k].l >= l && tree[k].r <= r) {
                    tree[k].val = ((c[tree[k].l] - c[tree[k].r + 1] + MOD) % MOD * val) % MOD;
                    tree[k].lazy = val;
                    return;
                }
                int mid = (tree[k].l + tree[k].r) >> 1;
                if (tree[k].lazy != 0) {
                    pushDown(k);
                }
                if (mid >= l) {
                    modify(k << 1, l, r, val);
                }
                if (mid + 1 <= r) {
                    modify(k << 1 | 1, l, r, val);
                }
                tree[k].val = (tree[k << 1].val + tree[k << 1 | 1].val) % MOD;
            }

            long query(int k, int l, int r) {
                if (tree[k].l > r || tree[k].r < l) {
                    return 0;
                }
                if (tree[k].lazy != 0) {
                    pushDown(k);
                }
                if (tree[k].l >= l && tree[k].r <= r) {
                    return tree[k].val;
                }
                long ans = (query(k << 1, l, r) + query(k << 1 | 1, l, r)) % MOD;
                if (tree[k].l < tree[k].r) {
                    tree[k].val = (tree[k << 1].val + tree[k << 1 | 1].val) % MOD;
                }
                return ans;
            }
        }

        class BinaryIndexedTree {

            int n = 1;
            long C[];

            BinaryIndexedTree(int sz) {
                while (n <= sz) {
                    n <<= 1;
                }
                n = sz + 1;
                C = new long[n];
            }

            int lowbit(int x) {
                return x & -x;
            }

            void add(int x, long val) {
                while (x < n) {
                    C[x] += val;
                    x += lowbit(x);
                }
            }

            long getSum(int x) {
                long res = 0;
                while (x > 0) {
                    res += C[x];
                    x -= lowbit(x);
                }
                return res;
            }

            int binSearch(long sum) {
                if (sum == 0) {
                    return 0;
                }
                int n = C.length;
                int mx = 1;
                while (mx < n) {
                    mx <<= 1;
                }
                int res = 0;
                for (int i = mx / 2; i >= 1; i >>= 1) {
                    if (C[res + i] < sum) {
                        sum -= C[res + i];
                        res += i;
                    }
                }
                return res + 1;
            }
        }

        int mnS;
        int mxS;
        Map<Integer, TrieNode> SS = new HashMap<>();

        class TrieNode {
            int cnt = 0;
            boolean isMin = false;
            boolean isMax = false;
            int mnIdx = 26;
            int mxIdx = -1;

            TrieNode next[];

            TrieNode() {
                next = new TrieNode[26];
            }

            private void insert(TrieNode trie, char ch[], int i, int j) {
                while (i < ch.length) {
                    int idx = ch[i] - 'a';
                    if (null == trie.next[idx]) {
                        trie.next[idx] = new TrieNode();
                    }
                    trie.cnt++;
                    int lastMnIdx = trie.mnIdx;
                    int lastMxIdx = trie.mxIdx;
                    trie.mnIdx = Math.min(trie.mnIdx, idx);
                    trie.mxIdx = Math.max(trie.mxIdx, idx);
                    if (trie.isMin && lastMnIdx != trie.mnIdx) {
                        if (lastMnIdx != 26) {
                            notMin(trie.next[lastMnIdx]);
                        }
                        trie.next[trie.mnIdx].isMin = true;
                    }
                    if (trie.isMax && lastMxIdx != trie.mxIdx) {
                        if (lastMxIdx >= 0) {
                            notMax(trie.next[lastMxIdx]);
                        }
                        trie.next[trie.mxIdx].isMax = true;
                    }
                    trie = trie.next[idx];
                    i++;
                }
                SS.put(j + 1, trie);
                if (trie.cnt == 0) {
                    if (trie.isMin) {
                        mnS = j + 1;
                    }
                    if (trie.isMax) {
                        mxS = j + 1;
                    }
                }
            }

            private void notMin(TrieNode trie) {
                while (null != trie) {
                    trie.isMin = false;
                    int val = trie.mnIdx;
                    if (val >= 26 || val < 0 || null == trie.next[val]) {
                        break;
                    }
                    trie = trie.next[val];
                }
            }

            private void notMax(TrieNode trie) {
                while (null != trie) {
                    trie.isMax = false;
                    int val = trie.mxIdx;
                    if (val >= 26 || val < 0 || null == trie.next[val]) {
                        break;
                    }
                    trie = trie.next[val];
                }
            }
        }

        //Binary tree
        class TreeNode {
            int val;
            int tier = -1;
            TreeNode parent;
            TreeNode left;
            TreeNode right;

            TreeNode(int val) {
                this.val = val;
            }
        }

        //binary tree dfs
        void tierTree(TreeNode root) {
            if (null == root) {
                return;
            }
            if (null != root.parent) {
                root.tier = root.parent.tier + 1;
            } else {
                root.tier = 0;
            }
            tierTree(root.left);
            tierTree(root.right);
        }

        //LCA start
        TreeNode[][] lca;
        TreeNode[] tree;

        void lcaDfsTree(TreeNode root) {
            if (null == root) {
                return;
            }
            tree[root.val] = root;
            TreeNode nxt = root.parent;
            int idx = 0;
            while (null != nxt) {
                lca[root.val][idx] = nxt;
                nxt = lca[nxt.val][idx];
                idx++;
            }
            lcaDfsTree(root.left);
            lcaDfsTree(root.right);
        }

        TreeNode lcaTree(TreeNode root, int n, TreeNode x, TreeNode y) throws IOException {
            if (null == root) {
                return null;
            }
            if (-1 == root.tier) {
                tree = new TreeNode[n + 1];
                tierTree(root);
            }
            if (null == lca) {
                lca = new TreeNode[n + 1][31];
                lcaDfsTree(root);
            }
            int z = Math.abs(x.tier - y.tier);
            int xx = x.tier > y.tier ? x.val : y.val;
            while (z > 0) {
                final int zz = z;
                int l = (int) BinSearch.bs(0, 31
                        , k -> zz < (1 << k));
                xx = lca[xx][l].val;
                z -= 1 << l;
            }
            int yy = y.val;
            if (x.tier <= y.tier) {
                yy = x.val;
            }
            while (xx != yy) {
                final int xxx = xx;
                final int yyy = yy;
                int l = (int) BinSearch.bs(0, 31
                        , k -> (1 << k) <= tree[xxx].tier && lca[xxx][(int) k] != lca[yyy][(int) k]);
                xx = lca[xx][l].val;
                yy = lca[yy][l].val;
            }
            return tree[xx];
        }
        //LCA end

        //graph
        List<Integer> adj[];
        List<int[]> adj2[];

        void initGraph(int n, int m, boolean hasW, boolean directed, int type) throws IOException {
            if (type == 1) {
                adj = new List[n + 1];
            } else {
                adj2 = new List[n + 1];
            }
            for (int i = 1; i <= n; i++) {
                if (type == 1) {
                    adj[i] = new ArrayList<>();
                } else {
                    adj2[i] = new ArrayList<>();
                }
            }
            for (int i = 0; i < m; i++) {
                int f = nextInt();
                int t = nextInt();
                if (type == 1) {
                    adj[f].add(t);
                    if (!directed) {
                        adj[t].add(f);
                    }
                } else {
                    int w = hasW ? nextInt() : 0;
                    adj2[f].add(new int[]{t, w});
                    if (!directed) {
                        adj2[t].add(new int[]{f, w});
                    }
                }
            }
        }

        void getDiv(Map<Integer, Integer> map, long n) {
            int sqrt = (int) Math.sqrt(n);
            for (int i = 2; i <= sqrt; i++) {
                int cnt = 0;
                while (n % i == 0) {
                    cnt++;
                    n /= i;
                }
                if (cnt > 0) {
                    map.put(i, cnt);
                }
            }
            if (n > 1) {
                map.put((int) n, 1);
            }
        }

        boolean[] generatePrime(int n) {
            boolean p[] = new boolean[n + 1];
            p[2] = true;

            for (int i = 3; i <= n; i += 2) {
                p[i] = true;
            }

            for (int i = 3; i <= Math.sqrt(n); i += 2) {
                if (!p[i]) {
                    continue;
                }
                for (int j = i * i; j <= n; j += i << 1) {
                    p[j] = false;
                }
            }
            return p;
        }

        boolean isPrime(long n) { //determines if n is a prime number
            int p[] = {2, 3, 5, 233, 331};
            int pn = p.length;
            long s = 0, t = n - 1;//n - 1 = 2^s * t
            while ((t & 1) == 0) {
                t >>= 1;
                ++s;
            }
            for (int i = 0; i < pn; ++i) {
                if (n == p[i]) {
                    return true;
                }
                long pt = pow(p[i], t, n);
                for (int j = 0; j < s; ++j) {
                    long cur = llMod(pt, pt, n);
                    if (cur == 1 && pt != 1 && pt != n - 1) {
                        return false;
                    }
                    pt = cur;
                }
                if (pt != 1) {
                    return false;
                }
            }
            return true;
        }

        long[] llAdd2(long a[], long b[], long mod) {
            long c[] = new long[2];
            c[1] = (a[1] + b[1]) % (mod * mod);
            c[0] = (a[1] + b[1]) / (mod * mod) + a[0] + b[0];
            return c;
        }

        long[] llMod2(long a, long b, long mod) {
            long x1 = a / mod;
            long y1 = a % mod;
            long x2 = b / mod;
            long y2 = b % mod;
            long c = (x1 * y2 + x2 * y1) / mod;
            c += x1 * x2;
            long d = (x1 * y2 + x2 * y1) % mod * mod + y1 * y2;
            return new long[]{c, d};
        }

        long llMod(long a, long b, long mod) {
            if (a > mod || b > mod) {
                return (a * b - (long) ((double) a / mod * b + 0.5) * mod + mod) % mod;
            }
            return a * b % mod;
//        long r = 0;
//        a %= mod;
//        b %= mod;
//        while (b > 0) {
//            if ((b & 1) == 1) {
//                r = (r + a) % mod;
//            }
//            b >>= 1;
//            a = (a << 1) % mod;
//        }
//        return r;
        }

        long pow(long a, long n) {
            long ans = 1;
            while (n > 0) {
                if ((n & 1) == 1) {
                    ans = ans * a;
                }
                a = a * a;
                n >>= 1;
            }
            return ans;
        }

        long pow(long a, long n, long mod) {
            long ans = 1;
            while (n > 0) {
                if ((n & 1) == 1) {
                    ans = llMod(ans, a, mod);
                }
                a = llMod(a, a, mod);
                n >>= 1;
            }
            return ans;
        }

        private long[][] initC(int n) {
            long c[][] = new long[n][n];

            for (int i = 0; i < n; i++) {
                c[i][0] = 1;
            }

            for (int i = 1; i < n; i++) {
                for (int j = 1; j <= i; j++) {
                    c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
                }
            }
            return c;
        }

        /**
         * ps: n >= m, choose m from n;
         */
//        private int cmn(long n, long m) {
//            if (m > n) {
//                n ^= m;
//                m ^= n;
//                n ^= m;
//            }
//            m = Math.min(m, n - m);
//
//            long top = 1;
//            long bot = 1;
//            for (long i = n - m + 1; i <= n; i++) {
//                top = (top * i) % MOD;
//            }
//            for (int i = 1; i <= m; i++) {
//                bot = (bot * i) % MOD;
//            }
//
//            return (int) ((top * pow(bot, MOD - 2, MOD)) % MOD);
//        }

        long[] exGcd(long a, long b) {
            if (b == 0) {
                return new long[]{a, 1, 0};
            }
            long[] ans = exGcd(b, a % b);
            long x = ans[2];
            long y = ans[1] - a / b * ans[2];
            ans[1] = x;
            ans[2] = y;
            return ans;
        }

        long gcd(long a, long b) {
            if (a < b) {
                return gcd(b, a);
            }
            while (b != 0) {
                long tmp = a % b;
                a = b;
                b = tmp;
            }
            return a;
        }

        int[] unique(int a[], Map<Integer, Integer> idx) {
            int tmp[] = a.clone();
            Arrays.sort(tmp);
            int j = 0;
            for (int i = 0; i < tmp.length; i++) {
                if (i == 0 || tmp[i] > tmp[i - 1]) {
                    idx.put(tmp[i], j++);
                }
            }
            int rs[] = new int[j];
            j = 0;
            for (int key : idx.keySet()) {
                rs[j++] = key;
            }
            Arrays.sort(rs);
            return rs;
        }

        boolean isEven(long n) {
            return (n & 1) == 0;
        }

        static class BinSearch {

            static long bs(long l, long r, IBinSearch sort) throws IOException {
                while (l < r) {
                    long m = l + (r - l) / 2;
                    if (sort.binSearchCmp(m)) {
                        l = m + 1;
                    } else {
                        r = m;
                    }
                }
                return l;
            }

            interface IBinSearch {
                boolean binSearchCmp(long k) throws IOException;
            }

        }
    }

    private static class InOut {
        private BufferedReader br;
        private StreamTokenizer st;
        private PrintWriter pw;

        InOut() throws FileNotFoundException {
            if (useInFile) {
                System.setIn(new FileInputStream("resources/inout/in.text"));
            }
            if (useOutFile) {
                System.setOut(new PrintStream("resources/inout/out.text"));
            }
            br = new BufferedReader(new InputStreamReader(System.in));
            st = new StreamTokenizer(br);
            pw = new PrintWriter(new OutputStreamWriter(System.out));
            st.ordinaryChar('\'');
            st.ordinaryChar('\"');
            st.ordinaryChar('/');
        }

        private long[] anLong(int n) throws IOException {
            long a[] = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextInt();
            }
            return a;
        }

        private String next() throws IOException {
            st.nextToken();
            return st.sval;
        }

        private String next(int len) throws IOException {
            char ch[] = new char[len];
            int cur = 0;
            char c;
            while ((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t') ;
            do {
                ch[cur++] = c;
            } while (!((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t'));
            return String.valueOf(ch, 0, cur);
        }

        private int nextInt() throws IOException {
            st.nextToken();
            return (int) st.nval;
        }

        private long nextLong(int n) throws IOException {
            return Long.parseLong(next(n));
        }

        private double nextDouble() throws IOException {
            st.nextToken();
            return st.nval;
        }

        private String[] nextSS(String reg) throws IOException {
            return br.readLine().split(reg);
        }

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

        private void print(String s, boolean newLine) {
            if (null != s) {
                pw.print(s);
            }
            if (newLine) {
                pw.println();
            }
        }

        private void format(String format, Object... obj) {
            pw.format(format, obj);
        }

        private void flush() {
            pw.flush();
        }
    }

    private static class FFT {
        double[] roots;
        int maxN;

        public FFT(int maxN) {
            this.maxN = maxN;
            initRoots();
        }

        public long[] multiply(int[] a, int[] b) {
            int minSize = a.length + b.length - 1;
            int bits = 1;
            while (1 << bits < minSize) bits++;
            int N = 1 << bits;
            double[] aa = toComplex(a, N);
            double[] bb = toComplex(b, N);
            fftIterative(aa, false);
            fftIterative(bb, false);
            double[] c = new double[aa.length];
            for (int i = 0; i < N; i++) {
                c[2 * i] = aa[2 * i] * bb[2 * i] - aa[2 * i + 1] * bb[2 * i + 1];
                c[2 * i + 1] = aa[2 * i] * bb[2 * i + 1] + aa[2 * i + 1] * bb[2 * i];
            }
            fftIterative(c, true);
            long[] ret = new long[minSize];
            for (int i = 0; i < ret.length; i++) {
                ret[i] = Math.round(c[2 * i]);
            }
            return ret;
        }

        static double[] toComplex(int[] arr, int size) {
            double[] ret = new double[size * 2];
            for (int i = 0; i < arr.length; i++) {
                ret[2 * i] = arr[i];
            }
            return ret;
        }

        void initRoots() {
            roots = new double[2 * (maxN + 1)];
            double ang = 2 * Math.PI / maxN;
            for (int i = 0; i <= maxN; i++) {
                roots[2 * i] = Math.cos(i * ang);
                roots[2 * i + 1] = Math.sin(i * ang);
            }
        }

        int bits(int N) {
            int ret = 0;
            while (1 << ret < N) ret++;
            if (1 << ret != N) throw new RuntimeException();
            return ret;
        }

        void fftIterative(double[] array, boolean inv) {
            int bits = bits(array.length / 2);
            int N = 1 << bits;
            for (int from = 0; from < N; from++) {
                int to = Integer.reverse(from) >>> (32 - bits);
                if (from < to) {
                    double tmpR = array[2 * from];
                    double tmpI = array[2 * from + 1];
                    array[2 * from] = array[2 * to];
                    array[2 * from + 1] = array[2 * to + 1];
                    array[2 * to] = tmpR;
                    array[2 * to + 1] = tmpI;
                }
            }
            for (int n = 2; n <= N; n *= 2) {
                int delta = 2 * maxN / n;
                for (int from = 0; from < N; from += n) {
                    int rootIdx = inv ? 2 * maxN : 0;
                    double tmpR, tmpI;
                    for (int arrIdx = 2 * from; arrIdx < 2 * from + n; arrIdx += 2) {
                        tmpR = array[arrIdx + n] * roots[rootIdx] - array[arrIdx + n + 1] * roots[rootIdx + 1];
                        tmpI = array[arrIdx + n] * roots[rootIdx + 1] + array[arrIdx + n + 1] * roots[rootIdx];
                        array[arrIdx + n] = array[arrIdx] - tmpR;
                        array[arrIdx + n + 1] = array[arrIdx + 1] - tmpI;
                        array[arrIdx] += tmpR;
                        array[arrIdx + 1] += tmpI;
                        rootIdx += (inv ? -delta : delta);
                    }
                }
            }
            if (inv) {
                for (int i = 0; i < array.length; i++) {
                    array[i] /= N;
                }
            }
        }
    }
}


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

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while (t-- > 0) {
			int y = sc.nextInt();
			int x = sc.nextInt();
			long[] cost = new long[6];
			for (int i = 0; i < 6; i++) {
				cost[i] = sc.nextInt();
			}
			for (int i = 0; i < 6; i++) {
				cost[i] = Math.min(cost[i], cost[(i - 1 + 6) % 6] + cost[(i + 1) % 6]);
			}
			if (x == 0 && y == 0) {
				System.out.println(0);
				continue;
			}
			if (x == 0 || y == 0) {
				if (x == 0) {
					System.out.println((long) Math.abs(y) * (y > 0 ? cost[5] : cost[2]));
				} else {
					System.out.println((long) Math.abs(x) * (x > 0 ? cost[1] : cost[4]));
				}
				continue;
			}
			if (x > 0) {
				if (y > 0) {
					long min = Math.min(x, y);
					long answer = min * cost[0];
					x -= min;
					y -= min;
					System.out.println(answer + (long) x * cost[1] + (long) y * cost[5]);
				} else {
					System.out.println((long) x * cost[1] - (long) y * cost[2]);
				}
			} else {
				if (y > 0) {
					System.out.println(-(long) x * cost[4] + (long) y * cost[5]);
				} else {
					long max = Math.max(x, y);
					long answer = -max * cost[3];
					x -= max;
					y -= max;
					System.out.println(answer - (long) x * cost[4] - (long) y * cost[2]);
				}
			}
		}
	}

	private static void swap(long[] a, int i, int j) {
		long t = a[i];
		a[i] = a[j];
		a[j] = t;
	}
}


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

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while (t-- > 0) {
			int y = sc.nextInt();
			int x = sc.nextInt();
			long[] cost = new long[6];
			for (int i = 0; i < 6; i++) {
				cost[i] = sc.nextInt();
			}
			for (int i = 0; i < 6; i++) {
				cost[i] = Math.min(cost[i], cost[(i - 1 + 6) % 6] + cost[(i + 1) % 6]);
			}
			if (x == 0 && y == 0) {
				System.out.println(0);
				continue;
			}
			if (x == 0 || y == 0) {
				if (x == 0) {
					System.out.println((long) Math.abs(y) * y > 0 ? cost[5] : cost[2]);
				} else {
					System.out.println((long) Math.abs(x) * x > 0 ? cost[1] : cost[4]);
				}
				continue;
			}
			if (x > 0) {
				if (y > 0) {
					long min = Math.min(x, y);
					long answer = min * cost[0];
					x -= min;
					y -= min;
					System.out.println(answer + (long) x * cost[1] + (long) y * cost[5]);
				} else {
					System.out.println((long) x * cost[1] - (long) y * cost[2]);
				}
			} else {
				if (y > 0) {
					System.out.println(-(long) x * cost[4] + (long) y * cost[5]);
				} else {
					long max = Math.max(x, y);
					long answer = -max * cost[3];
					x -= max;
					y -= max;
					System.out.println(answer - (long) x * cost[4] - (long) y * cost[2]);
				}
			}
		}
	}

	private static void swap(long[] a, int i, int j) {
		long t = a[i];
		a[i] = a[j];
		a[j] = t;
	}
}


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

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while (t-- > 0) {
			int y = sc.nextInt();
			int x = sc.nextInt();
			long[] cost = new long[6];
			for (int i = 0; i < 6; i++) {
				cost[i] = sc.nextInt();
			}
			for (int i = 0; i < 6; i++) {
				cost[i] = Math.min(cost[i], cost[(i - 1 + 6) % 6] + cost[(i + 1) % 6]);
			}
			if (x == 0 && y == 0) {
				System.out.println(0);
				continue;
			}
			if (x == 0 || y == 0) {
				if (x == 0) {
					System.out.println((long) Math.abs(y) * y > 0 ? cost[5] : cost[2]);
				} else {
					System.out.println((long) Math.abs(x) * x > 0 ? cost[1] : cost[4]);
				}
				continue;
			}
			if (x > 0) {
				if (y > 0) {
					long min = Math.min(x, y);
					long answer = min * cost[0];
					x -= min;
					y -= min;
					System.out.println(answer + (long) x * cost[1] + (long) y * cost[5]);
				} else {
					System.out.println((long) x * cost[1] - (long) y * cost[2]);
				}
			} else {
				if (y > 0) {
					System.out.println(-(long) x * cost[4] + (long) y * cost[5]);
				} else {
					long max = Math.max(x, y);
					long answer = max * cost[3];
					x -= max;
					y -= max;
					System.out.println(answer - (long) x * cost[4] - (long) y * cost[2]);
				}
			}
		}
	}

	private static void swap(long[] a, int i, int j) {
		long t = a[i];
		a[i] = a[j];
		a[j] = t;
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author xwchen
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        int[] dx = {1, 0, -1, -1, 0, 1};
        int[] dy = {1, 1, 0, -1, -1, 0};
        long[] c;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int T = in.nextInt();
            while (T-- > 0) {
                long x = in.nextLong();
                long y = in.nextLong();
                c = in.nextLongArray(6);
                long best = 1L << 62;
                for (int k = 0; k < 6; ++k) {
                    for (int v = 0; v < 6; ++v) {
                        long cost = getCostByTwo(k, v, x, y);
                        //Utils.pf("%d %d  = %d", k, v, cost);
                        if (cost > -1) {
                            best = Math.min(best, cost);
                        }
                    }
                }
                out.println(best);
            }
        }

        long getCostByTwo(int u, int v, long x, long y) {
            long tx = 0;
            long ty = 0;
            long cost = 0;
            boolean reachable = false;
            //Utils.pf("%d %d   %d %d", u, v, x, y);
            if (sameFlag(x, dx[u]) && v != 0 && v != 3) {
                long steps = x / dx[u];
                tx = dx[u] * steps;
                ty = dy[u] * steps;
                cost += c[u] * steps;
                //Utils.pf("%d %d  cost = %d", tx, ty, cost);
                long diffY = y - ty;
                if (diffY == 0) {
                    reachable = true;
                } else if (sameFlag(diffY, dy[v])) {
                    reachable = true;
                    steps = diffY / dy[v];
                    cost += c[v] * steps;
                }
            } else if (sameFlag(y, dy[u]) && v != 0 && v != 3) {
                long steps = y / dy[u];
                tx = dx[u] * steps;
                ty = dy[u] * steps;
                cost += c[u] * steps;
                long diffX = x - tx;
                if (diffX == 0) {
                    reachable = true;
                } else if (sameFlag(diffX, dx[v])) {
                    reachable = true;
                    steps = diffX / dx[v];
                    cost += c[v] * steps;
                }
            }

            return reachable ? cost : -1;
        }

        boolean sameFlag(long a, long b) {
            return a * b > 0;
        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer = new StringTokenizer("");

        public InputReader(InputStream inputStream) {
            this.reader = new BufferedReader(
                    new InputStreamReader(inputStream));
        }

        public String next() {
            while (!tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokenizer.nextToken();
        }

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

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

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author xwchen
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        int[] dx = {1, 0, -1, -1, 0, 1};
        int[] dy = {1, 1, 0, -1, -1, 0};
        long[] c;
        long inf = 1L << 62;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int T = in.nextInt();
            while (T-- > 0) {
                long x = in.nextLong();
                long y = in.nextLong();
                c = in.nextLongArray(6);
                long best = 1L << 62;
                for (int k = 0; k < 6; ++k) {
                    for (int v = 0; v < 6; ++v) {
                        long cost = getCostByTwo(k, v, x, y);
                        //Utils.pf("%d %d  = %d", k, v, cost);
                        if (cost > -1) {
                            best = Math.min(best, cost);
                        }
                    }
                }
                out.println(best);
            }
        }

        long getCostByTwo(int u, int v, long x, long y) {
            long tx = 0;
            long ty = 0;
            long cost1 = 0;
            long cost2 = 0;
            boolean reachable1 = false;
            boolean reachable2 = false;
            //Utils.pf("%d %d   %d %d", u, v, x, y);
            if (sameFlag(x, dx[u]) && v != 0 && v != 3) {
                long steps = x / dx[u];
                tx = dx[u] * steps;
                ty = dy[u] * steps;
                cost1 += c[u] * steps;
                //Utils.pf("%d %d  cost = %d", tx, ty, cost);
                long diffY = y - ty;
                if (diffY == 0) {
                    reachable1 = true;
                } else if (sameFlag(diffY, dy[v])) {
                    reachable1 = true;
                    steps = diffY / dy[v];
                    cost1 += c[v] * steps;
                }
            }
            if (sameFlag(y, dy[u]) && v != 0 && v != 3) {
                long steps = y / dy[u];
                tx = dx[u] * steps;
                ty = dy[u] * steps;
                cost2 += c[u] * steps;
                long diffX = x - tx;
                if (diffX == 0) {
                    reachable2 = true;
                } else if (sameFlag(diffX, dx[v])) {
                    reachable2 = true;
                    steps = diffX / dx[v];
                    cost2 += c[v] * steps;
                }
            }
            long cost = inf;
            if (reachable1) {
                cost = Math.min(cost, cost1);
            }
            if (reachable2) {
                cost = Math.min(cost, cost2);
            }
            return cost;
        }

        boolean sameFlag(long a, long b) {
            return a * b > 0;
        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer = new StringTokenizer("");

        public InputReader(InputStream inputStream) {
            this.reader = new BufferedReader(
                    new InputStreamReader(inputStream));
        }

        public String next() {
            while (!tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokenizer.nextToken();
        }

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

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

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author xwchen
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        long[] dx = {1, 0, -1, -1, 0, 1};
        long[] dy = {1, 1, 0, -1, -1, 0};
        long[] c;
        long inf = 1L << 62;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int T = in.nextInt();
            while (T-- > 0) {
                long x = in.nextLong();
                long y = in.nextLong();
                c = in.nextLongArray(6);
                long best = 1L << 62;
                for (int k = 0; k < 6; ++k) {
                    for (int v = 0; v < 6; ++v) {
                        long cost = getCostByTwo(k, v, x, y);
                        //Utils.pf("%d %d  = %d", k, v, cost);
                        if (cost > -1) {
                            best = Math.min(best, cost);
                        }
                    }
                }
                if (x == 0 && y == 0) {
                    best = 0;
                }
                out.println(best);
            }
        }

        long getCostByTwo(int u, int v, long x, long y) {
            long tx = 0;
            long ty = 0;
            long cost1 = 0;
            long cost2 = 0;
            boolean reachable1 = false;
            boolean reachable2 = false;
            //Utils.pf("%d %d   %d %d", u, v, x, y);
            if (sameFlag(x, dx[u]) && v != 0 && v != 3) {
                long steps = x / dx[u];
                tx = dx[u] * steps;
                ty = dy[u] * steps;
                cost1 += c[u] * steps;
                //Utils.pf("%d %d  cost = %d", tx, ty, cost);
                long diffY = y - ty;
                if (diffY == 0) {
                    reachable1 = true;
                } else if (sameFlag(diffY, dy[v])) {
                    reachable1 = true;
                    steps = diffY / dy[v];
                    cost1 += c[v] * steps;
                }
            }
            if (sameFlag(y, dy[u]) && v != 0 && v != 3) {
                long steps = y / dy[u];
                tx = dx[u] * steps;
                ty = dy[u] * steps;
                cost2 += c[u] * steps;
                long diffX = x - tx;
                if (diffX == 0) {
                    reachable2 = true;
                } else if (sameFlag(diffX, dx[v])) {
                    reachable2 = true;
                    steps = diffX / dx[v];
                    cost2 += c[v] * steps;
                }
            }
            long cost = inf;
            if (reachable1) {
                cost = Math.min(cost, cost1);
            }
            if (reachable2) {
                cost = Math.min(cost, cost2);
            }
            return cost;
        }

        boolean sameFlag(long a, long b) {
            return a * b > 0;
        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer = new StringTokenizer("");

        public InputReader(InputStream inputStream) {
            this.reader = new BufferedReader(
                    new InputStreamReader(inputStream));
        }

        public String next() {
            while (!tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokenizer.nextToken();
        }

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

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

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }

    }
}


import java.util.*;
import java.io.*;
public class EdD {
	static long num = 998244353;
	public static void main(String[] args) throws Exception{

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		int t = Integer.parseInt(bf.readLine());
 		for(int i = 0;i<t;i++){
//	 		String input1 = bf.readLine().trim();
//	 		String input2 = bf.readLine().trim();
	 		StringTokenizer st = new StringTokenizer(bf.readLine());
	 		long x = Integer.parseInt(st.nextToken());
	 		long y = Integer.parseInt(st.nextToken());
	 		long[] array = new long[6];
	 		StringTokenizer st2 = new StringTokenizer(bf.readLine());
	 		for(int j=0;j<6;j++){
	 			array[j] = Long.parseLong(st2.nextToken());
	 		}
	 		array[0] = Math.min(array[0],  array[1]+array[5]);
	 		array[1] = Math.min(array[1],  array[0]+array[2]);
	 		array[2] = Math.min(array[2],  array[1]+array[3]);
	 		array[3] = Math.min(array[3],  array[2]+array[4]);
	 		array[4] = Math.min(array[4],  array[3]+array[5]);
	 		array[5] = Math.min(array[5],  array[4]+array[0]);
	 		array[0] = Math.min(array[0],  array[1]+array[5]);
	 		array[1] = Math.min(array[1],  array[0]+array[2]);
	 		array[2] = Math.min(array[2],  array[1]+array[3]);
	 		array[3] = Math.min(array[3],  array[2]+array[4]);
	 		array[4] = Math.min(array[4],  array[3]+array[5]);
	 		array[5] = Math.min(array[5],  array[4]+array[0]);
	 		array[0] = Math.min(array[0],  array[1]+array[5]);
	 		array[1] = Math.min(array[1],  array[0]+array[2]);
	 		array[2] = Math.min(array[2],  array[1]+array[3]);
	 		array[3] = Math.min(array[3],  array[2]+array[4]);
	 		array[4] = Math.min(array[4],  array[3]+array[5]);
	 		array[5] = Math.min(array[5],  array[4]+array[0]);
	 		array[0] = Math.min(array[0],  array[1]+array[5]);
	 		array[1] = Math.min(array[1],  array[0]+array[2]);
	 		array[2] = Math.min(array[2],  array[1]+array[3]);
	 		array[3] = Math.min(array[3],  array[2]+array[4]);
	 		array[4] = Math.min(array[4],  array[3]+array[5]);
	 		array[5] = Math.min(array[5],  array[4]+array[0]);
	 		array[0] = Math.min(array[0],  array[1]+array[5]);
	 		array[1] = Math.min(array[1],  array[0]+array[2]);
	 		array[2] = Math.min(array[2],  array[1]+array[3]);
	 		array[3] = Math.min(array[3],  array[2]+array[4]);
	 		array[4] = Math.min(array[4],  array[3]+array[5]);
	 		array[5] = Math.min(array[5],  array[4]+array[0]);
	 		array[0] = Math.min(array[0],  array[1]+array[5]);
	 		array[1] = Math.min(array[1],  array[0]+array[2]);
	 		array[2] = Math.min(array[2],  array[1]+array[3]);
	 		array[3] = Math.min(array[3],  array[2]+array[4]);
	 		array[4] = Math.min(array[4],  array[3]+array[5]);
	 		array[5] = Math.min(array[5],  array[4]+array[0]);
	 		array[0] = Math.min(array[0],  array[1]+array[5]);
	 		array[1] = Math.min(array[1],  array[0]+array[2]);
	 		array[2] = Math.min(array[2],  array[1]+array[3]);
	 		array[3] = Math.min(array[3],  array[2]+array[4]);
	 		array[4] = Math.min(array[4],  array[3]+array[5]);
	 		array[5] = Math.min(array[5],  array[4]+array[0]);

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

 		}

 		out.println();

 		out.close();

 	}
}

//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is