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

public class Main {
    static int MOD = 1000000007;

    // After writing solution, quick scan for:
    //   array out of bounds
    //   special cases e.g. n=1?
    //
    // Big numbers arithmetic bugs:
    //   int overflow
    //   sorting, or taking max, after MOD
    void solve() throws IOException {
        int T = ri();
        for (int Ti = 0; Ti < T; Ti++) {
            long[] xy = rll(2);
            long x = xy[0];
            long y = xy[1];
            long[] cost = rll(6);
            long[][] dirs = new long[][]{
                {1, 1}, {0, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, 0}
            };

            if (x == 0 && y == 0) {
                pw.println("0");
                continue;
            }

            // ATTEMPT 1: conjecture that at most 2 directions ever needed
            // INCORRECT?
            long ans = Long.MAX_VALUE;
            // 1 direction
            for (int i = 0; i < 6; i++) {
                long a = dirs[i][0];
                long c = dirs[i][1];
                if (x != 0) {
                    if (a == 0) continue;
                    long times = x / a;
                    if (c * times != y) continue;
                    if (times < 0) continue;
                    ans = Math.min(ans, times * cost[i]);
                } else {
                    if (c == 0) continue;
                    long times = y / c;
                    if (a * times != x) continue;
                    if (times < 0) continue;
                    ans = Math.min(ans, times * cost[i]);
                }
            }

            for (int i = 0; i < 6; i++) {
                long a = dirs[i][0];
                long c = dirs[i][1];
                for (int j = 0; j < 6; j++) {
                    if (i == j) continue;
                    long b = dirs[j][0];
                    long d = dirs[j][1];
                    long det = a*d - b*c;
                    if (det == 0) continue;

                    long A = x * d - y * b;
                    long B = -x * c + a * y;
                    if (A % det != 0 || B % det != 0) continue;
                    A /= det;
                    B /= det;
                    if (A < 0 || B < 0) continue;
                    ans = Math.min(ans, A * cost[i] + B * cost[j]);
                }
            }
            pw.println(ans);
        }
    }

    // Template code below

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter pw = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        Main m = new Main();
        m.solve();
        m.close();
    }

    void close() throws IOException {
        pw.flush();
        pw.close();
        br.close();
    }

    int ri() throws IOException {
        return Integer.parseInt(br.readLine().trim());
    }

    long rl() throws IOException {
        return Long.parseLong(br.readLine().trim());
    }

    int[] ril(int n) throws IOException {
        int[] nums = new int[n];
        int c = 0;
        for (int i = 0; i < n; i++) {
            int sign = 1;
            c = br.read();
            int x = 0;
            if (c == '-') {
                sign = -1;
                c = br.read();
            }
            while (c >= '0' && c <= '9') {
                x = x * 10 + c - '0';
                c = br.read();
            }
            nums[i] = x * sign;
        }
        while (c != '\n' && c != -1) c = br.read();
        return nums;
    }

    long[] rll(int n) throws IOException {
        long[] nums = new long[n];
        int c = 0;
        for (int i = 0; i < n; i++) {
            int sign = 1;
            c = br.read();
            long x = 0;
            if (c == '-') {
                sign = -1;
                c = br.read();
            }
            while (c >= '0' && c <= '9') {
                x = x * 10 + c - '0';
                c = br.read();
            }
            nums[i] = x * sign;
        }
        while (c != '\n' && c != -1) c = br.read();
        return nums;
    }

    int[] rkil() throws IOException {
        int sign = 1;
        int c = br.read();
        int x = 0;
        if (c == '-') {
            sign = -1;
            c = br.read();
        }
        while (c >= '0' && c <= '9') {
            x = x * 10 + c - '0';
            c = br.read();
        }
        return ril(x);
    }

    long[] rkll() throws IOException {
        int sign = 1;
        int c = br.read();
        int x = 0;
        if (c == '-') {
            sign = -1;
            c = br.read();
        }
        while (c >= '0' && c <= '9') {
            x = x * 10 + c - '0';
            c = br.read();
        }
        return rll(x);
    }

    char[] rs() throws IOException {
        return br.readLine().toCharArray();
    }

    void sort(int[] A) {
        Random r = new Random();
        for (int i = A.length-1; i > 0; i--) {
            int j = r.nextInt(i+1);
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        }
        Arrays.sort(A);
    }
}


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

public class Hexagons {

	static int cost[];
	static long x, y;
	static int d[][] = {{1,1},{0,1},{-1,0},{-1,-1},{0,-1},{1,0}};

	public static void main(String[] args) {
        FastScanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
    	while(t-->0) {
    		x = in.nextInt(); y = in.nextInt();
    		cost = in.readArray(6);
    		long a1,a2,a3;
    		a1=a2=a3=0;

    		if(x<0) a1 = Math.abs(x*cost[2]);
    		else a1 = Math.abs(x*cost[5]);
    		if(y<0) a1 += Math.abs(y*cost[4]);
    		else a1 += Math.abs(y*cost[1]);

    		if(x<0) a2 = Math.abs(x*cost[3]);
            else a2 = Math.abs(x*cost[0]);
            long k = y-x;
            if(k<0){
                a2 += Math.abs(k*cost[4]);
            }
            else{
                a2 += Math.abs(k*cost[1]);
            }

    		if(y<0) a3 = Math.abs(y*cost[3]);
            else a3 = Math.abs(y*cost[0]);
            x = x-y;
            if(x<0){
                a3 += Math.abs(x*cost[2]);
            }
            else{
                a3 += Math.abs(x*cost[5]);
            }

    		out.println(Math.min(a1,Math.min(a2,a3)));
    	}
        out.flush();
	}

	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");

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

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

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

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

		int[] readArray(int n) {
			int a[] = new int[n];
			for(int i=0;i<n;i++) a[i] = nextInt();
			return a;
		}
	}

	static final Random random = new Random();

	static void ruffleSort(int[] a){
		int n = a.length;
		for(int i=0;i<n;i++){
			int j = random.nextInt(n), temp = a[j];
			a[j] = a[i]; a[i] = temp;
		}
		Arrays.sort(a);
	}
}


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

public class D {

	static int cost[];
	static long x, y;
	static int d[][] = {{1,1},{0,1},{-1,0},{-1,-1},{0,-1},{1,0}};

	public static void main(String[] args) {
        FastScanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
    	while(t-->0) {
    		x = in.nextInt(); y = in.nextInt();
    		cost = in.readArray(6);
    		long a1,a2,a3,a4,a5;
    		a1=a2=a3=a4=a5=Long.MAX_VALUE;
    		if(x<0){
    			a5 = Math.abs(x*cost[2]);
    		}
    		else{
    			a5 = Math.abs(x*cost[5]);
    		}
    		if(y<0){
    			a5 += Math.abs(y*cost[4]);
    		}
    		else{
    			a5 += Math.abs(y*cost[1]);
    		}
    		if(x<0){
    			a1 = Math.abs(x*cost[3]);
    			if(y<0) y = y-x;
    			else y = y+x;
    			if(y<0){
    				a1 += Math.abs(y*cost[4]);
    			}
    			else{
    				a1 += Math.abs(y*cost[1]);
    			}
    		}
    		else{
    			a2 = Math.abs(x*cost[0]);
    			if(y<0) y = y+x;
    			else y = y-x;
    			if(y<0){
    				a2 += Math.abs(y*cost[4]);
    			}
    			else{
    				a2 += Math.abs(y*cost[1]);
    			}
    		}
    		if(y<0){
    			a3 = Math.abs(y*cost[3]);
    			if(x<0) x = x-y;
    			else x = x+y;
    			if(x<0){
    				a3 += Math.abs(x*cost[2]);
    			}
    			else{
    				a3 += Math.abs(x*cost[5]);
    			}
    		}
    		else{
    			a4 = Math.abs(y*cost[0]);
    			if(x<0) x = x+y;
    			else x = x-y;
    			if(x<0){
    				a4 += Math.abs(x*cost[2]);
    			}
    			else{
    				a4 += Math.abs(x*cost[5]);
    			}
    		}
    		out.println(Math.min(a5,Math.min(Math.min(a1,a2),Math.min(a3,a4))));
    	}
        out.flush();
	}

	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");

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

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

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

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

		int[] readArray(int n) {
			int a[] = new int[n];
			for(int i=0;i<n;i++) a[i] = nextInt();
			return a;
		}
	}

	static final Random random = new Random();

	static void ruffleSort(int[] a){
		int n = a.length;
		for(int i=0;i<n;i++){
			int j = random.nextInt(n), temp = a[j];
			a[j] = a[i]; a[i] = temp;
		}
		Arrays.sort(a);
	}
}


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

public class D {

	static int cost[];
	static long x, y;
	static int d[][] = {{1,1},{0,1},{-1,0},{-1,-1},{0,-1},{1,0}};

	public static void main(String[] args) {
        FastScanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
    	while(t-->0) {
    		x = in.nextInt(); y = in.nextInt();
    		cost = in.readArray(6);
    		long a1,a2,a3,a4,a5;
    		a1=a2=a3=a4=a5=Long.MAX_VALUE;
    		if(x<0){
    			a5 = Math.abs(x*cost[2]);
    		}
    		else{
    			a5 = Math.abs(x*cost[5]);
    		}
    		if(y<0){
    			a5 += Math.abs(y*cost[4]);
    		}
    		else{
    			a5 += Math.abs(y*cost[1]);
    		}
    		if(x<0){
    			a1 = Math.abs(x*cost[3]);
    			y = y + x;
    			if(y<0){
    				a1 += Math.abs(y*cost[4]);
    			}
    			else{
    				a1 += Math.abs(y*cost[1]);
    			}
    		}
    		else{
    			a2 = Math.abs(x*cost[0]);
    			y = y + x;
    			if(y<0){
    				a2 += Math.abs(y*cost[4]);
    			}
    			else{
    				a2 += Math.abs(y*cost[1]);
    			}
    		}
    		if(y<0){
    			a3 = Math.abs(y*cost[3]);
    			x = y + x;
    			if(x<0){
    				a3 += Math.abs(x*cost[2]);
    			}
    			else{
    				a3 += Math.abs(x*cost[5]);
    			}
    		}
    		else{
    			a4 = Math.abs(y*cost[0]);
    			x = y + x;
    			if(x<0){
    				a4 += Math.abs(x*cost[2]);
    			}
    			else{
    				a4 += Math.abs(x*cost[5]);
    			}
    		}
    		out.println(Math.min(a5,Math.min(Math.min(a1,a2),Math.min(a3,a4))));
    	}
        out.flush();
	}

	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");

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

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

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

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

		int[] readArray(int n) {
			int a[] = new int[n];
			for(int i=0;i<n;i++) a[i] = nextInt();
			return a;
		}
	}

	static final Random random = new Random();

	static void ruffleSort(int[] a){
		int n = a.length;
		for(int i=0;i<n;i++){
			int j = random.nextInt(n), temp = a[j];
			a[j] = a[i]; a[i] = temp;
		}
		Arrays.sort(a);
	}
}


import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
public class Prac{
    static class InputReader {
        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        public InputReader(InputStream st) {
            this.stream = st;
        }
        public int read() {
            if (snumChars == -1)
                throw new InputMismatchException();
            if (curChar >= snumChars) {
                curChar = 0;
                try {
                    snumChars = stream.read(buf);
                }
                catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (snumChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }
        public int ni() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
        public long nl() {
            int c = read();
            while (isSpaceChar(c)) {
            c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
        public int[] nia(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }
        public int[] nia1(int n) {
            int a[] = new int[n+1];
            for (int i = 1; i <=n; i++) {
                a[i] = ni();
            }
            return a;
        }
        public long[] nla(int n) {
            long a[] = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = nl();
            }
            return a;
        }
        public long[] nla1(int n) {
            long a[] = new long[n+1];
            for (int i = 1; i <= n; i++) {
                a[i] = nl();
            }
            return a;
        }
        public Long[] nLa(int n) {
            Long a[] = new Long[n];
            for (int i = 0; i < n; i++) {
                a[i] = nl();
            }
            return a;
        }
        public Long[] nLa1(int n) {
            Long a[] = new Long[n+1];
            for (int i = 1; i <= n; i++) {
                a[i] = nl();
            }
            return a;
        }
        public Integer[] nIa(int n) {
            Integer a[] = new Integer[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }
        public Integer[] nIa1(int n) {
            Integer a[] = new Integer[n+1];
            for (int i = 1; i <= n; i++) {
                a[i] = ni();
            }
            return a;
        }
        public String rs() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                    c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }
        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }
        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }
    }
    public static class Key {
        private final int x, y;
        public Key(int x, int y) {
            this.x = x; this.y = y;
        }
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Key)) return false;
            Key key = (Key) o;
            return x == key.x && y == key.y;
        }
        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }
    static class Pair{
            int x,y;
            Pair(int a,int b){
                    x=a;y=b;
            }
//            @Override
//            public int compareTo(Pair p) {
//                    if(x==p.x) return y-p.y;
//                    return x-p.x;
//            }
    }
    static void shuffleArray(int temp[]){
        int n = temp.length;
        Random rnd = new Random();
        for(int i=0; i<n; ++i){
            int tmp = temp[i];
            int randomPos = i + rnd.nextInt(n-i);
            temp[i] = temp[randomPos];
            temp[randomPos] = tmp;
        }
    }
    static long gcd(long a,long b){ return b==0?a:gcd(b,a%b);}
    static long lcm(long a,long b){return (a/gcd(a,b))*b;}
    static PrintWriter ww = new PrintWriter(System.out);
    static long mod=998244353L,mod1=1000000007;
    //static int r[]={0,1,0,-1}, c[]={1,0,-1,0};
    static long modInverse(long a, long m){
        long num=m;
        long x=1;
        long power=a%mod;
        while(num>0){
              if(num%2==1){
                  x=(x*power)%mod;
            }
            num>>=1;
            power=(power*power)%mod;
        }
        return x;
    }
    static long fact[]=new long[1000005];
    public static void findFact(){
        fact[0]=1;
        for(int i=1;i<=1000000;i++){
            fact[i]=(i*fact[i-1])%mod;
        }
    }
    static long ncrWithMod(int n,int r){
        if(n<r)return 0L;

        return (fact[n]*modInverse((fact[r]*fact[n-r])%mod,mod-2))%mod;
    }

    static long a[][] = {{1 , 1},{0 , 1},{-1 , 0},{-1 , - 1},{0 , -1},{1 , 0}};
    public static void main(String [] args){
        InputReader sc=new InputReader(System.in);
        int tt = sc.ni();
        while(tt -- > 0){
            long x = sc.ni() , y = sc.ni();
            long cost[] = sc.nla(6);
            long ans = Long.MAX_VALUE;
            for(int i = 0 ; i < 6 ; i ++){
                for(int j = i + 1 ; j < 6 ; j ++){
                    if((j - i) == 3)continue;
                    long den = a[i][0]*a[j][1] - a[j][0]*a[i][1];
                    if(den == 0)continue;
                    long q_num = y*a[i][0] - x*a[i][1];
                    long p_num= x*a[j][1] - y*a[j][0];
                    long q = q_num/den;
                    long p = p_num/den;
                    if(q < 0 || p < 0)continue;
                    ans = Math.min(ans , p*cost[i] + q*cost[j]);
                }
            }
            ww.println(ans);
        }
        ww  .close();
    }
}


import java.util.*;

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

public class Solution {

	public static class Reader {

		private InputStream stream;
		private byte[] buf = new byte[4096];
		private int curChar, snumChars;

		public Reader(InputStream stream) {
			this.stream = stream;
		}

		public int read() {
			if (snumChars == -1) {
				throw new InputMismatchException();
			}
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException E) {
					throw new InputMismatchException();
				}
			}
			if (snumChars <= 0) {
				return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int number = 0;
			do {
				number *= 10;
				number += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return number * sgn;
		}

		public long nextLong() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			long sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long number = 0;
			do {
				number *= 10L;
				number += (long) (c - '0');
				c = read();
			} while (!isSpaceChar(c));
			return number * sgn;
		}

		public int[] nextIntArray(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = this.nextInt();
			}
			return arr;
		}

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

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

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

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

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

	}

	public static Reader sc;
	public static PrintWriter out;
	public static void main(String[] args) {
		new Thread(null, null, "Thread", 1 << 29) {
			public void run() {
				try {
					out = new PrintWriter(new BufferedOutputStream(System.out));
					sc = new Reader(System.in);
					new Solution().solve();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
					System.exit(1);
				}
			}
		}.start();
	}

		long x,y;
		long[] c;
		long[][] dir = { {1 , 1} , {0 , 1} , {-1 , 0 } , {-1, -1} , {0 , -1} , {1, 0} };

	    public void solve(){

	    	int t = sc.nextInt();
	    	while(t-->0)
	    	{
	    		x = sc.nextInt();
	    		y = sc.nextInt();
	    	    c = sc.nextLongArray(6);

	    		long ans = (long) 1e18;
	    		for(int i = 0 ; i < 6; ++i ) {
	    			for(int j = i + 1; j < 6; ++j ) {
	    				long cost = f(i , j );
	    				if(cost >= 0)
	    				ans = Math.min(ans , cost);
	    			}
	    		}
	    		if(ans < 0) while(true);
	    		out.println(ans);

	    	}

	    }

	    long f(long a , long b) {
	    	if(x == 0 && y == 0)
	    		return 0;

	    	if(Math.abs(a - b) == 3) {
	    		long c1 = go(a);
	    		long c2 = go(b);
	    		if(c1 < 0 && c2 < 0) return -1;
	    		if(c1 < 0) return c[(int) b] * c2;
	    		if(c2 < 0 ) return c[(int) a] * c1;
	    		return Math.min(c[(int) a]  * c1 , c[(int) b] * c2);
	    	} else {

	    		long a1 = dir[(int) a][0] , b1 = dir[(int) b][0] , c1 = -x;
	    		long a2 = dir[(int) a][1] , b2 = dir[(int) b][1] , c2 = -y;

	    		if((a1*b2 - a2*b1)==0 || (b1*c2 - b2*c1) % (a1*b2 - a2*b1)  !=0 )
	    			return -1;
	    		if( (a1*b2 - a2*b1)==0 || (c1*a2 - c2*a1) % (a1*b2 - a2*b1) != 0)
	    			return -1;

	    		long u = (b1*c2 - b2*c1) / (a1*b2 - a2*b1);

	    		long v = (c1*a2 - c2*a1) / (a1*b2 - a2*b1);
	    		if( u < 0 || v < 0 ) return -1;
	    		return c[(int)a] * u + c[(int)b] * v;
	    	}
	    }

	    long go(long a) {
	    	if(dir[(int)a][0] == 0) {
	    		if(x != 0 ) return -1;

	    		if( y * dir[(int) a][1] >= 0 ) return y/dir[(int) a][1];
	    		else return -1;
	    	} else {
	    		if( dir[(int) a][1] == 0 ) {
	    			if(y != 0) return -1;

	    			if( x * dir[(int) a][0] >=0 ) return x/dir[(int) a][0];
	    			else return -1;
	    		}  else {
	    			long q1 = x / dir[(int) a][0];
	    			long q2 = y / dir[(int) a][1];

	    			if(q1 != q2) return -1;
	    			if(q1 < 0 ) return -1;
	    			return q1;
	    		}
	    	}
	    }

}


import java.util.*;

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

public class Solution {

	public static class Reader {

		private InputStream stream;
		private byte[] buf = new byte[4096];
		private int curChar, snumChars;

		public Reader(InputStream stream) {
			this.stream = stream;
		}

		public int read() {
			if (snumChars == -1) {
				throw new InputMismatchException();
			}
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException E) {
					throw new InputMismatchException();
				}
			}
			if (snumChars <= 0) {
				return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int number = 0;
			do {
				number *= 10;
				number += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return number * sgn;
		}

		public long nextLong() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			long sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long number = 0;
			do {
				number *= 10L;
				number += (long) (c - '0');
				c = read();
			} while (!isSpaceChar(c));
			return number * sgn;
		}

		public int[] nextIntArray(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = this.nextInt();
			}
			return arr;
		}

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

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

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

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

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

	}

	public static Reader sc;
	public static PrintWriter out;
	public static void main(String[] args) {
		new Thread(null, null, "Thread", 1 << 29) {
			public void run() {
				try {
					out = new PrintWriter(new BufferedOutputStream(System.out));
					sc = new Reader(System.in);
					new Solution().solve();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
					System.exit(1);
				}
			}
		}.start();
	}

		int x,y;
		int[] c;
		int[][] dir = { {1 , 1} , {0 , 1} , {-1 , 0 } , {-1, -1} , {0 , -1} , {1, 0} };

	    public void solve(){

	    	int t = sc.nextInt();
	    	while(t-->0)
	    	{
	    		x = sc.nextInt();
	    		y = sc.nextInt();
	    	    c = sc.nextIntArray(6);

	    		long ans = (long) 1e18;
	    		for(int i = 0 ; i < 6; ++i ) {
	    			for(int j = i + 1; j < 6; ++j ) {
	    				long cost = f(i , j );
	    				if(cost >= 0)
	    				ans = Math.min(ans , cost);
	    			}
	    		}
	    		out.println(ans);

	    	}

	    }

	    long f(int a , int b) {
	    	if(x == 0 && y == 0)
	    		return 0;

	    	if(Math.abs(a - b) == 3) {
	    		long c1 = go(a);
	    		long c2 = go(b);
	    		if(c1 < 0 && c2 < 0) return -1;
	    		if(c1 < 0) return c[b] * c2;
	    		if(c2 < 0 ) return c[a] * c1;
	    		return Math.min(c[a]  * c1 , c[b] * c2);
	    	} else {

	    		long a1 = dir[a][0] , b1 = dir[b][0] , c1 = -x;
	    		long a2 = dir[a][1] , b2 = dir[b][1] , c2 = -y;

	    		if((a1*b2 - a2*b1)==0 || (b1*c2 - b2*c1) % (a1*b2 - a2*b1)  !=0 )
	    			return -1;
	    		if( (a1*b2 - a2*b1)==0 || (c1*a2 - c2*a1) % (a1*b2 - a2*b1) != 0)
	    			return -1;

	    		long u = (b1*c2 - b2*c1) / (a1*b2 - a2*b1);

	    		long v = (c1*a2 - c2*a1) / (a1*b2 - a2*b1);
	    		if( u < 0 || v < 0 ) return -1;
	    		return c[a] * u + c[b] * v;
	    	}
	    }

	    long go(int a) {
	    	if(dir[a][0] == 0) {
	    		if(x != 0 ) return -1;

	    		if( y * dir[a][1] >= 0 ) return y/dir[a][1];
	    		else return -1;
	    	} else {
	    		if( dir[a][1] == 0 ) {
	    			if(y != 0) return -1;

	    			if( x * dir[a][0] >=0 ) return x/dir[a][0];
	    			else return -1;
	    		}  else {
	    			int q1 = x / dir[a][0];
	    			int q2 = y / dir[a][1];

	    			if(q1 != q2) return -1;
	    			if(q1 < 0 ) return -1;
	    			return q1;
	    		}
	    	}
	    }

}


import java.util.*;

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

public class Solution {

	public static class Reader {

		private InputStream stream;
		private byte[] buf = new byte[4096];
		private int curChar, snumChars;

		public Reader(InputStream stream) {
			this.stream = stream;
		}

		public int read() {
			if (snumChars == -1) {
				throw new InputMismatchException();
			}
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException E) {
					throw new InputMismatchException();
				}
			}
			if (snumChars <= 0) {
				return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int number = 0;
			do {
				number *= 10;
				number += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return number * sgn;
		}

		public long nextLong() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			long sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long number = 0;
			do {
				number *= 10L;
				number += (long) (c - '0');
				c = read();
			} while (!isSpaceChar(c));
			return number * sgn;
		}

		public int[] nextIntArray(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = this.nextInt();
			}
			return arr;
		}

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

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

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

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

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

	}

	public static Reader sc;
	public static PrintWriter out;
	public static void main(String[] args) {
		new Thread(null, null, "Thread", 1 << 29) {
			public void run() {
				try {
					out = new PrintWriter(new BufferedOutputStream(System.out));
					sc = new Reader(System.in);
					new Solution().solve();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
					System.exit(1);
				}
			}
		}.start();
	}

		int x,y;
		int[] c;
		int[][] dir = { {1 , 1} , {0 , 1} , {-1 , 0 } , {-1, -1} , {0 , -1} , {1, 0} };

	    public void solve(){

	    	int t = sc.nextInt();
	    	while(t-->0)
	    	{
	    		x = sc.nextInt();
	    		y = sc.nextInt();
	    	    c = sc.nextIntArray(6);

	    		long ans = (long) 1e18;
	    		for(int i = 0 ; i < 6; ++i ) {
	    			for(int j = i + 1; j < 6; ++j ) {
	    				long cost = f(i , j );
	    				if(cost >= 0)
	    				ans = Math.min(ans , cost);
	    			}
	    		}
	    		if(ans < 0) while(true);
	    		out.println(ans);

	    	}

	    }

	    long f(int a , int b) {
	    	if(x == 0 && y == 0)
	    		return 0;

	    	if(Math.abs(a - b) == 3) {
	    		long c1 = go(a);
	    		long c2 = go(b);
	    		if(c1 < 0 && c2 < 0) return -1;
	    		if(c1 < 0) return c[b] * c2;
	    		if(c2 < 0 ) return c[a] * c1;
	    		return Math.min(c[a]  * c1 , c[b] * c2);
	    	} else {

	    		long a1 = dir[a][0] , b1 = dir[b][0] , c1 = -x;
	    		long a2 = dir[a][1] , b2 = dir[b][1] , c2 = -y;

	    		if((a1*b2 - a2*b1)==0 || (b1*c2 - b2*c1) % (a1*b2 - a2*b1)  !=0 )
	    			return -1;
	    		if( (a1*b2 - a2*b1)==0 || (c1*a2 - c2*a1) % (a1*b2 - a2*b1) != 0)
	    			return -1;

	    		long u = (b1*c2 - b2*c1) / (a1*b2 - a2*b1);

	    		long v = (c1*a2 - c2*a1) / (a1*b2 - a2*b1);
	    		if( u < 0 || v < 0 ) return -1;
	    		return c[a] * u + c[b] * v;
	    	}
	    }

	    long go(int a) {
	    	if(dir[a][0] == 0) {
	    		if(x != 0 ) return -1;

	    		if( y * dir[a][1] >= 0 ) return y/dir[a][1];
	    		else return -1;
	    	} else {
	    		if( dir[a][1] == 0 ) {
	    			if(y != 0) return -1;

	    			if( x * dir[a][0] >=0 ) return x/dir[a][0];
	    			else return -1;
	    		}  else {
	    			int q1 = x / dir[a][0];
	    			int q2 = y / dir[a][1];

	    			if(q1 != q2) return -1;
	    			if(q1 < 0 ) return -1;
	    			return q1;
	    		}
	    	}
	    }

}


import java.util.*;

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

public class Solution {

	public static class Reader {

		private InputStream stream;
		private byte[] buf = new byte[4096];
		private int curChar, snumChars;

		public Reader(InputStream stream) {
			this.stream = stream;
		}

		public int read() {
			if (snumChars == -1) {
				throw new InputMismatchException();
			}
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException E) {
					throw new InputMismatchException();
				}
			}
			if (snumChars <= 0) {
				return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int number = 0;
			do {
				number *= 10;
				number += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return number * sgn;
		}

		public long nextLong() {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			long sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long number = 0;
			do {
				number *= 10L;
				number += (long) (c - '0');
				c = read();
			} while (!isSpaceChar(c));
			return number * sgn;
		}

		public int[] nextIntArray(int n) {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = this.nextInt();
			}
			return arr;
		}

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

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

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

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

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

	}

	public static Reader sc;
	public static PrintWriter out;
	public static void main(String[] args) {
		new Thread(null, null, "Thread", 1 << 29) {
			public void run() {
				try {
					out = new PrintWriter(new BufferedOutputStream(System.out));
					sc = new Reader(System.in);
					new Solution().solve();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
					System.exit(1);
				}
			}
		}.start();
	}

		long x,y;
		long[] c;
		long[][] dir = { {1 , 1} , {0 , 1} , {-1 , 0 } , {-1, -1} , {0 , -1} , {1, 0} };

	    public void solve(){

	    	int t = sc.nextInt();
	    	while(t-->0)
	    	{
	    		x = sc.nextInt();
	    		y = sc.nextInt();
	    	    c = sc.nextLongArray(6);

	    		long ans = Long.MAX_VALUE;
	    		for(int i = 0 ; i < 6; ++i ) {
	    			for(int j = i + 1; j < 6; ++j ) {
	    				long cost = f(i , j );
	    				if(cost >= 0)
	    				ans = Math.min(ans , cost);
	    			}
	    		}
	    		if(ans < 0) while(true);
	    		out.println(ans);

	    	}

	    }

	    long f(long a , long b) {
	    	if(x == 0 && y == 0)
	    		return 0;

	    	if(Math.abs(a - b) == 3) {
	    		long c1 = go(a);
	    		long c2 = go(b);
	    		if(c1 < 0 && c2 < 0) return -1;
	    		if(c1 < 0) return c[(int) b] * c2;
	    		if(c2 < 0 ) return c[(int) a] * c1;
	    		return Math.min(c[(int) a]  * c1 , c[(int) b] * c2);
	    	} else {

	    		long a1 = dir[(int) a][0] , b1 = dir[(int) b][0] , c1 = -x;
	    		long a2 = dir[(int) a][1] , b2 = dir[(int) b][1] , c2 = -y;

	    		if((a1*b2 - a2*b1)==0 || (b1*c2 - b2*c1) % (a1*b2 - a2*b1)  !=0 )
	    			return -1;
	    		if( (a1*b2 - a2*b1)==0 || (c1*a2 - c2*a1) % (a1*b2 - a2*b1) != 0)
	    			return -1;

	    		long u = (b1*c2 - b2*c1) / (a1*b2 - a2*b1);

	    		long v = (c1*a2 - c2*a1) / (a1*b2 - a2*b1);
	    		if( u < 0 || v < 0 ) return -1;
	    		return c[(int)a] * u + c[(int)b] * v;
	    	}
	    }

	    long go(long a) {
	    	if(dir[(int)a][0] == 0) {
	    		if(x != 0 ) return -1;

	    		if( y * dir[(int) a][1] >= 0 ) return y/dir[(int) a][1];
	    		else return -1;
	    	} else {
	    		if( dir[(int) a][1] == 0 ) {
	    			if(y != 0) return -1;

	    			if( x * dir[(int) a][0] >=0 ) return x/dir[(int) a][0];
	    			else return -1;
	    		}  else {
	    			long q1 = x / dir[(int) a][0];
	    			long q2 = y / dir[(int) a][1];

	    			if(q1 != q2) return -1;
	    			if(q1 < 0 ) return -1;
	    			return q1;
	    		}
	    	}
	    }

}


import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class D {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

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

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

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

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

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

    static FastReader s = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);

    private static int[] rai(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }
        return arr;
    }

    private static int[][] rai(int n, int m) {
        int[][] arr = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = s.nextInt();
            }
        }
        return arr;
    }

    private static long[] ral(int n) {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextLong();
        }
        return arr;
    }

    private static long[][] ral(int n, int m) {
        long[][] arr = new long[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = s.nextLong();
            }
        }
        return arr;
    }

    private static int ri() {
        return s.nextInt();
    }

    private static long rl() {
        return s.nextLong();
    }

    private static String rs() {
        return s.next();
    }

//    static int MOD=1000000007;
    public static void main(String[] args) {
        StringBuilder ans = new StringBuilder();
        int t = ri();
//        int t=1;
        while(t-- >0) {
            int x=ri();
            int y=ri();
            List<Change> list=new ArrayList<>();
            long[] arr=ral(6);
//            list.add(new Change(0,0));
            list.add(new Change(1,1));//0
            list.add(new Change(0,1));//1
            list.add(new Change(-1,0));//2
            list.add(new Change(-1,-1));//3
            list.add(new Change(0,-1));//4
            list.add(new Change(1,0));//5

            if(x>0)
            {
                if(y>0)
                {
                    x=Math.abs(x);
                    y=Math.abs(y);

                    int min=Math.min(x,y);
                    long cost1=(min*Math.min(arr[0],arr[1]+arr[5]))+((y-min)*Math.min(arr[1],arr[0]+arr[2]))+ ((x-min)*Math.min(arr[5],arr[0]+arr[4]));

                    ans.append(cost1).append("\n");
                }
                else if(y<0)
                {
                    x=Math.abs(x);
                    y=Math.abs(y);

                    //x*(5,0+4) + y*(4,3+5);
                    long cost=x*Math.min(arr[5],arr[0]+arr[4])+ y*Math.min(arr[4],arr[3]+arr[5]);
                    ans.append(cost).append("\n");
                }
                else
                {
                    x=Math.abs(x);
                    long cost=x*Math.min(arr[5],arr[0]+arr[4]);
                    ans.append(cost).append("\n");
                }
            }
            else if(x<0)
            {
                if(y>0)
                {
                    x=Math.abs(x);
                    y=Math.abs(y);

                    //x*(2,1+3) + y*(1,0+2)
                    long cost=x*Math.min(arr[2],arr[1]+arr[3])+ y*Math.min(arr[1],arr[0]+arr[2]);
                    ans.append(cost).append("\n");

//                    int min=Math.min(x,y);
//                    long cost1=(min*Math.min(arr[0],arr[1]+arr[5]))+((y-min)*Math.min(arr[1],arr[0]+arr[2]))+ ((x-min)*Math.min(arr[5],arr[0]+arr[4]));
//
//                    ans.append(cost1).append("\n");
                }
                else if(y<0)
                {
                    x=Math.abs(x);
                    y=Math.abs(y);

                    int min=Math.min(x,y);
                    long cost1=(min*Math.min(arr[3],arr[2]+arr[4]))+((y-min)*Math.min(arr[4],arr[3]+arr[5]))+ ((x-min)*Math.min(arr[2],arr[1]+arr[3]));

                    ans.append(cost1).append("\n");
                }
                else
                {
                    x=Math.abs(x);
                    long cost=x*Math.min(arr[2],arr[1]+arr[3]);
                    ans.append(cost).append("\n");
                }
            }
            else
            {
                if(y>0)
                {
                    y=Math.abs(y);

                    long cost=y*Math.min(arr[1],arr[0]+arr[2]);
                    ans.append(cost).append("\n");

                }
                else if(y<0)
                {
                    y=Math.abs(y);

                    long cost1=((y)*Math.min(arr[4],arr[3]+arr[5]));

                    ans.append(cost1).append("\n");
                }
                else
                {
                    ans.append("0\n");
                }
            }

        }
        out.print(ans.toString());
        out.flush();

    }

    static class Change
    {
        int x,y;

        public Change(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;
import java.util.StringTokenizer;
import static java.lang.Math.*;

public class Main {

	public static void main(String[] args) {
		FastScanner sc =new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		int T=sc.nextInt();
		while(T-- > 0) {
			long x = sc.nextInt();
			long y = sc.nextInt();
			long ne = sc.nextInt();
			long e = sc.nextInt();
			long se = sc.nextInt();
			long sw = sc.nextInt();
			long w = sc.nextInt();
			long nw = sc.nextInt();
			ne = Math.min(ne, nw+e);
			e = Math.min(e, se+ne);
			se = Math.min(se,  e+sw);
			sw = Math.min(sw, se+w);
			w = Math.min(w,  sw + nw);
			nw = Math.min(nw,  w+ne);
			long ans = Long.MAX_VALUE;
			if(x <= 0) {
				ans = Math.min(ans,  (-x*se + (y >= 0 ? y*e : -y*w)));
				ans = Math.min(ans, (-x*sw + (y >= x ? abs(x-y)*e : abs(x-y)*w)));
			}else {
				ans = Math.min(ans,  (x*nw + (y >= 0 ? y*e : -y*w)));
				ans = Math.min(ans, (x*ne + (y >= x ? abs(x-y)*e : abs(x-y)*w)));
			}
			pw.println(ans);
		}

		pw.close();
	}

	static void sort(int[] a) {
		ArrayList<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;
import java.util.StringTokenizer;
import static java.lang.Math.*;

public class Main {

	public static void main(String[] args) {
		FastScanner sc =new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		int T=sc.nextInt();
		while(T-- > 0) {
			long x = sc.nextInt();
			long y = sc.nextInt();
			long ne = sc.nextInt();
			long e = sc.nextInt();
			long se = sc.nextInt();
			long sw = sc.nextInt();
			long w = sc.nextInt();
			long nw = sc.nextInt();
			ne = Math.min(ne, nw+e);
			e = Math.min(e, se+ne);
			se = Math.min(se,  e+sw);
			sw = Math.min(sw, se+w);
			w = Math.min(w,  sw + nw);
			nw = Math.min(nw,  w+ne);
			long ans = Long.MAX_VALUE;
			if(x <= 0) {
				ans = Math.min(ans,  (-x*se + (y >= 0 ? y*e : -y*w)));
				ans = Math.min(ans, (-x*sw + (y >= x ? abs(x-y)*e : abs(x-y)*w)));
			}else {
				ans = Math.min(ans,  (x*nw + (y >= 0 ? y*e : -y*w)));
				ans = Math.min(ans, (x*ne + (y >= x ? abs(x-y)*e : abs(x-y)*w)));
			}
			ans = min(ans , ((y <= 0 ? -y*sw : y*ne) + (x >= y ? abs(x-y)*nw : abs(x-y)*se)));
			pw.println(ans);
		}

		pw.close();
	}

	static void sort(int[] a) {
		ArrayList<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}

	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}

}


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

public class Practice {
  BufferedReader br;
  PrintWriter out;
  StringTokenizer st;
  boolean eof;
  private static final long MOD = 1000L * 1000L * 1000L + 7;
  private static final int[] dx = {0, -1, 0, 1};
  private static final int[] dy = {1, 0, -1, 0};
  private static final String yes = "Yes";
  private static final String no = "No";

  void solve() {
//    int T = 1;
    int T = nextInt();
    for (int i = 0; i < T; i++) {
      helper();
    }
  }

  void helper() {
    long x = nextInt();
    long y = nextInt();
    int n = 6;
    long[] arr = nextLongArr(n);
    long[] min = new long[n];
    for (int i = 0; i < min.length; i++) {
      int prev = (i - 1 + n) % n;
      int nxt = (i + 1) % n;
      min[i] = Math.min(arr[i], arr[prev] + arr[nxt]);
    }

    int[][] directions = {{1, 1}, {0, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, 0}};
    for (int i = 0; i < directions.length; i++) {
      int j = (i + 1) % n;
      int[][] mat = new int[2][2];
      mat[0][0] = directions[i][0];
      mat[0][1] = directions[j][0];
      mat[1][0] = directions[i][1];
      mat[1][1] = directions[j][1];
      int[][] inv = invert(mat);
      long a = inv[0][0] * x + inv[0][1] * y;
      long b = inv[1][0] * x + inv[1][1] * y;
      if (a >= 0 && b >= 0) {
        long res = a * min[i] + b * min[j];
        outln(res);
        return;
      }
    }
  }

  int[][] invert(int[][] original) {
    int[][] res = new int[2][2];
    int dter = original[0][0] * original[1][1] - original[0][1] * original[1][0];
    res[0][0] = 1 / dter * original[1][1];
    res[0][1] = 1 / dter * (-original[0][1]);
    res[1][0] = 1 / dter * (-original[1][0]);
    res[1][1] = 1 / dter * original[0][0];
    return res;
  }

  void shuffle(long[] a) {
    int n = a.length;
    for(int i = 0; i < n; i++) {
      int r = i + (int) (Math.random() * (n - i));
      long tmp = a[i];
      a[i] = a[r];
      a[r] = tmp;
    }
  }
  long gcd(long a, long b) {
    while(a != 0 && b != 0) {
      long c = b;
      b = a % b;
      a = c;
    }
    return a + b;
  }
  private void outln(Object o) {
      out.println(o);
  }
  private void out(Object o) {
      out.print(o);
  }
  private void formatPrint(double val) {
      outln(String.format("%.9f%n", val));
  }
  public Practice() {
    br = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out);
    solve();
    out.close();
  }
  public static void main(String[] args) {
    new Practice();
  }

  public long[] nextLongArr(int n) {
    long[] res = new long[n];
    for(int i = 0; i < n; i++)
      res[i] = nextLong();
    return res;
  }
  public int[] nextIntArr(int n) {
    int[] res = new int[n];
    for(int i = 0; i < n; i++)
      res[i] = nextInt();
    return res;
  }
  public String nextToken() {
    while (st == null || !st.hasMoreTokens()) {
      try {
        st = new StringTokenizer(br.readLine());
      } catch (Exception e) {
        eof = true;
        return null;
      }
    }
    return st.nextToken();
  }
  public String nextString() {
    try {
      return br.readLine();
    } catch (IOException e) {
      eof = true;
      return null;
    }
  }
  public int nextInt() {
    return Integer.parseInt(nextToken());
  }
  public long nextLong() {
    return Long.parseLong(nextToken());
  }
  public double nextDouble() {
    return Double.parseDouble(nextToken());
  }
}


import java.io.*;
import java.util.*;
public class d2prac implements Runnable
{
	private boolean console=false;

	public void solve()
	{
		int i; long y=in.ni(); long x=in.ni(); long c[]=new long[7];
		for(i=1;i<=6;i++)
			c[i]=in.nl();
		long ans=0;
		if(x*y>0)
		{
			long min=Math.min(Math.abs(x), Math.abs(y));
			if(x<0)
			{
				ans+=Math.min(min*c[4], min*(c[5]+c[3]));
				y+=min; x+=min;
			}
			else
			{
				ans+=Math.min(min*c[1], min*(c[6]+c[2]));
				y-=min; x-=min;
			}
		}
		if(x>0)
			ans+=Math.min(c[2]*x, (c[1]+c[3])*x);
		else
		{
			x=-x;
			ans+=Math.min(c[5]*x, (c[6]+c[4])*x);
		}
		if(y>0)
			ans+=Math.min(c[6]*y, (c[5]+c[1])*y);
		else
		{
			y=-y;
			ans+=Math.min(c[3]*y, (c[4]+c[2])*y);
		}
		out.println(ans);
	}
	@Override
	public void run() {
		try {  init();  }
		catch (FileNotFoundException e) {  e.printStackTrace();   }

		int t= in.ni();

		while (t-->0) {
			solve();
			out.flush(); }
	}
	private FastInput in;    private PrintWriter out;
	public static void main(String[] args) throws Exception {	 new d2prac().run();	 }

	private void init() throws FileNotFoundException {
		InputStream inputStream = System.in; 	 OutputStream outputStream = System.out;
		try {  if (!console && System.getProperty("user.name").equals("sachan")) {
			outputStream = new FileOutputStream("/home/sachan/Desktop/output.txt");
			inputStream = new FileInputStream("/home/sachan/Desktop/input.txt");   	}
		}	catch (Exception ignored) {	}
		out = new PrintWriter(outputStream); 	 in = new FastInput(inputStream);
	}
	static class FastInput { InputStream obj;
	public FastInput(InputStream obj) {	this.obj = obj;	}
	byte inbuffer[] = new byte[1024];  int lenbuffer = 0, ptrbuffer = 0;
	int readByte() { if (lenbuffer == -1) throw new InputMismatchException();
	if (ptrbuffer >= lenbuffer) { ptrbuffer = 0;
	try { lenbuffer = obj.read(inbuffer);  }
	catch (IOException e) { throw new InputMismatchException(); } }
	if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; }

	String ns() { int b = skip();StringBuilder sb = new StringBuilder();
	while (!(isSpaceChar(b)))	  { sb.appendCodePoint(b);b = readByte(); }return sb.toString();}

	int ni() { int num = 0, b;boolean minus = false;
	while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
	if (b == '-') { minus = true;b = readByte(); }
	while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else {
		return minus ? -num : num; }b = readByte(); }}

	long nl() { long num = 0;int b;boolean minus = false;
	while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
	if (b == '-') { minus = true;b = readByte(); }
	while (true) { if (b >= '0' && b <= '9') { num = num * 10L + (b - '0'); } else {
		return minus ? -num : num; }b = readByte(); } }

	boolean isSpaceChar(int c) { return (!(c >= 33 && c <= 126)); }
	int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; }
	float nf() {return Float.parseFloat(ns());}
	double nd() {return Double.parseDouble(ns());}
	char nc() {return (char) skip();}
	}
}


// No sorcery shall prevail. //
import java.util.*;
import java.io.*;
public class InVoker {
	//Variables
	static long mod = 1000000007;
	static long mod2 = 998244353;
	static FastReader inp= new FastReader();
	static PrintWriter out= new PrintWriter(System.out);
	public static void main(String args[]) {
	   	InVoker g=new InVoker();
	   	g.main();
	   	out.close();
	}

	//Main
	void main() {

		int t=inp.nextInt();
		while(t-->0) {
			long x=inp.nextLong();
			long y=inp.nextLong();
			long c[]=new long[7];
			for(int i=1;i<=6;i++) c[i]=inp.nextLong();
			long gg;
			if(x>=0 && y>=0) {
				if(y>=x) {
					gg=Math.min(c[1]*y+c[3]*(y-x), Math.min(c[2]*y+c[6]*x,c[1]*x+c[2]*(y-x)));
				}else { // x>y
					gg=Math.min(c[1]*x+c[5]*(x-y), Math.min(c[6]*x+c[2]*y, c[1]*y+c[6]*(x-y)));
				}
			}else if(x>=0) {
				gg=Math.min(c[1]*x+c[5]*(x-y), Math.min(c[5]*(0-y)+c[6]*x,c[4]*(0-y)+c[6]*(x-y)));
			}else if(y>=0) {
				gg=Math.min(c[1]*y+c[3]*(y-x), Math.min(c[3]*(0-x)+c[2]*y,c[4]*(0-x)+c[2]*(y-x)));
			}else {
				if(x<=y) {
					gg=Math.min(c[4]*(0-y)+c[3]*(y-x), Math.min(c[3]*(0-x)+c[5]*(0-y),c[4]*(0-x)+c[2]*(y-x)));
				}else { // x>y
					gg=Math.min(c[4]*(0-x)+c[5]*(x-y), Math.min(c[3]*(0-x)+c[5]*(0-y),c[4]*(0-y)+c[6]*(x-y)));
				}
			}
			out.println(gg);
		}

	}

	/*********************************************************************************************************************************************************************************************************
	 * ti;.                               .:,:i:         :,;;itt;.        fDDEDDEEEEEEKEEEEEKEEEEEEEEEEEEEEEEE###WKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWW#WWWWWKKKKKEE      :,:.    f::::. .,ijLGDDDDDDDEEEEEEE*
	 *ti;.                               .:,:i:          .:,;itt;:      GLDEEGEEEEEEEEEEEEEEEEEEDEEEEEEEEEEE#W#WEKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKKKKKKG.     .::.      f:,...,ijLGDDDDDDDDEEEEEE *
	 *ti;.                               .:,:i:            :,;;iti,    :fDDEEEEEEEEEEEEEEEKEEEEDEEEEEEEEEEEW##WEEEKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWWKKKKKKEG     .::.       .f,::,ijLGDDDDDDDDEEEEEE *
	 *ti;.                               .:,:i:             .,,;iti;.  LDDEEEEEEEEEEKEEEEWEEEDDEEEEEEEEEEE#WWWEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWWWWWKKKKKEDj    .::.        .:L;;ijfGDDDDDDDDDEEEEE *
	 *ti;.                               .:,:i:              .:,;;iii:LLDEEEEEEEEEEEKEEEEEEEEDEEEEEEEEEEEW#WWEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKWWKWWWWWWWWWWWWWWKKKKKKKEL    .::.         .:;LijLGGDDDDDDDDEEEEE *
	 *ti;.                               .:,:;:                :,;;ittfDEEEEEEEEEEEEEEEEKEEEGEEEEEEEEEEEKWWWEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWWWWKKKKKKKELj   .::.          :,;jffGGDDDDDDDDDEEEE *
	 *ti;.                               .:,:i:                 .,;;tGGDEEEEEEEEEEEKEEEKEEEDEEEEEEEEEEEEWWWEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWKWWWWWWKKKKKKKEEL   .::.          .:;itDGGDDDDDDDDDEEEE *
	 *ti;.                               .:::;:                   :;ifDEEEEEEEEEEEEKEEEKEEEEEEEEEEEEEEEWWWEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WWWKKKKKKKKEEf   .::.           :,itfGEDDDDDDDDDDDEE *
	 *ti;.                               .:::;:                    :GGEEEEEEEEEEEKEKEEKEEEEEEEEEEEEEEEEWWEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKKKKEEDG  .::.           .,;jfLGKDLDDDDDDEEDD *
	 *ti;.                               .:::;:                     fDEEEEEEKKKKKKKKKEKEEEEEEEEEEEEEEE#WEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#KKKKKKKKKKEEf .:::.           .,;tfLGDEDDDDDDDDEEE *
	 *ti;.                                :::;:                    fDEEEEEEKKKKKKKKKKWKEEEEEEEEEEEEEEEWKEEEEEEEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKW##KKKKKKKKKEEft :::.           .,;tfLGDDDKDDDDDDDDD *
	 *ti;.                                .::;:                    fDEEEEEEKKKKKKKWKKKKKEEEEEEEEEEEEE#WEEWEEEEEDEEDEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKKKEEGG :,:.           .,;tfLGGDDDKDDDDDDDD *
	 *ti;.                                .:.;:                   tGDEEEEKKKKKKKKKKKKKKKKKEEEEEEEEEEEWEEKWEEEEEEEDEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKWWWKKKKKKKEEDf :::.           .,;tfLGGDDDDEDDDDDDD *
	 *ti;.                                .::;:                   fDEEEEEKKKKKKKKKKKWKKKKKKKKEEEEEEEWWEEWEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKW##KKKKKKKEEEft.::.           .,;tfLGGDDDDDDEDDDDD *
	 *ti;.                                .:.;:                  tGDEEEKKKKKKKKKKKKKKKKKKKKKKEKEEEEE#EEWWEEEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKW#WKKKKKKEEEGD:::.           .,;tfLGGDDDDDDDEDDDD *
	 *ti;.                                .:.,.                  LDEEEEKKKKKKKKKKWKWKKKKKKKKKKKKEEEKWEKW#EEEEEEEEEEEEEEEEKEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKW##KKKKKKEEEEf,,:.           .,;tfLGGDDDDDDDDEDDD *
	 *ti;.                               ..:.,.                 LGDEEEEKKKKKKKKKKWKKKKKKKKKKKKKKKKKWEEW#WEEEEEEEEEEEEEEEKEEEEEEEEEEEEEEEEEEEKEKKKKKKKKKKKKKKKK##KKKKKEEEEEfi;,.           .,;tfLGGDDDDDDDDDKDD *
	 *tt;.                                .:.,:                 jDEEEEKKKKKKKKKKWWKKKKKKKKKKKKKKKKKWKE#WWEEEEEEEEEEEEEEWEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKKKKKKWWKKKKEEEEDfG;,:           .,;tfLGGDDDDDDDDDDKD *
	 *tii,.                               .:.,.                tGDEEEEKKKKKKKKKKWWWKKKKKKKKKKKKKKKWKKWWWKEEEEEEEEEEEEEKEEEEEEEEEEEEEEEEEEEEEEEEEEEEEKKKKKKKKKKKW#KKKKEEEEDGGi;,.          .,;tfLGGDDDDDDDDDDDE *
	 *ti;;,:.                             .:.,:                fDEEEEKKKKKKKKKKKWKKKKKKKKKKKKKKKKKWEK#WWKEEEEEEEEEEEEDEEEEEEEEEEEEEEGEEEEEEEEEEEEEEEEEEEKKKKKKKWWKKEEEEEEDDf;;;,.         .,;tfLGGDDDDDDDDDDDD *
	 *tii;,,:..                           ...,.               ;LEEEEEKKKKKKWKKKKWKKKKKKKKKKKKKKKKKEKKW#WEEEEEEEEEEEEEjEEEEEEEEEKEEEEGEEEEEEEEEKEEEEEEEEEEEEEEEEE#WKEEEEEEDDf;;;;,:        .,itfLGGDDDDDDDDDDDD *
	 *ti;,,,,,:.                          ...,.               LDEEEEKKKKKKKKKKKWWWKKKKKKKKKKKKKKKWKK#W#WEEEEEEEEEEEDDLEEEEEEEEEWEEEEDEEEEEEEEEKEEEEEEEEEEEEEEEEEWWEEEEEEEDDfj,,,,,:.      .,itfGGGDDDDDDDDDDDD *
	 *tii,,,,::::.                        ...,:              .fDEEEEKKKKKKWKKKKWWWKKKKKKKKKKKKKKKEKKW#WWEEEEEEEEEEEKiKEEKEEEEEEWEEEEDEEEEEEEEEEEEEEEEEEEEEEEEEEEWWEEEEEEEDDLD:::,,,:.     .,ijfGGGDDDDDDDDDDDD *
	 *ti;:::::::::..                      .:.,:              LDEEEEKKKKKKKWKKKKWWKKKKKKKKKKKKKKKKtKKWWWWKEEEEEEEEEDiiDEEEEEEEEWWEEEEEEDEEEEEEEEEEEEEEEEEEEEEEEEEEWKEEEEEDDDGL:. .:,,,:    .,ijLGGGDDDDDDDDDDDD *
	 *tt;. .::::::::..                    ...,:             :fDEEEKKKKKKKKKKKKWW#KKKKKKKKKKKKKKKKfKKWWWWKEEEEEEEEDti,DEKEEEEEEWWEEEDEEEEEEEEEKEEEEEEEEEEEEEDEEEEE#WEEEEEGGDGf:.  .:,;,:.  .,ijLGGDDDDDDDDDDDDD *
	 *tt;.   .:::::::..                   ...,:             GDEEEKKKKKKKKWKKKKWWWKKKWKKKKKKKWWWKDEKLWWWWKKEEEEEEDEi,LDEEEEEEEEWWEEEEEEEEEEEEEEEEEEEEEEEEEDEDEEEEEW#EEEEDDDDGf,.    :,,,:...,ijLGGGDDDDDDDDDDDD *
	 *tt;.    .....::::..                 ...,:             fDEEEKKKKKKKKWKKKKWWWWKKKWKKKKKKKKKKfWKiWWW#KKEEEEEEEi;.EDfEEEDEEiWWEEEEEEEEEEEEDGKEEEEEEEEEEDEEEEEEEWWEEEEDDDGGLi.     .,;,:::,ijLGGGDDDDDDDDDDDD *
	 *tt;.      ....:::::.                ...,.            iDEEEEKKKKKKKKWKKWKWWWWWKKWWWKKKKKKKKtWKt#WWWKKEEEEEDji..DDKDDEDEGiWKEEEEEEEEEEDDEjEEEEEEEEEEEDEEEEEEEKWKEEDDDDGGff.      .:,;,,;ijLGGGDDDDDDDDDDDD *
	 *tt;.        ....::::..              .:.,:           .LDEEEKKKKKKKKKKKKWWWWKWWWWWWWWWWKKKKWtKKiDWWWKKKEEEEKi:..DEDDDDDDiiWKEEEEEEEEEEDDEijDEEEEEKEEEEEEEEEEEEWWEEGDDDGGLG.       .:,;;iijLGGGDDDDDDDDDDDD *
	 *tt;.          .....:::..            ...,.           .fEEEEKKKKKKKKWKKKKWWWWWWWWWWWWWWKWKKKiKDiLWWWWKEEEEEi,..fD:DDDDDti;WEEEEEEEEEEKDDi:iDDEEEEWEEEEEEEEEEEE#WEEGDDDDGGG.         :,iitjLGGGDDDDDDDDDDDD *
	 *tti.            .....:::..          ...,.           GDEEEKKKKKKKKKWKKKWWW#WWWWWWWWWWWKWKKjiEjitWWWKKWEEEDi...DDLDDDDji;;WEEEEEEEEEEEDEj.iDDEEEEWEEEEEEEEEEEEWWEEDDDDDDGf.          .,;tjfLGGDDDDDDDDDDDD *
	 *tti.              ....::::..        ...,.           fEEEKKKKKKKKKKKKKKKW#WWWWWWWWWWWWWWWWtiEiiiWWWKKEWKEi....D.EDDDEi;.fWEEEEEEEEEEDDfL.;EDDEEEWEEEEEEEEEEEEWWEEEDDDDDGf.           :;ijfLGGDDDDDDDDDDDD *
	 *tti.                ....::::..      ...,.          LDEEEKKKKKKKKKKKKKKWWWWWWWWWWWWWWWW####WKiiiWWWKKKEEK,...:E:DDDEii..GWEEEEEEEEDWDDiL.,KDDEEEWEEEEEEEEEEEEWWKEEDDDDDGf:           .,itfLGGDDDDDDDDDDDD *
	 *tti.                 .....:::..     ...,.          fDEEEKKKKKKKKKWKKKKWWWWWWWWWWWWW########WLiiWWWKKKEEjD...G,DDDDi;...EWEEEEEEEEDKDEii..LDDEEEWEEEEEEEEEEEEWWWEEDDDDDGfi           .,;tfLGGGDDDDDDDDDDD *
	 *tti.                   .....:::..   ...,.         iGEEEKKKKKKKKKKWKKKKWWWWWWWWWWWW###########KiWWWKKEEE,.D..D.DDDii:...KKEEEEEEEEEDDj:...tEDEEEWEEEEEEEEEEEEWWWEEEDDDDDLL           .,;tjLLGGDDDDDDDDDDD *
	 *tti.                     ....::::......:.         LEEEKKKKKKKKKKWWKKKWWW#KWWWWWWWW#####W####W##KWWKKEEL..:D.jjDDi;,....KKEEEEEEEDfDDi...:iKDEEEWKEEEEEEEEEEEWWWEEEEDDDDLG           .,;tjLLGGDDDDDDDDDDD *
	 *tti.                        ...::::::..,.        :GEEEKKKKKKKKKKKKWWWWW##WWWWWWWWW##WKWK#W#W####WWKEEK.....G.DDti,.....KKEEEEEEDWGDf.,...iKDEEEWWEEEEEEEEEEEW#WEEEEEDDDGL           .,;tjLLGGDDDDDDDDDDD *
	 *tti.                         ....::::::,.        GDEEKKKKKKKKKKKKKWWWW###WWWWWWWWWW#WWWK###W#####WKEKK.....jDDL;;......KKEEEEEEEEEDi.f...;KDEEEWWEEEEEEEEEEEWWWWEEEEEDDGf           .,;tjLLGGDDDDDDDDDDD *
	 *tti.                           ....:::,,.       .LEEEKKKKKWKKKKKWWWWWW###WWWWWWWWWW#WWKW#WW##W#WWWKEKD:....:DD:;......;KEEEEEEEKiDD..f...,KKEEEWWEEEEEEEEEEEWWWWEEEEEDDDf           .:;tjLLGGGDDDDDDDDDD *
	 *tti.                             ...::,,,:.     GDEEKKKKKKKKKKKKWWWWWWW#WWWWWWWWWWW#KjKWWWWWWWWWWWWEK.j,..;fD.;.......fKEEEEEDKG:Di..,....DKEEEWWEEEEEEKEKKKWWWWEEEEEEDDf           .:;tjLLGGDDDDDDDDDDD *
	 *jti.                              ...::,,,,:.  .fEEEKKKKKWKKKKKKWWWWWWW#WWWWWWWWWWK#KKKWWWWWWWWWWWWWK..f:.:G.,:.......EKEEEEEKK;:E:.......fKEEEWWKEKEKKKKKKKW#WWEEEEEEDDf:          .,;tfLLGGDDDDDDDDDDD *
	 *tti.                               ...:,,,;;,: iDEEKKKKKWKKKKKKKWWWWWWW#WWWWWWWWWWK#WDKWWKKWWWWWWWWWE..;G:G..,........KKEEEEEKi.Gi..:.....tKEEKWWWKKKKKKKKKKW##WKEEEEEEDfi          .,;tfLLGGGDDDDDDDDDD *
	 *tti.                               ....::,,;;;,LEEKKKKKKWKKKKKWWWWWWW###WWWWWWWWWWKWWDKWEEEWKKWWWWWKKj.:LG..;.........EKEEEEKG;.G...;.....;KKEKWWWKKKKKKKKKKW##WWKEEEEEDfL          .,;tfLGGGDDDDDDDDDDD *
	 *jti.                                ...::::,;ijDEEKKKKKWKKKKKKWKWWWWW##WWWWWWWWWWWKK#KKGDGDWEEWKKWKKGE,.i;.:.........:EKEEEKE;.:L...j.....,KWEKWWWKKKKKKKKKK####WKKEEEEDLG          .,;tfLGGGGDDDDDDDDDD *
	 *jti.                                ...:...,,;GEEKKKKKWWKKKKKWWWWWWWW###WWWWWWWWWKKKWWKiLGGEDEDEKGKKiEG..;...........jKEEEKK;:.G....,.....:KKEWWWWKKKKKKKWKK####WKKKKEEEGL          .,;tfLGGGGDDDDDDDDDD *
	 *jti.                                ...:.  .:,GEEKKKKKWKKKKKWWWWWWWW####WWWWWWWWWKKKWWKii;fDLGDK: EEi:E:.............EKEEKK;;..L...........KKKWWWWKKKKKKKWKK####WKKKWKEEDf          .,;tfGGGGDDDDDDDDDDD *
	 *jti.                                ...:.    ,EEKKKKKWWKKKKKWWWWWWWWW###WWWWWWWWKKKKfWWLt;i,. fi  EG..D:.............EKEKK;;..t....:.......KWKWWWWKKKKKKKWKK####WKKKWEEEDf:.        .,;tfGGGGDDDDDDDDDDD *
	 *jti.                                ...:.    GEEKKKKKWKKKKKWWWWWWWWW####WWWWWWWKKKKKt;KKEfff     .;t.................KKKKi;:..GtGGfG.......KWWWWWWKKKKKKKWKK###WWWKKKKEEEf,,:       .,;tfGGGGDDDDDDDDDDD *
	 *jti.                                ...:.    GEKKKKKWWKKKKKWWWWWWWWWW##WWWWWWWKKKKKKt;EiKKKK,   ...t................jEKKG;;..,.....,LGi....KWWWWWWKKKKKKWKKKW####WKKKKKEEL,,,:.     .,;tfGGGDDDDDDDDDDDD *
	 *jti.                                ...:.  .GEEKKKKKWKKKKKWWWWWWWWWW###WWWWWWWKKKKKKtiE::tGG........................EEEj;;...,.........:D..DKWWWWWWKKKKK#KKW###W#WKKKKKEEfj:,,,:.   .,;tfGGGDDDDDDDDDDDD *
	 *jti.                                ...:.   DEKKKKKWWKKKKKWWWWWWWWW####WWWWWWWKKKKKKiiE:::.::.......................EEi;;...j.....f......:iDKWWWWWWKKKKK#WW######WKKKKKEELG :,,,,:. .,;tfGGGDDDDDDDDDDDD *
	 *jti.                                ...:.  fEEKKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKK;tE::..........................DD;.;,.::......;........EWWWWWWWKKKKW#WW#####WWKKKWKKELG  .:,,,:::,;tfGGGDDDDDDDDDDDD *
	 *jti.                                ...:. .DEKEKKKWWKKKKWWWWWWWWWWW###WWWWWWWWKKKKKE,iD::..........................D..,;.,;tLffi...........DWDWWWW#KKKWWWWW#####W#KKKWKEEGL    .:,;,,,;tfGGGDDDDDDDDDDDD *
	 *jti.                                ...:. ;EEKKKKWWKKKKKWWWWWW#WWWW####WWWWWWKKKKKEL:iD:..........................j ..;..;;:.....i,........DKtWWWWWKKWWWWWW#####WWWKKKEKEDf     .:,;;;itfGGGDDDDDDDDDDDD *
	 *jti.                                ...:. DEKKKKKWWKKKKWWWWWWW#WWWW####WWWWWWKKKKKEj:iG...............................:....................GKiWWWWWKKWW#WWW######WWKKKKKEEf       .,;iitfGGGDDDDDDDDDDDD *
	 *jti.                                ...:.:EKKKKKWWKKKKKWWWWWWW#WWW#####WWWWWKWKKKKEi:if:.................................iEKEKKKKKKDj......DKiWWWWWKWK##WW#######WWKKK:KEEL        .:;itfGGGDDDDDDDDDDDD *
	 *jji.                                ...:,DEEKKKWWWKWKKWWWWWWWWWWWW#####WWWWWWWKKKKEi:it..................................j.  KKKKKKKKKKKf..DKiWWWWWKWW##WW#######WWKKK,KEEf         .,;tfGGGDDDDDDDDDDDD *
	 *jji.                                ..L:iDEEKKKWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKKi.i;..................................  . KKKWWWWWWWWK..DGiWWWWWKK##WWW#####W#WWKKKjEKEL,        .:;tfGGGDDDDDDDDDDDD *
	 *jji.                               .f:::EEEKKKWWWKKKKKWWWWWWWWWWWW#####WWWWWKWKKKKK;.i,.................................::  KKEKWWWWWWfWK..EiiWWWWWKWW#WW##########KKKD,KELj        .:;tfGGDDDDDDDDDDDDD *
	 *jji.                             .t::::,DEEKKKWWKKKKWWWWWWWWW#WWWW#####WWWWKKWKKKEK;.i:.................................GDDEEEKKKWWWWWtWWD.E;iWWWWWW###WW#########WWKKK.EEDG        .:;tfGGGDDDDDDDDDDDD *
	 *jji.                          . j..::::EKEKKKWWWKKKKWWWWWWWWW#WWW######WWWWKKWKKKEK;.t:.................................ELLEDDEEEWWWWEtWK,.KiiWWWWWW###W##########WWKKK:EEEG     .;tjfLLGDDDDDDDDDDDDDDD *
	 *jji.                          i.::::::,EEEKKWWWKKKKKWWWWWWWWW#WWW#####WWWWWKWKKKKEE,.t..................................DfiEGDDDEEKKKttKWG.KiiWWWWW##WWW##########WWKKK:fEEL  ,fGGGDDDDDDDDEEEDDDDDDDDDD *
	 *jji.                        .;:..:::::DEEEKKWWWKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKED,.t..................................ifjDDGGEGDKK.ttKKE.DiWWWWW###WW##########WWWKKK:.KELiLGGGGDDDDDDDDDDDDEEEDDDDDDD *
	 *jji.                       i.:.::::::,KEEKKWWWKKKKKKWWWWWWWWW#WWWW####WWWWWWWKKKKEL:.j..................................GGf,;ifLLED .iiKKi:fWWWWWW##W#W##########WWWKKK:.KKLGGDDDDDDDDDDDDDDDDEDDEEDDDDD *
	 *jji.                     .j:.::::::::EEEKKKWWWKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKf:.f..................................:EEfftf .,. ;iE,..jWWWWWWW###W############WWKKK,:KKGDDDDDDDDDDDDDDDDDDDDDDDEDDDD *
	 *jji.                    .:.::::::::,,EEEKKWWWKKKKKKKWWWWWWWW##WWW#####WWWWWWWKKKKKt..G....................................EEELL;   .j....tKWWWWWWW################WWWKKtfGKGEDDDDDDDDDDDDDDDDDDDDDDDEEDD *
	 *jji.                   :...:::::::,,jEEKKWWWWKKKKKKWWWWWWWWW##KWW#####KWWWWWWKKKKEi..D....................................:jEEE.........;KKWWWWWWWW#WW##W##########WWKKDLGKEKDDDDDDDDDDDDDDDDDDDDDDDDDED *
	 *jji.                  i:.::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWW##WWW#####WWWWWWWKKKKKi..D......................................:::::......,KKKWWWWWWWWW#####W########WWWKKKGGKKEGGGGGGGGDDDDDDDDDDDDDDDDDDE *
	 *jji.                 i..:::::::::,,tEEKKWWWWKKKKKWWWWWWWWWWW##WW######WWWWWWWKKKKKi..D......................................::::......:EKKKWWWWWWWWWWW##WW########W#WKKWGGKKGGGGGGGGGGGGGGGDDDDDDDDDDDDD *
	 *jji.                .:::::::::::,,,EEEKKWWWWKKKKKWWWWWWWWWWW##WW#####WWWWWWWWKKKKKi..D....................................:::::::::..tELii;KWWWWWWWWWW##WW######WWWWWWKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGDG *
	 *jjt.                :.::::::::,,,,fEEKKWWWWKKKKKKWWWWWWWWWW###WW####WWWWWWW#WKKKKKi..D....................................:::::::.:.,;;;;;;,KKWWWWWWWWW#WW########WWWKKWGGGKGGGGGGGGGGGGGGGGGGGGGGGGGGGG *
	 *jji.               ;.::::::::,,,,;EEEKWWWWWKKKKKWWWWWWWWWWWW##WW###WKWWWWWK#WKKKKKi..G......................................:::::::,;;;;:...KKKWWWWWWWWWKWW#######WWWWKKGLGKDGGGGGGLLGGGGGGGGGGGGGGGGGGG *
	 *jjt.              f.:::::::::,,,,fEEKKWWWWWKKKKKWWWWWWWWWWW###WW##WKKWWWWWW#WKKKKK;.jt........i.............................:::::::;j;;....:E.KKKWWWWWWWKWW#####W#WWWWKKLLGWEEGGGGGLGGGGGGGGGGGGGGGGGGGG *
	 *jjt.             ...:::::::,,,,,;DEEKWWWWWKKKKKWWWWWWWWWWWW####WWWKKKWWWWWWWWKKKKK;.E;.........t.............................:::::ii;;.....D...KKWWWWWWWKWW#####WWEWWWKKGGGEKKGGGGGLGGGGGGGGGGGGGLGGGGGG *
	 *fji.             ;.:::::::,,,,,;LEEKKWWWWWKKKKKWWWWWWWWWWWW####KWKKKKWWWWWWWWKKKKKi.D;..........j.............................:::tt;,.....:.....KKWWWWWWKWWWW##WWWGWWWKKGGGGKEGGGGGGGGGGGGGGGGGGGLLGGGGL *
	 *fji.            t::::::::,,,,,,;EEEKWWWWWKKKKKKKWWWWWWWWWWW##WKWKKKKKWWWWWWWWKKKKKi:D;............j...........................::LL;,.............KKWWWWWKWWWWWWWWWGWWWKKGGGGKGGGGGGGGGGGGGGGGGGGGLLGGGGL *
	 *fjt:            .:::::::,,,,,,,DEEKWWWWWWKKKKKKKWWWWWWWWWWWWKKWKKKKKKWWWWK#WWKKKKWitE;........... ............................:G;;:...............KKKWWKKWWWWWWWWWGWWWKKGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGL *
	 *fjji;:.       .f:::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWWKKKKKKKKKKKWWKWWWWWKKKKWGKD;........................................L;;..................DKKWKKWWWWWWWWWGWWWKKDGGGKDGGGGGGGGGGGGGGGGGGGGGGGGGG *
	 *fjjtii;,:.     :::::::,,,,,,,;EEEKWWWWWWKKKKKKWWWWWWWWWWKKKKKKKKKKKKWWWWWW#WWKKKKWiEj;......................................:i,;....,...............;KKEKWWWWWWWWWGKWWKKDDGGDEGGGDGGGGGDGGGGGGGGGGGGGGGG *
	 *fjtiiiii;;:.  j::::::,,,,,,,;;EEEKWWWWW#KKKKKWWWWWWWWWKKKKKKWKKKKKKKWWWWWWWWWKKKKWtEL;:....................................;;;:...,;j................:KEEWWWWWWWWWDDWWKKDDDDDKDDDDDDDDDDDDDDDGGGGGGGGGGG *
	 *fjti;;iiii;;,:::::::,,,,,,,,;EEEKWWWWWWWKKKKWWWWWWWWKKKKKKKWKKKKKKKWWWWWWW#W#KKKKWEEii;...................................f;:....,;L...................EEKWWWWWWWWDDWWKKDDDDDKEDDDDDDDDDDDDDDDDDDDDDGGGG *
	 *fjt,,,;;;;ii;f::::::,,,,,,,;;EEKWWWWWWWKKKKKWWWKWWKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWKEij;:...............................:G;,.....,;f....................:tKKWWWWWWWDDWWKKDDDDDKKDDDDDDDDDDDDDDDDDDDDDDDDD *
	 *jjt. ..:,;;;;,::::,,,,,,,,;;GEEWWWWWWWWKKKKWKKWKKKKKKKKKKKKKKKKKKKKWWWWWWW#W#KKKKWEDi;j;............................,Li;L;;;..,;;f........................KKKKWWWKDDWWKKDDDGDKKGGGGGGGGDGDDDDDDDDDDDDDDD *
	 *fjt.    .:,,,:::::,,,,,,,;;;EEKWWWWWWWKKKKKKWKKKKKKKKKKKKKKKKKKKKKWKKKWKW##W#KKKKWEti;;G;........................tEEEL;;;;;;;;;;L..........................DKKKKKEDDWWKEDGftiLE;;;;itjLGGGGGGDDDDDDDDDDD *
	 *fjt.       .j::::,,,,,,,;;;DEEWWWWWWWWKKKKWKKKKKKKKKKKKKKKKKKKKKKKWKKWWWK##W#KKKKKEii;;;L;...................iDEEEEEEKKi;j;;;;jD.....:......................,KKKKDGGEKKE:::::;E::::::::::,tLGGDDDDDDDDDD *
	 *fjt.       .;:::,,,,,,,;;;;EEKWWWWWWWWKWKKKKKKKKKKKKKKKWKKKKKKKKKKWKKWWWW#WW#KKKKKKii;;;;f;.............:tDEEEEEKKKKKKKKEti;;;L...............................EEKf;:iKKE::::::E::::::::::::::ifDDDDDDDDD *
	 *fjt:        :::,,,,,,,,;;;DEEWWWWWWWWWEKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####KKKKKEiii;;;;f,.........iDEEEEKKKKKKKKKKKKKKKf;iG......i..........................fK::::KKE::::::E::::::::::::::::,tGGDDDDD *
	 *fjt:       t:::,,,,,,;;;;iDEKWWWWWWKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKWWWW####WKKKKLiii;;;;;L,....,Li;EDEEEEKKKKKKKKKKKKKKKKiG......;:...........................:i:::KKE:::::,E,::::::::::::::::::iGDDDD *
	 *jjt.       f::,,,,,,,;;;;GEEWWWWKEEKEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKWWWWW###WWKKKKiii;;;;;;;G,;L;;iiEEEEEEEKKKKKKKKKKKKKWWKE......;t.........:....................j::KEE:,::,,D,,::::,,,,,,:::::::::tDDD *
	 *fjt:.      ,::,,,,,,,;;;;EEWWKEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKWWKKWWWWW#W#KKKKKKiiiiii;;;;;i;;iiiEEKEEKKWKKKKKKKWKKKKKWWWGi;...;t......,;;;;,....................:,EEE,,,,,,D,,,,,,,,,,,,,,,,::,::::tG *
	 *fjt:.     ,::,,,,,,,;;;;DEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWW#W#KKKKKKiiiii;;i;;;;;iiiKKKEKKKKWWKWWWWWWKKKKWWWWW;;;:;L.....;;;;;;;;;....................,KEE,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,; *
	 *fjt:.     f:,,,,,,,;;;;jEDEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWW#W##KKKKKKiiiiiiii;i;;iiiEKKKKKKKKWKWWWWWWWWKKKWWWWWKi;;i.....,jEEfGi;;;;;...................EED,,,,,,E,,,,,,,,,,,,,,,,,,,,,,,,, *
	 *fjt:.    .f::,,,,,,;;jEEDEEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWKWWWWW###KKKKKLiiiiiiiiiiiiiiEEKKKKKKKKWWWWWWWWWWWWKWWWWWWGi;i;,..;jDDDKEGi;;;;;;:................EED,,,,,,D,,,,,,,,,,,,,,,,,,,,,,,,, *
	 *fjt:.  .. ;::,,,,,;;EDDEEEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWKKW#WW####KWKKKiiiiiiiiiiiiijKKKKKKKKKKWWWWWWWWWWWWWWWWWWWWWt;i;;;;i;DDDDDDGi;;;;;;;;:.............EDf;,,,;,G;;;;;;;;;;;;;;;,,,,,,,,,, *
	 *fjt:......:,,,,,,;LDDDEEEEEEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKKKWWWWKWWWW####KKKKKiiiiiiiiiiijKEKKWKKKKKKKWWWWWWWWWWWWWWWWWWWWWWiLiii;i;DEEEEDDE;i;;;;;;;;;:..........EDi,;;;;;L;;;;;;;;;;;;;;;;;;,,,,,,, *
	 *fjt:......:,,,,,;EDDDEEKEEEEEEEEEKKKKKKKKKKKKKKKWKKKKKKKKKKKKKKWWWWKKWWW##W#KWKKWEiiiiiijGKKKKKWWKKKKKKKKWWWWWWWWWWWWWWWWWWWWWWKi;iiiiDDEEEEEEDEi;;;;;;;;;;;;;,:.....ED;;;;;;;j;;;;;;;;;;;;;;;;;;;;;;;,, *
	 *fjt:.....t,,,,,;DDDDEEEKEEEEEEEEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWKKKWWWW##WKWKKWKiiiKKKKKKKKKWWKKKKKKKKWWWWWWWWWWWWWWW#WWWWWWWWWiiiiifLEEEEEEEEDi;i;;;;;;;;;;;;.....DD;;;;;;;i;;;;;;;;;;;;;;;;;;;;;;;;; *
	 *fjt:.....G,,,,,GDDDEEEEEEEEEEEEKKKKKKKKKKKKKKKKWKKKKKKKKKKKKKKKWWWKKKWWW###WKWKKWKitKKKKKKKKKWKKKKKKKKKKWWWWWWWWWWWWWW###WWWWWWWWEiiiiiiiEEEEEEEEDGiiii;;;;;;;;;.....GD;;;;;;;i;;;;;;;;;;;;;;;;;;;;;;;;; *
	 *fjt:.....L,,,,;GDDDEEEEEEEEEEKEKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKWWWWWDGWWW###KKWWKWKKKKKKKKKKKKKKKKKKKKKKKWWWWWWWWWWWWW####WWWWWWWWWiiiiiiiiEEEEEEEEEEDi;i;;;;;;;;.....Lj;;;;;;i;iiiiii;;;;;;ii;;;;;;;;;;; *
	 ***********************************************************************************************************************************************************************************************************/

	void sort(int a[]) {
		ArrayList<Integer> list=new ArrayList<>();
		for(int x: a) list.add(x);
		Collections.sort(list);
		for(int i=0;i<a.length;i++) a[i]=list.get(i);
	}
	void sort(long a[]) {
		ArrayList<Long> list=new ArrayList<>();
		for(long x: a) list.add(x);
		Collections.sort(list);
		for(int i=0;i<a.length;i++) a[i]=list.get(i);
	}
	void ruffleSort(int a[]) {
		Random rand=new Random();
		int n=a.length;
		for(int i=0;i<n;i++) {
			int j=rand.nextInt(n);
			int temp=a[i];
			a[i]=a[j];
			a[j]=temp;
		}
		Arrays.sort(a);
	}
	void ruffleSort(long a[]) {
		Random rand=new Random();
		int n=a.length;
		for(int i=0;i<n;i++) {
			int j=rand.nextInt(n);
			long temp=a[i];
			a[i]=a[j];
			a[j]=temp;
		}
		Arrays.sort(a);
	}

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st==null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine() {
            String s="";
            try {
                s=br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return s;
        }
    }

	long fact[];
	long invFact[];
	void init(int n) {
		fact=new long[n+1];
		invFact=new long[n+1];
		fact[0]=1;
		for(int i=1;i<=n;i++) {
			fact[i]=mul(i,fact[i-1]);
		}
		invFact[n]=power(fact[n],mod-2);
		for(int i=n-1;i>=0;i--) {
			invFact[i]=mul(invFact[i+1],i+1);
		}
	}

	long nCr(int n, int r) {
		if(n<r || r<0) return 0;
		return mul(fact[n],mul(invFact[r],invFact[n-r]));
	}

	long mul(long a, long b) {
		return a*b%mod;
	}
	long add(long a, long b) {
		return (a+b)%mod;
	}

	long power(long x, long y) {
		long gg=1;
		while(y>0) {
			if(y%2==1) gg=mul(gg,x);
			x=mul(x,x);
			y/=2;
		}
		return gg;
	}

    // Functions
	static long gcd(long a, long b) {
		return b==0?a:gcd(b,a%b);
	}
	static int gcd(int a, int b) {
		return b==0?a:gcd(b,a%b);
	}

	void print(int a[]) {
		int n=a.length;
		for(int i=0;i<n;i++) out.print(a[i]+" ");
	}

	void print(long a[]) {
		int n=a.length;
		for(int i=0;i<n;i++) out.print(a[i]+" ");
	}

    //Input Arrays
    static void input(long a[], int n) {
		for(int i=0;i<n;i++) {
			a[i]=inp.nextLong();
		}
	}
	static void input(int a[], int n) {
		for(int i=0;i<n;i++) {
			a[i]=inp.nextInt();
		}
	}
	static void input(String s[],int n) {
		for(int i=0;i<n;i++) {
			s[i]=inp.next();
		}
	}
	static void input(int a[][], int n, int m) {
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				a[i][j]=inp.nextInt();
			}
		}
	}
	static void input(long a[][], int n, int m) {
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				a[i][j]=inp.nextLong();
			}
		}
	}

}