//package codeforces.D1401;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * @author muhossain
 * @since 2020-08-14
 */

public class B {

    public static void main(String[] args) {
        FastReader fs = new FastReader();

        int testCases = fs.nextInt();
        StringBuilder output = new StringBuilder();

        for (int t = 0; t < testCases; t++) {

            int x1 = fs.nextInt(); // 0
            int y1 = fs.nextInt(); // 1
            int z1 = fs.nextInt(); // 2

            int x2 = fs.nextInt(); // 0
            int y2 = fs.nextInt(); // 1
            int z2 = fs.nextInt(); // 2

            long total = 0;

            if (z1 > 0 && y2 > 0) {
                if (z1 >= y2) {
                    total += (y2 * 2);

                    z1 -= y2;
                    y2 = 0;
                } else {
                    total += (z1 * 2);

                    y2 -= z1;
                    z1 = 0;
                }
            }

            if (z2 > 0 && x1 > 0) {
                if (z2 >= x1) {
                    z2 -= x1;
                    x1 = 0;
                } else {
                    z2 = 0;
                    x1 -= z2;
                }
            }

            if (z2 > 0 && z1 > 0) {
                if (z2 >= z1) {
                    z2 -= z1;
                    z1 = 0;
                } else {
                    z2 = 0;
                    z1 -= z2;
                }
            }

            if (y1 > 0 && z2 > 0) {
                if (y1 >= z2) {
                    total -= (z2 * 2);

                    y1 -= z2;
                    z2 = 0;

                } else {
                    total -= (y1 * 2);

                    y1 = 0;
                    z2 -= y1;
                }
            }

            output.append(total).append("\n");
        }

        System.out.print(output);
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

        public static FastReader getFileReader(String fileName) throws FileNotFoundException {
            return new FastReader(new InputStreamReader(new FileInputStream(new File(fileName))));
        }

        public static FastReader getDefaultReader() throws FileNotFoundException {
            return new FastReader();
        }

        public FastReader(InputStreamReader inputStreamReader) {
            br = new BufferedReader(inputStreamReader);
        }

        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;
        }
    }
}


//package codeforces.D1401;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * @author muhossain
 * @since 2020-08-14
 */

public class B {

    public static void main(String[] args) {
        FastReader fs = new FastReader();

        int testCases = fs.nextInt();
        StringBuilder output = new StringBuilder();

        for (int t = 0; t < testCases; t++) {

            int x1 = fs.nextInt(); // 0
            int y1 = fs.nextInt(); // 1
            int z1 = fs.nextInt(); // 2

            int x2 = fs.nextInt(); // 0
            int y2 = fs.nextInt(); // 1
            int z2 = fs.nextInt(); // 2

            if (x1 > 0 && z2 > 0) {
                if (x1 >= z2) {
                    x1 -= z2;
                    z2 = 0;
                } else {
                    x1 = 0;
                    z2 -= x1;
                }
            }

            if (x1 > 0 && x2 > 0) {
                if (x1 >= x2) {
                    x1 -= x2;
                    x2 = 0;
                } else {
                    x1 = 0;
                    x2 -= x1;
                }
            }

            if (x1 > 0 && y2 > 0) {
                if (x1 >= y2) {
                    x1 -= y2;
                    y2 = 0;
                } else {
                    x1 = 0;
                    y2 -= x1;
                }

            }

            if (y1 > 0 && x2 > 0) {
                if (y1 >= x2) {
                    y1 -= x2;
                    x2 = 0;
                } else {
                    y1 = 0;
                    x2 -= y1;
                }
            }

            if (y1 > 0 && y2 > 0) {
                if (y1 >= y2) {
                    y1 -= y2;
                    y2 = 0;
                } else {
                    y1 = 0;
                    y2 -= y1;
                }
            }

            long total = 0;

            if (y1 > 0 && z2 > 0) {
                if (y1 >= z2) {
                    total -= (z2 * 2);

                    y1 -= z2;
                    z2 = 0;

                } else {
                    total -= (y1 * 2);

                    y1 = 0;
                    z2 -= y1;
                }
            }

            if (z1 > 0 && y2 > 0) {
                if (z1 >= y2) {
                    total += (y2 * 2);

                    z1 -= y2;
                    y2 = 0;
                } else {
                    total += (z1 * 2);

                    y2 -= z1;
                    z1 = 0;
                }
            }

            output.append(total).append("\n");
        }

        System.out.print(output);
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

        public static FastReader getFileReader(String fileName) throws FileNotFoundException {
            return new FastReader(new InputStreamReader(new FileInputStream(new File(fileName))));
        }

        public static FastReader getDefaultReader() throws FileNotFoundException {
            return new FastReader();
        }

        public FastReader(InputStreamReader inputStreamReader) {
            br = new BufferedReader(inputStreamReader);
        }

        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;
        }
    }
}


//package codeforces.D1401;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * @author muhossain
 * @since 2020-08-14
 */

public class B {

    public static void main(String[] args) {
        FastReader fs = new FastReader();

        int testCases = fs.nextInt();
        StringBuilder output = new StringBuilder();

        for (int t = 0; t < testCases; t++) {

            int x1 = fs.nextInt(); // 0
            int y1 = fs.nextInt(); // 1
            int z1 = fs.nextInt(); // 2

            int x2 = fs.nextInt(); // 0
            int y2 = fs.nextInt(); // 1
            int z2 = fs.nextInt(); // 2

            if (x1 > 0 && z2 > 0) {
                if (x1 >= z2) {
                    x1 -= z2;
                    z2 = 0;
                } else {
                    x1 = 0;
                    z2 -= x1;
                }
            }

            if (x1 > 0 && x2 > 0) {
                if (x1 >= x2) {
                    x1 -= x2;
                    x2 = 0;
                } else {
                    x1 = 0;
                    x2 -= x1;
                }
            }

            if (x1 > 0 && y2 > 0) {
                if (x1 >= y2) {
                    x1 -= y2;
                    y2 = 0;
                } else {
                    x1 = 0;
                    y2 -= x1;
                }

            }

            if (y1 > 0 && x2 > 0) {
                if (y1 >= x2) {
                    y1 -= x2;
                    x2 = 0;
                } else {
                    y1 = 0;
                    x2 -= y1;
                }
            }

            int total = 0;

            if (y1 > 0 && z2 > 0) {
                if (y1 >= z2) {
                    total -= (z2 * 2);

                    y1 -= z2;
                    z2 = 0;

                } else {
                    total -= (y1 * 2);

                    y1 = 0;
                    z2 -= y1;
                }
            }

            if (y1 > 0 && y2 > 0) {
                if (y1 >= y2) {
                    y1 -= y2;
                    y2 = 0;
                } else {
                    y1 = 0;
                    y2 -= y1;
                }
            }

            if (z1 > 0 && y2 > 0) {
                if (z1 >= y2) {
                    total += (y2 * 2);

                    z1 -= y2;
                    y2 = 0;
                } else {
                    total += (z1 * 2);

                    y2 -= z1;
                    z1 = 0;
                }
            }

            output.append(total).append("\n");
        }

        System.out.print(output);
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

        public static FastReader getFileReader(String fileName) throws FileNotFoundException {
            return new FastReader(new InputStreamReader(new FileInputStream(new File(fileName))));
        }

        public static FastReader getDefaultReader() throws FileNotFoundException {
            return new FastReader();
        }

        public FastReader(InputStreamReader inputStreamReader) {
            br = new BufferedReader(inputStreamReader);
        }

        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;
        }
    }
}


//package codeforces.D1401;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * @author muhossain
 * @since 2020-08-14
 */

public class B {

    public static void main(String[] args) {
        FastReader fs = new FastReader();

        int testCases = fs.nextInt();
        StringBuilder output = new StringBuilder();

        for (int t = 0; t < testCases; t++) {

            int x1 = fs.nextInt(); // 0
            int y1 = fs.nextInt(); // 1
            int z1 = fs.nextInt(); // 2

            int x2 = fs.nextInt(); // 0
            int y2 = fs.nextInt(); // 1
            int z2 = fs.nextInt(); // 2

            if (x1 > 0 && z2 > 0) {
                if (x1 >= z2) {
                    x1 -= z2;
                    z2 = 0;
                } else {
                    x1 = 0;
                    z2 -= x1;
                }
            }

            if (x1 > 0 && x2 > 0) {
                if (x1 >= x2) {
                    x1 -= x2;
                    x2 = 0;
                } else {
                    x1 = 0;
                    x2 -= x1;
                }
            }

            if (x1 > 0 && y2 > 0) {
                if (x1 >= y2) {
                    x1 -= y2;
                    y2 = 0;
                } else {
                    x1 = 0;
                    y2 -= x1;
                }

            }

            if (y1 > 0 && x2 > 0) {
                if (y1 >= x2) {
                    y1 -= x2;
                    x2 = 0;
                } else {
                    y1 = 0;
                    x2 -= y1;
                }
            }

            long total = 0;

            if (y1 > 0 && z2 > 0) {
                if (y1 >= z2) {
                    total -= (z2 * 2);

                    y1 -= z2;
                    z2 = 0;

                } else {
                    total -= (y1 * 2);

                    y1 = 0;
                    z2 -= y1;
                }
            }

            if (y1 > 0 && y2 > 0) {
                if (y1 >= y2) {
                    y1 -= y2;
                    y2 = 0;
                } else {
                    y1 = 0;
                    y2 -= y1;
                }
            }

            if (z1 > 0 && y2 > 0) {
                if (z1 >= y2) {
                    total += (y2 * 2);

                    z1 -= y2;
                    y2 = 0;
                } else {
                    total += (z1 * 2);

                    y2 -= z1;
                    z1 = 0;
                }
            }

            output.append(total).append("\n");
        }

        System.out.print(output);
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

        public static FastReader getFileReader(String fileName) throws FileNotFoundException {
            return new FastReader(new InputStreamReader(new FileInputStream(new File(fileName))));
        }

        public static FastReader getDefaultReader() throws FileNotFoundException {
            return new FastReader();
        }

        public FastReader(InputStreamReader inputStreamReader) {
            br = new BufferedReader(inputStreamReader);
        }

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

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

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

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

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


		 					import java.math.BigInteger;
							import java.util.ArrayList;
							import java.util.Arrays;
						import java.util.Collection;
						import java.util.Collections;
						import java.util.Comparator;
						import java.util.HashMap;
							import java.util.Iterator;
						import java.util.LinkedList;
						import java.util.List;
						import java.util.Map;
					import java.util.Map.Entry;
					import java.util.PriorityQueue;
						import java.util.Scanner;
						import java.util.Set;
						import java.util.Stack;
						import java.io.BufferedReader;
						import java.io.IOException;
						import java.io.InputStreamReader;
						import java.util.Scanner;
						import java.util.StringTokenizer;
						import java.util.TreeMap;
						import java.util.TreeSet;

							public class Main {

								static FastReader sc= new FastReader();
								static List<Integer> C;
								static List<Integer> B;
							static	int mod=(int)1e9+9;
								static long dp[];
								static boolean prime[] = new boolean[100001];
								public static void main(String[] args) {

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

									long z1=sc.nextInt();long o1=sc.nextInt();long t1=sc.nextInt();
									long z2=sc.nextInt();long o2=sc.nextInt();long t2=sc.nextInt();

									long total=2*Math.min(o2, t1);
									t1-=Math.min(o2, t1);
									o2-=Math.min(o2, t1);

									t2-=Math.min(t2,z1);
									t2-=Math.min(t2,t1);

									o1-=Math.min(o1,z2);
									o1-=Math.min(o2,o1);

									if(o1>0&&t2>0) {
										t2-=2*Math.min(o1, t2);
									}

									System.out.println(total);

										}
								}

								 static final int MAXN = 100001;

								    // stores smallest prime factor for every number
								    static int spf[] = new int[MAXN];

								    // Calculating SPF (Smallest Prime Factor) for every
								    // number till MAXN.
								    // Time Complexity : O(nloglogn)
								    static void sieve()
								    {
								        spf[1] = 1;
								        for (int i=2; i<MAXN; i++)

								            // marking smallest prime factor for every
								            // number to be itself.
								            spf[i] = i;

								        // separately marking spf for every even
								        // number as 2
								        for (int i=4; i<MAXN; i+=2)
								            spf[i] = 2;

								        for (int i=3; i*i<MAXN; i++)
								        {
								            // checking if i is prime
								            if (spf[i] == i)
								            {
								                // marking SPF for all numbers divisible by i
								                for (int j=i*i; j<MAXN; j+=i)

								                    // marking spf[j] if it is not
								                    // previously marked
								                    if (spf[j]==j)
								                        spf[j] = i;
								            }
								        }
								    }

							static	 void sieveOfEratosthenes(int n)
								    {
								        // Create a boolean array "prime[0..n]" and initialize
								        // all entries it as true. A value in prime[i] will
								        // finally be false if i is Not a prime, else true.
								        //boolean prime[] = new boolean[n+1];
								        for(int i=0;i<n;i++)
								            prime[i] = true;

								        for(int p = 2; p*p <=n; p++)
								        {
								            // If prime[p] is not changed, then it is a prime
								            if(prime[p] == true)
								            {
								                // Update all multiples of p
								                for(int i = p*p; i <= n; i += p)
								                    prime[i] = false;
								            }
								        }

								       // System.out.println(n);
								    }

								static long  solve(List<Integer> A,int n,long cost,int k,HashMap<Integer,  Integer> map) {
									//if(!map.containsKey(curr))map.put(curr,);
									//if(n==0&&curr!=0)return 1;
									if(dp[n]!=-1)return dp[n];
									if(n==0)return cost;
					//				if(dp[n]!=-1)return dp[n];
								//	if(dp[n]!=-1)return dp[n];

									if(map.containsKey(C.get(n-1))) {

										long addon=0l;
										if(map.get(C.get(n-1))==1)addon=2l;
										else addon=1l;

										map.put(C.get(n-1),map.get(C.get(n-1))+1);
										//System.out.println(cost+" "+n+" "+map);
										return dp[n]=Math.min(solve(A,n-1,cost+addon,k,map),solve(A, n-1,k+cost,k, new HashMap<Integer,Integer>(C.get(n-1),1)));

									}
									else {map.put(C.get(n-1), 1); return dp[n] =solve(A,  n-1, cost,k, map);
									}

								}

							static int exponentMod(int A,
					                       int B, int C)
					{

					    // Base cases
					    if (A == 0)
					        return 0;
					    if (B == 0)
					        return 1;

					    // If B is even
					    long y;
					    if (B % 2 == 0)
					    {
					        y = exponentMod(A, B / 2, C);
					        y = (y * y) % C;
					    }

					    // If B is odd
					    else
					    {
					        y = A % C;
					        y = (y * exponentMod(A, B - 1,
					                             C) % C) % C;
					    }

					    return (int)((y + C) % C);
					}

							static	int modulo(int a,int b,int c) {
								    long x=1;
								    long y=a;
								    while(b > 0){
								        if(b%2 == 1){
								            x=(x*y)%c;
								        }
								        y = (y*y)%c; // squaring the base
								        b /= 2;
								    }
								    return (int) x%c;
								}

								 static int power(int x, int y, int p)
								    {
								        // Initialize result
								        int res = 1;

								        // Update x if it is more
								        // than or equal to p
								        x = x % p;

								       if (x == 0) return 0; // In case x is divisible by p;

								        while (y > 0)
								        {
								            // If y is odd, multiply x
								            // with result
								            if((y & 1)==1)
								                res = (res * x) % p;

								            // y must be even now
								            // y = y / 2
								            y = y >> 1;
								            x = (x * x) % p;
								        }
								        return res;
								    }
								static int N = 100000;

							    static int n; // array size

							    // Max size of tree
							    static int []tree = new int[2 * N];

								 static void build( int []arr)
								    {

								        // insert leaf nodes in tree
								        for (int i = 0; i < n; i++)
								            tree[n + i] = arr[i];

								        // build the tree by calculating
								        // parents
								        for (int i = n - 1; i > 0; --i)
								            tree[i] = tree[i << 1] +
								                      tree[i << 1 | 1];
								    }

								    // function to update a tree node
								    static void updateTreeNode(int p, int value)
								    {

								        // set value at position p
								        tree[p + n] = value;
								        p = p + n;

								        // move upward and update parents
								        for (int i = p; i > 1; i >>= 1)
								            tree[i >> 1] = tree[i] + tree[i^1];
								    }

								    // function to get sum on
								    // interval [l, r)
								    static int query(int l, int r)
								    {
								        int res = 0;

								        // loop to find the sum in the range
								        for (l += n, r += n; l < r;
								                             l >>= 1, r >>= 1)
								        {
								            if ((l & 1) > 0)
								                res += tree[l++];

								            if ((r & 1) > 0)
								                res += tree[--r];
								        }

								        return res;
								    }

								static int a=1;

							static class decimal{
								int n;double d;
								public decimal(int n,double d) {

									this.n=n;
									this.d=d;
									// TODO Auto-generated constructor stub
								}
							}

							static	boolean isPossible(List<Integer> C,int m,int k) {
									if(m==0)return false;
									for(int i=0;i<C.size();i++) {

										if(C.get(i)>m) {
											k-=C.get(i)/m;
										}
										if(k<0)return false;
									}
										return true;

								}

								static boolean isSubsetSum(int set[],  int n, int sum)
						{
						// The value of subset[i][j] will be
						// true if there is a subset of
						// set[0..j-1] with sum equal to i
						boolean subset[][] = new boolean[sum + 1][n + 1];

						// If sum is 0, then answer is true
						for (int i = 0; i <= n; i++)
						subset[0][i] = true;

						// If sum is not 0 and set is empty,
						// then answer is false
						for (int i = 1; i <= sum; i++)
						subset[i][0] = false;

						// Fill the subset table in botton
						// up manner
						for (int i = 1; i <= sum; i++) {
						for (int j = 1; j <= n; j++) {
						    subset[i][j] = subset[i][j - 1];
						    if (i >= set[j - 1])
						        subset[i][j] = subset[i][j]
						                       || subset[i - set[j - 1]][j - 1];
						}
						}

						return subset[sum][n];
						}

								static boolean search(ArrayList<Integer> a,int m,int h) {
									PriorityQueue<Integer> p=new PriorityQueue<Integer>(new Comparator<Integer>() {
											@Override
											public int compare(Integer o1, Integer o2) {
												// TODO Auto-generated method stub
												return o2-o1;
											}
									});

									for(int i=0;i<m;i++) {
										p.add(a.get(i));
									}
									int c=0;
									//System.out.println(p);
									while(!p.isEmpty()) {
										//System.out.println(p);

										h-=p.poll();
										if(h<0)break;

										c++;

									//	System.out.println(h+" "+p+" "+c+" "+m);

										if(!p.isEmpty()) {
										p.poll();
										c++;}

									}
								//	System.out.println(c!=m);
									if(c!=m)return false;

									return true;
								}
							//	static int  key=-1;

								static boolean isPowerOfTwo(long n)
							    {
							        if (n == 0)
							            return false;

							        while (n != 1)
							        {
							            if (n % 2 != 0)
							                return false;
							            n = n / 2;
							        }
							        return true;
							    }

								 static boolean isPerfectSquare(double x)
								    {

								        // Find floating point value of
								        // square root of x.
								        double sr = Math.sqrt(x);

								        // If square root is an integer
								        return ((sr - Math.floor(sr)) == 0);
								    }

							static	int binarySearch(ArrayList<Long> arr, int l, int r, long x,int key)
							    {
							        if (r >= l) {
							            int mid =  (r + l) / 2;
							//  if(mid>=arr.size())return -1;
							            // If the element is present at the
							            // middle itself

							            if (arr.get(mid) == x)
							                return mid+1;

							            // If element is smaller than mid, then
							            // it can only be present in left subarray
							            if (arr.get(mid) > x) {
							                return binarySearch(arr, l, mid - 1, x,key);
							            }
							            // Else the element can only be present
							            // in right subarray
							            key=mid+1;
							            return binarySearch(arr, mid + 1, r, x,key);
							        }

							        // We reach here when element is not present
							        // in array
							        return key;
							    }

							    static boolean isPrime(long n)
							    {
							         // Corner cases
							        if (n <= 1)
							            return false;
							        if (n <= 3)
							            return true;

							        // This is checked so that we can skip
							        // middle five numbers in below loop
							        if (n % 2 == 0 || n % 3 == 0)
							            return false;

							        for (int i = 5; i * i <= n; i = i + 6)
							            if (n % i == 0 || n % (i + 2) == 0)
							                return false;

							        return true;
							    }

							    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;
							        }
							    }

							}

							class Sol {

								int value;int in;int i;
								Sol(int value,int in,int i){
									this.value=value;
									this.in=in;
									this.i=i;
								}

						}

					 class SegmentTree {

						SegNode st[];
						int n;
					//	long arr[];
						public SegmentTree(int n) {
								this.n=n;
							//this.arr=arr;
							st=new SegNode[4*n];

						}

						void init(long n) {

							for(int i=0;i<4*n;i++) {
								st[i]=new SegNode();
							}

						}

						void set(int i,long v,int x,int lx,int rx) {

							if(rx-lx==1) {
								st[x].sum=v;
								st[x].max=v;
								return ;
							}
							int m=lx+(rx-lx)/2;
							if(i<m) set(i,v,2*x+1,lx,m);
							else set(i,v,2*x+2,m,rx);

							st[x].sum=st[2*x+1].sum+st[2*x+2].sum;
							long ma=Math.max(st[2*x+1].max,st[2*x+2].max);
								st[x].max=Math.max(ma,st[x].sum);
						}

						void set (int i,long v) {
							set(i,v,0,0,n);
						}

						long max=Integer.MIN_VALUE;
					long sum(int l,int r,int x,int lx,int rx) {
						if(lx>=r||l>=rx)return 0;
						if(lx>=l||rx<=r)return st[x].sum;

							int m=(rx+lx)/2;

							long s1=sum(l,r,2*x+1,lx,m);
							long s2=sum(l,r,2*x+2,m,rx);

							return max= Math.max(Math.max(Math.max(s1,s2),s1+s2),max);

						}
						long sum(int l,int r) {
							return sum(l,r,0,0,n);
						}

					}
						class SegNode{
							long max;long sum;
							public SegNode() {
								// TODO Auto-generated constructor stub
								sum=0;
								max=Integer.MIN_VALUE;

							}
						}


		 					import java.math.BigInteger;
							import java.util.ArrayList;
							import java.util.Arrays;
						import java.util.Collection;
						import java.util.Collections;
						import java.util.Comparator;
						import java.util.HashMap;
							import java.util.Iterator;
						import java.util.LinkedList;
						import java.util.List;
						import java.util.Map;
					import java.util.Map.Entry;
					import java.util.PriorityQueue;
						import java.util.Scanner;
						import java.util.Set;
						import java.util.Stack;
						import java.io.BufferedReader;
						import java.io.IOException;
						import java.io.InputStreamReader;
						import java.util.Scanner;
						import java.util.StringTokenizer;
						import java.util.TreeMap;
						import java.util.TreeSet;

							public class Main {

								static FastReader sc= new FastReader();
								static List<Integer> C;
								static List<Integer> B;
							static	int mod=(int)1e9+9;
								static long dp[];
								static boolean prime[] = new boolean[100001];
								public static void main(String[] args) {

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

									long z1=sc.nextInt();long o1=sc.nextInt();long t1=sc.nextInt();
									long z2=sc.nextInt();long o2=sc.nextInt();long t2=sc.nextInt();
									System.out.println(2*Math.min(o2, t1));

										}
								}

								 static final int MAXN = 100001;

								    // stores smallest prime factor for every number
								    static int spf[] = new int[MAXN];

								    // Calculating SPF (Smallest Prime Factor) for every
								    // number till MAXN.
								    // Time Complexity : O(nloglogn)
								    static void sieve()
								    {
								        spf[1] = 1;
								        for (int i=2; i<MAXN; i++)

								            // marking smallest prime factor for every
								            // number to be itself.
								            spf[i] = i;

								        // separately marking spf for every even
								        // number as 2
								        for (int i=4; i<MAXN; i+=2)
								            spf[i] = 2;

								        for (int i=3; i*i<MAXN; i++)
								        {
								            // checking if i is prime
								            if (spf[i] == i)
								            {
								                // marking SPF for all numbers divisible by i
								                for (int j=i*i; j<MAXN; j+=i)

								                    // marking spf[j] if it is not
								                    // previously marked
								                    if (spf[j]==j)
								                        spf[j] = i;
								            }
								        }
								    }

							static	 void sieveOfEratosthenes(int n)
								    {
								        // Create a boolean array "prime[0..n]" and initialize
								        // all entries it as true. A value in prime[i] will
								        // finally be false if i is Not a prime, else true.
								        //boolean prime[] = new boolean[n+1];
								        for(int i=0;i<n;i++)
								            prime[i] = true;

								        for(int p = 2; p*p <=n; p++)
								        {
								            // If prime[p] is not changed, then it is a prime
								            if(prime[p] == true)
								            {
								                // Update all multiples of p
								                for(int i = p*p; i <= n; i += p)
								                    prime[i] = false;
								            }
								        }

								       // System.out.println(n);
								    }

								static long  solve(List<Integer> A,int n,long cost,int k,HashMap<Integer,  Integer> map) {
									//if(!map.containsKey(curr))map.put(curr,);
									//if(n==0&&curr!=0)return 1;
									if(dp[n]!=-1)return dp[n];
									if(n==0)return cost;
					//				if(dp[n]!=-1)return dp[n];
								//	if(dp[n]!=-1)return dp[n];

									if(map.containsKey(C.get(n-1))) {

										long addon=0l;
										if(map.get(C.get(n-1))==1)addon=2l;
										else addon=1l;

										map.put(C.get(n-1),map.get(C.get(n-1))+1);
										//System.out.println(cost+" "+n+" "+map);
										return dp[n]=Math.min(solve(A,n-1,cost+addon,k,map),solve(A, n-1,k+cost,k, new HashMap<Integer,Integer>(C.get(n-1),1)));

									}
									else {map.put(C.get(n-1), 1); return dp[n] =solve(A,  n-1, cost,k, map);
									}

								}

							static int exponentMod(int A,
					                       int B, int C)
					{

					    // Base cases
					    if (A == 0)
					        return 0;
					    if (B == 0)
					        return 1;

					    // If B is even
					    long y;
					    if (B % 2 == 0)
					    {
					        y = exponentMod(A, B / 2, C);
					        y = (y * y) % C;
					    }

					    // If B is odd
					    else
					    {
					        y = A % C;
					        y = (y * exponentMod(A, B - 1,
					                             C) % C) % C;
					    }

					    return (int)((y + C) % C);
					}

							static	int modulo(int a,int b,int c) {
								    long x=1;
								    long y=a;
								    while(b > 0){
								        if(b%2 == 1){
								            x=(x*y)%c;
								        }
								        y = (y*y)%c; // squaring the base
								        b /= 2;
								    }
								    return (int) x%c;
								}

								 static int power(int x, int y, int p)
								    {
								        // Initialize result
								        int res = 1;

								        // Update x if it is more
								        // than or equal to p
								        x = x % p;

								       if (x == 0) return 0; // In case x is divisible by p;

								        while (y > 0)
								        {
								            // If y is odd, multiply x
								            // with result
								            if((y & 1)==1)
								                res = (res * x) % p;

								            // y must be even now
								            // y = y / 2
								            y = y >> 1;
								            x = (x * x) % p;
								        }
								        return res;
								    }
								static int N = 100000;

							    static int n; // array size

							    // Max size of tree
							    static int []tree = new int[2 * N];

								 static void build( int []arr)
								    {

								        // insert leaf nodes in tree
								        for (int i = 0; i < n; i++)
								            tree[n + i] = arr[i];

								        // build the tree by calculating
								        // parents
								        for (int i = n - 1; i > 0; --i)
								            tree[i] = tree[i << 1] +
								                      tree[i << 1 | 1];
								    }

								    // function to update a tree node
								    static void updateTreeNode(int p, int value)
								    {

								        // set value at position p
								        tree[p + n] = value;
								        p = p + n;

								        // move upward and update parents
								        for (int i = p; i > 1; i >>= 1)
								            tree[i >> 1] = tree[i] + tree[i^1];
								    }

								    // function to get sum on
								    // interval [l, r)
								    static int query(int l, int r)
								    {
								        int res = 0;

								        // loop to find the sum in the range
								        for (l += n, r += n; l < r;
								                             l >>= 1, r >>= 1)
								        {
								            if ((l & 1) > 0)
								                res += tree[l++];

								            if ((r & 1) > 0)
								                res += tree[--r];
								        }

								        return res;
								    }

								static int a=1;

							static class decimal{
								int n;double d;
								public decimal(int n,double d) {

									this.n=n;
									this.d=d;
									// TODO Auto-generated constructor stub
								}
							}

							static	boolean isPossible(List<Integer> C,int m,int k) {
									if(m==0)return false;
									for(int i=0;i<C.size();i++) {

										if(C.get(i)>m) {
											k-=C.get(i)/m;
										}
										if(k<0)return false;
									}
										return true;

								}

								static boolean isSubsetSum(int set[],  int n, int sum)
						{
						// The value of subset[i][j] will be
						// true if there is a subset of
						// set[0..j-1] with sum equal to i
						boolean subset[][] = new boolean[sum + 1][n + 1];

						// If sum is 0, then answer is true
						for (int i = 0; i <= n; i++)
						subset[0][i] = true;

						// If sum is not 0 and set is empty,
						// then answer is false
						for (int i = 1; i <= sum; i++)
						subset[i][0] = false;

						// Fill the subset table in botton
						// up manner
						for (int i = 1; i <= sum; i++) {
						for (int j = 1; j <= n; j++) {
						    subset[i][j] = subset[i][j - 1];
						    if (i >= set[j - 1])
						        subset[i][j] = subset[i][j]
						                       || subset[i - set[j - 1]][j - 1];
						}
						}

						return subset[sum][n];
						}

								static boolean search(ArrayList<Integer> a,int m,int h) {
									PriorityQueue<Integer> p=new PriorityQueue<Integer>(new Comparator<Integer>() {
											@Override
											public int compare(Integer o1, Integer o2) {
												// TODO Auto-generated method stub
												return o2-o1;
											}
									});

									for(int i=0;i<m;i++) {
										p.add(a.get(i));
									}
									int c=0;
									//System.out.println(p);
									while(!p.isEmpty()) {
										//System.out.println(p);

										h-=p.poll();
										if(h<0)break;

										c++;

									//	System.out.println(h+" "+p+" "+c+" "+m);

										if(!p.isEmpty()) {
										p.poll();
										c++;}

									}
								//	System.out.println(c!=m);
									if(c!=m)return false;

									return true;
								}
							//	static int  key=-1;

								static boolean isPowerOfTwo(long n)
							    {
							        if (n == 0)
							            return false;

							        while (n != 1)
							        {
							            if (n % 2 != 0)
							                return false;
							            n = n / 2;
							        }
							        return true;
							    }

								 static boolean isPerfectSquare(double x)
								    {

								        // Find floating point value of
								        // square root of x.
								        double sr = Math.sqrt(x);

								        // If square root is an integer
								        return ((sr - Math.floor(sr)) == 0);
								    }

							static	int binarySearch(ArrayList<Long> arr, int l, int r, long x,int key)
							    {
							        if (r >= l) {
							            int mid =  (r + l) / 2;
							//  if(mid>=arr.size())return -1;
							            // If the element is present at the
							            // middle itself

							            if (arr.get(mid) == x)
							                return mid+1;

							            // If element is smaller than mid, then
							            // it can only be present in left subarray
							            if (arr.get(mid) > x) {
							                return binarySearch(arr, l, mid - 1, x,key);
							            }
							            // Else the element can only be present
							            // in right subarray
							            key=mid+1;
							            return binarySearch(arr, mid + 1, r, x,key);
							        }

							        // We reach here when element is not present
							        // in array
							        return key;
							    }

							    static boolean isPrime(long n)
							    {
							         // Corner cases
							        if (n <= 1)
							            return false;
							        if (n <= 3)
							            return true;

							        // This is checked so that we can skip
							        // middle five numbers in below loop
							        if (n % 2 == 0 || n % 3 == 0)
							            return false;

							        for (int i = 5; i * i <= n; i = i + 6)
							            if (n % i == 0 || n % (i + 2) == 0)
							                return false;

							        return true;
							    }

							    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;
							        }
							    }

							}

							class Sol {

								int value;int in;int i;
								Sol(int value,int in,int i){
									this.value=value;
									this.in=in;
									this.i=i;
								}

						}

					 class SegmentTree {

						SegNode st[];
						int n;
					//	long arr[];
						public SegmentTree(int n) {
								this.n=n;
							//this.arr=arr;
							st=new SegNode[4*n];

						}

						void init(long n) {

							for(int i=0;i<4*n;i++) {
								st[i]=new SegNode();
							}

						}

						void set(int i,long v,int x,int lx,int rx) {

							if(rx-lx==1) {
								st[x].sum=v;
								st[x].max=v;
								return ;
							}
							int m=lx+(rx-lx)/2;
							if(i<m) set(i,v,2*x+1,lx,m);
							else set(i,v,2*x+2,m,rx);

							st[x].sum=st[2*x+1].sum+st[2*x+2].sum;
							long ma=Math.max(st[2*x+1].max,st[2*x+2].max);
								st[x].max=Math.max(ma,st[x].sum);
						}

						void set (int i,long v) {
							set(i,v,0,0,n);
						}

						long max=Integer.MIN_VALUE;
					long sum(int l,int r,int x,int lx,int rx) {
						if(lx>=r||l>=rx)return 0;
						if(lx>=l||rx<=r)return st[x].sum;

							int m=(rx+lx)/2;

							long s1=sum(l,r,2*x+1,lx,m);
							long s2=sum(l,r,2*x+2,m,rx);

							return max= Math.max(Math.max(Math.max(s1,s2),s1+s2),max);

						}
						long sum(int l,int r) {
							return sum(l,r,0,0,n);
						}

					}
						class SegNode{
							long max;long sum;
							public SegNode() {
								// TODO Auto-generated constructor stub
								sum=0;
								max=Integer.MIN_VALUE;

							}
						}


		 					import java.math.BigInteger;
							import java.util.ArrayList;
							import java.util.Arrays;
						import java.util.Collection;
						import java.util.Collections;
						import java.util.Comparator;
						import java.util.HashMap;
							import java.util.Iterator;
						import java.util.LinkedList;
						import java.util.List;
						import java.util.Map;
					import java.util.Map.Entry;
					import java.util.PriorityQueue;
						import java.util.Scanner;
						import java.util.Set;
						import java.util.Stack;
						import java.io.BufferedReader;
						import java.io.IOException;
						import java.io.InputStreamReader;
						import java.util.Scanner;
						import java.util.StringTokenizer;
						import java.util.TreeMap;
						import java.util.TreeSet;

							public class Main {

								static FastReader sc= new FastReader();
								static List<Integer> C;
								static List<Integer> B;
							static	int mod=(int)1e9+9;
								static long dp[];
								static boolean prime[] = new boolean[100001];
								public static void main(String[] args) {

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

									long z1=sc.nextInt();long o1=sc.nextInt();long t1=sc.nextInt();
									long z2=sc.nextInt();long o2=sc.nextInt();long t2=sc.nextInt();

									long total=2*Math.min(o2, t1);
									t1-=Math.min(o2, t1);
									o2-=Math.min(o2, t1);
									if(t1>0&&t2>0) {
										t1-=Math.min(t2, t1);
										t2-=Math.min(t2, t1);

									}
									if(o1>0&&o2>0) {
										o1-=Math.min(o2, o1);
										o2-=Math.min(o2, o1);

									}
									if(z1>0&&z2>0) {
										z1-=Math.min(z2, z1);
										z2-=Math.min(z2, z1);

									}
									if(t1>0&&z2>0) {
										t1-=Math.min(z2, t1);
										z2-=Math.min(z2, t1);

									}
									if(o1>0&&z2>0) {
										o1-=Math.min(z2, o1);
										z2-=Math.min(z2, o1);

									}
									if(o2>0&&z1>0) {
										o2-=Math.min(z1, o2);
										z1-=Math.min(z1, o2);

									}
									if(t2>0&&z1>0) {
										t2-=Math.min(z1, t2);
										z1-=Math.min(z1, t2);

									}

									if(t2>0&&o1>0) {

										total-=2*Math.min(o1, t2);
									}

									System.out.println(total);

										}
								}

								 static final int MAXN = 100001;

								    // stores smallest prime factor for every number
								    static int spf[] = new int[MAXN];

								    // Calculating SPF (Smallest Prime Factor) for every
								    // number till MAXN.
								    // Time Complexity : O(nloglogn)
								    static void sieve()
								    {
								        spf[1] = 1;
								        for (int i=2; i<MAXN; i++)

								            // marking smallest prime factor for every
								            // number to be itself.
								            spf[i] = i;

								        // separately marking spf for every even
								        // number as 2
								        for (int i=4; i<MAXN; i+=2)
								            spf[i] = 2;

								        for (int i=3; i*i<MAXN; i++)
								        {
								            // checking if i is prime
								            if (spf[i] == i)
								            {
								                // marking SPF for all numbers divisible by i
								                for (int j=i*i; j<MAXN; j+=i)

								                    // marking spf[j] if it is not
								                    // previously marked
								                    if (spf[j]==j)
								                        spf[j] = i;
								            }
								        }
								    }

							static	 void sieveOfEratosthenes(int n)
								    {
								        // Create a boolean array "prime[0..n]" and initialize
								        // all entries it as true. A value in prime[i] will
								        // finally be false if i is Not a prime, else true.
								        //boolean prime[] = new boolean[n+1];
								        for(int i=0;i<n;i++)
								            prime[i] = true;

								        for(int p = 2; p*p <=n; p++)
								        {
								            // If prime[p] is not changed, then it is a prime
								            if(prime[p] == true)
								            {
								                // Update all multiples of p
								                for(int i = p*p; i <= n; i += p)
								                    prime[i] = false;
								            }
								        }

								       // System.out.println(n);
								    }

								static long  solve(List<Integer> A,int n,long cost,int k,HashMap<Integer,  Integer> map) {
									//if(!map.containsKey(curr))map.put(curr,);
									//if(n==0&&curr!=0)return 1;
									if(dp[n]!=-1)return dp[n];
									if(n==0)return cost;
					//				if(dp[n]!=-1)return dp[n];
								//	if(dp[n]!=-1)return dp[n];

									if(map.containsKey(C.get(n-1))) {

										long addon=0l;
										if(map.get(C.get(n-1))==1)addon=2l;
										else addon=1l;

										map.put(C.get(n-1),map.get(C.get(n-1))+1);
										//System.out.println(cost+" "+n+" "+map);
										return dp[n]=Math.min(solve(A,n-1,cost+addon,k,map),solve(A, n-1,k+cost,k, new HashMap<Integer,Integer>(C.get(n-1),1)));

									}
									else {map.put(C.get(n-1), 1); return dp[n] =solve(A,  n-1, cost,k, map);
									}

								}

							static int exponentMod(int A,
					                       int B, int C)
					{

					    // Base cases
					    if (A == 0)
					        return 0;
					    if (B == 0)
					        return 1;

					    // If B is even
					    long y;
					    if (B % 2 == 0)
					    {
					        y = exponentMod(A, B / 2, C);
					        y = (y * y) % C;
					    }

					    // If B is odd
					    else
					    {
					        y = A % C;
					        y = (y * exponentMod(A, B - 1,
					                             C) % C) % C;
					    }

					    return (int)((y + C) % C);
					}

							static	int modulo(int a,int b,int c) {
								    long x=1;
								    long y=a;
								    while(b > 0){
								        if(b%2 == 1){
								            x=(x*y)%c;
								        }
								        y = (y*y)%c; // squaring the base
								        b /= 2;
								    }
								    return (int) x%c;
								}

								 static int power(int x, int y, int p)
								    {
								        // Initialize result
								        int res = 1;

								        // Update x if it is more
								        // than or equal to p
								        x = x % p;

								       if (x == 0) return 0; // In case x is divisible by p;

								        while (y > 0)
								        {
								            // If y is odd, multiply x
								            // with result
								            if((y & 1)==1)
								                res = (res * x) % p;

								            // y must be even now
								            // y = y / 2
								            y = y >> 1;
								            x = (x * x) % p;
								        }
								        return res;
								    }
								static int N = 100000;

							    static int n; // array size

							    // Max size of tree
							    static int []tree = new int[2 * N];

								 static void build( int []arr)
								    {

								        // insert leaf nodes in tree
								        for (int i = 0; i < n; i++)
								            tree[n + i] = arr[i];

								        // build the tree by calculating
								        // parents
								        for (int i = n - 1; i > 0; --i)
								            tree[i] = tree[i << 1] +
								                      tree[i << 1 | 1];
								    }

								    // function to update a tree node
								    static void updateTreeNode(int p, int value)
								    {

								        // set value at position p
								        tree[p + n] = value;
								        p = p + n;

								        // move upward and update parents
								        for (int i = p; i > 1; i >>= 1)
								            tree[i >> 1] = tree[i] + tree[i^1];
								    }

								    // function to get sum on
								    // interval [l, r)
								    static int query(int l, int r)
								    {
								        int res = 0;

								        // loop to find the sum in the range
								        for (l += n, r += n; l < r;
								                             l >>= 1, r >>= 1)
								        {
								            if ((l & 1) > 0)
								                res += tree[l++];

								            if ((r & 1) > 0)
								                res += tree[--r];
								        }

								        return res;
								    }

								static int a=1;

							static class decimal{
								int n;double d;
								public decimal(int n,double d) {

									this.n=n;
									this.d=d;
									// TODO Auto-generated constructor stub
								}
							}

							static	boolean isPossible(List<Integer> C,int m,int k) {
									if(m==0)return false;
									for(int i=0;i<C.size();i++) {

										if(C.get(i)>m) {
											k-=C.get(i)/m;
										}
										if(k<0)return false;
									}
										return true;

								}

								static boolean isSubsetSum(int set[],  int n, int sum)
						{
						// The value of subset[i][j] will be
						// true if there is a subset of
						// set[0..j-1] with sum equal to i
						boolean subset[][] = new boolean[sum + 1][n + 1];

						// If sum is 0, then answer is true
						for (int i = 0; i <= n; i++)
						subset[0][i] = true;

						// If sum is not 0 and set is empty,
						// then answer is false
						for (int i = 1; i <= sum; i++)
						subset[i][0] = false;

						// Fill the subset table in botton
						// up manner
						for (int i = 1; i <= sum; i++) {
						for (int j = 1; j <= n; j++) {
						    subset[i][j] = subset[i][j - 1];
						    if (i >= set[j - 1])
						        subset[i][j] = subset[i][j]
						                       || subset[i - set[j - 1]][j - 1];
						}
						}

						return subset[sum][n];
						}

								static boolean search(ArrayList<Integer> a,int m,int h) {
									PriorityQueue<Integer> p=new PriorityQueue<Integer>(new Comparator<Integer>() {
											@Override
											public int compare(Integer o1, Integer o2) {
												// TODO Auto-generated method stub
												return o2-o1;
											}
									});

									for(int i=0;i<m;i++) {
										p.add(a.get(i));
									}
									int c=0;
									//System.out.println(p);
									while(!p.isEmpty()) {
										//System.out.println(p);

										h-=p.poll();
										if(h<0)break;

										c++;

									//	System.out.println(h+" "+p+" "+c+" "+m);

										if(!p.isEmpty()) {
										p.poll();
										c++;}

									}
								//	System.out.println(c!=m);
									if(c!=m)return false;

									return true;
								}
							//	static int  key=-1;

								static boolean isPowerOfTwo(long n)
							    {
							        if (n == 0)
							            return false;

							        while (n != 1)
							        {
							            if (n % 2 != 0)
							                return false;
							            n = n / 2;
							        }
							        return true;
							    }

								 static boolean isPerfectSquare(double x)
								    {

								        // Find floating point value of
								        // square root of x.
								        double sr = Math.sqrt(x);

								        // If square root is an integer
								        return ((sr - Math.floor(sr)) == 0);
								    }

							static	int binarySearch(ArrayList<Long> arr, int l, int r, long x,int key)
							    {
							        if (r >= l) {
							            int mid =  (r + l) / 2;
							//  if(mid>=arr.size())return -1;
							            // If the element is present at the
							            // middle itself

							            if (arr.get(mid) == x)
							                return mid+1;

							            // If element is smaller than mid, then
							            // it can only be present in left subarray
							            if (arr.get(mid) > x) {
							                return binarySearch(arr, l, mid - 1, x,key);
							            }
							            // Else the element can only be present
							            // in right subarray
							            key=mid+1;
							            return binarySearch(arr, mid + 1, r, x,key);
							        }

							        // We reach here when element is not present
							        // in array
							        return key;
							    }

							    static boolean isPrime(long n)
							    {
							         // Corner cases
							        if (n <= 1)
							            return false;
							        if (n <= 3)
							            return true;

							        // This is checked so that we can skip
							        // middle five numbers in below loop
							        if (n % 2 == 0 || n % 3 == 0)
							            return false;

							        for (int i = 5; i * i <= n; i = i + 6)
							            if (n % i == 0 || n % (i + 2) == 0)
							                return false;

							        return true;
							    }

							    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;
							        }
							    }

							}

							class Sol {

								int value;int in;int i;
								Sol(int value,int in,int i){
									this.value=value;
									this.in=in;
									this.i=i;
								}

						}

					 class SegmentTree {

						SegNode st[];
						int n;
					//	long arr[];
						public SegmentTree(int n) {
								this.n=n;
							//this.arr=arr;
							st=new SegNode[4*n];

						}

						void init(long n) {

							for(int i=0;i<4*n;i++) {
								st[i]=new SegNode();
							}

						}

						void set(int i,long v,int x,int lx,int rx) {

							if(rx-lx==1) {
								st[x].sum=v;
								st[x].max=v;
								return ;
							}
							int m=lx+(rx-lx)/2;
							if(i<m) set(i,v,2*x+1,lx,m);
							else set(i,v,2*x+2,m,rx);

							st[x].sum=st[2*x+1].sum+st[2*x+2].sum;
							long ma=Math.max(st[2*x+1].max,st[2*x+2].max);
								st[x].max=Math.max(ma,st[x].sum);
						}

						void set (int i,long v) {
							set(i,v,0,0,n);
						}

						long max=Integer.MIN_VALUE;
					long sum(int l,int r,int x,int lx,int rx) {
						if(lx>=r||l>=rx)return 0;
						if(lx>=l||rx<=r)return st[x].sum;

							int m=(rx+lx)/2;

							long s1=sum(l,r,2*x+1,lx,m);
							long s2=sum(l,r,2*x+2,m,rx);

							return max= Math.max(Math.max(Math.max(s1,s2),s1+s2),max);

						}
						long sum(int l,int r) {
							return sum(l,r,0,0,n);
						}

					}
						class SegNode{
							long max;long sum;
							public SegNode() {
								// TODO Auto-generated constructor stub
								sum=0;
								max=Integer.MIN_VALUE;

							}
						}


		 					import java.math.BigInteger;
							import java.util.ArrayList;
							import java.util.Arrays;
						import java.util.Collection;
						import java.util.Collections;
						import java.util.Comparator;
						import java.util.HashMap;
							import java.util.Iterator;
						import java.util.LinkedList;
						import java.util.List;
						import java.util.Map;
					import java.util.Map.Entry;
					import java.util.PriorityQueue;
						import java.util.Scanner;
						import java.util.Set;
						import java.util.Stack;
						import java.io.BufferedReader;
						import java.io.IOException;
						import java.io.InputStreamReader;
						import java.util.Scanner;
						import java.util.StringTokenizer;
						import java.util.TreeMap;
						import java.util.TreeSet;

							public class Main {

								static FastReader sc= new FastReader();
								static List<Integer> C;
								static List<Integer> B;
							static	int mod=(int)1e9+9;
								static long dp[];
								static boolean prime[] = new boolean[100001];
								public static void main(String[] args) {

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

									long z1=sc.nextInt();long o1=sc.nextInt();long t1=sc.nextInt();
									long z2=sc.nextInt();long o2=sc.nextInt();long t2=sc.nextInt();

									long total=2*Math.min(o2, t1);
									long f=Math.min(o2, t1);
									o2-=f;
									t1-=f;
									if(t1>0&&t2>0) {
										long j=t1;
										t1-=Math.min(t2, t1);
										t2-=Math.min(t2, j);

									}
							//	System.out.println(o2+" "+o1+" "+t2);

									if(o1>0&&o2>0) {
										long j=o1;

										o1-=Math.min(o2, o1);
										o2-=Math.min(o2, j);

									}
								//	System.out.println(o1+" "+t2);

									if(o1>0&&z2>0) {
										long j=o1;

										o1-=Math.min(z2, o1);
										z2-=Math.min(z2, j);

									}
									//System.out.println(o1+" "+t2);

									if(t2>0&&z1>0) {
										long j=t2;

										t2-=Math.min(z1, t2);
										z1-=Math.min(z1, j);

									}

								//	System.out.println(o1+" "+t2);
									if(t2>0&&o1>0) {

										total-=2*Math.min(o1, t2);
									}

									System.out.println(total);

										}
								}

								 static final int MAXN = 100001;

								    // stores smallest prime factor for every number
								    static int spf[] = new int[MAXN];

								    // Calculating SPF (Smallest Prime Factor) for every
								    // number till MAXN.
								    // Time Complexity : O(nloglogn)
								    static void sieve()
								    {
								        spf[1] = 1;
								        for (int i=2; i<MAXN; i++)

								            // marking smallest prime factor for every
								            // number to be itself.
								            spf[i] = i;

								        // separately marking spf for every even
								        // number as 2
								        for (int i=4; i<MAXN; i+=2)
								            spf[i] = 2;

								        for (int i=3; i*i<MAXN; i++)
								        {
								            // checking if i is prime
								            if (spf[i] == i)
								            {
								                // marking SPF for all numbers divisible by i
								                for (int j=i*i; j<MAXN; j+=i)

								                    // marking spf[j] if it is not
								                    // previously marked
								                    if (spf[j]==j)
								                        spf[j] = i;
								            }
								        }
								    }

							static	 void sieveOfEratosthenes(int n)
								    {
								        // Create a boolean array "prime[0..n]" and initialize
								        // all entries it as true. A value in prime[i] will
								        // finally be false if i is Not a prime, else true.
								        //boolean prime[] = new boolean[n+1];
								        for(int i=0;i<n;i++)
								            prime[i] = true;

								        for(int p = 2; p*p <=n; p++)
								        {
								            // If prime[p] is not changed, then it is a prime
								            if(prime[p] == true)
								            {
								                // Update all multiples of p
								                for(int i = p*p; i <= n; i += p)
								                    prime[i] = false;
								            }
								        }

								       // System.out.println(n);
								    }

								static long  solve(List<Integer> A,int n,long cost,int k,HashMap<Integer,  Integer> map) {
									//if(!map.containsKey(curr))map.put(curr,);
									//if(n==0&&curr!=0)return 1;
									if(dp[n]!=-1)return dp[n];
									if(n==0)return cost;
					//				if(dp[n]!=-1)return dp[n];
								//	if(dp[n]!=-1)return dp[n];

									if(map.containsKey(C.get(n-1))) {

										long addon=0l;
										if(map.get(C.get(n-1))==1)addon=2l;
										else addon=1l;

										map.put(C.get(n-1),map.get(C.get(n-1))+1);
										//System.out.println(cost+" "+n+" "+map);
										return dp[n]=Math.min(solve(A,n-1,cost+addon,k,map),solve(A, n-1,k+cost,k, new HashMap<Integer,Integer>(C.get(n-1),1)));

									}
									else {map.put(C.get(n-1), 1); return dp[n] =solve(A,  n-1, cost,k, map);
									}

								}

							static int exponentMod(int A,
					                       int B, int C)
					{

					    // Base cases
					    if (A == 0)
					        return 0;
					    if (B == 0)
					        return 1;

					    // If B is even
					    long y;
					    if (B % 2 == 0)
					    {
					        y = exponentMod(A, B / 2, C);
					        y = (y * y) % C;
					    }

					    // If B is odd
					    else
					    {
					        y = A % C;
					        y = (y * exponentMod(A, B - 1,
					                             C) % C) % C;
					    }

					    return (int)((y + C) % C);
					}

							static	int modulo(int a,int b,int c) {
								    long x=1;
								    long y=a;
								    while(b > 0){
								        if(b%2 == 1){
								            x=(x*y)%c;
								        }
								        y = (y*y)%c; // squaring the base
								        b /= 2;
								    }
								    return (int) x%c;
								}

								 static int power(int x, int y, int p)
								    {
								        // Initialize result
								        int res = 1;

								        // Update x if it is more
								        // than or equal to p
								        x = x % p;

								       if (x == 0) return 0; // In case x is divisible by p;

								        while (y > 0)
								        {
								            // If y is odd, multiply x
								            // with result
								            if((y & 1)==1)
								                res = (res * x) % p;

								            // y must be even now
								            // y = y / 2
								            y = y >> 1;
								            x = (x * x) % p;
								        }
								        return res;
								    }
								static int N = 100000;

							    static int n; // array size

							    // Max size of tree
							    static int []tree = new int[2 * N];

								 static void build( int []arr)
								    {

								        // insert leaf nodes in tree
								        for (int i = 0; i < n; i++)
								            tree[n + i] = arr[i];

								        // build the tree by calculating
								        // parents
								        for (int i = n - 1; i > 0; --i)
								            tree[i] = tree[i << 1] +
								                      tree[i << 1 | 1];
								    }

								    // function to update a tree node
								    static void updateTreeNode(int p, int value)
								    {

								        // set value at position p
								        tree[p + n] = value;
								        p = p + n;

								        // move upward and update parents
								        for (int i = p; i > 1; i >>= 1)
								            tree[i >> 1] = tree[i] + tree[i^1];
								    }

								    // function to get sum on
								    // interval [l, r)
								    static int query(int l, int r)
								    {
								        int res = 0;

								        // loop to find the sum in the range
								        for (l += n, r += n; l < r;
								                             l >>= 1, r >>= 1)
								        {
								            if ((l & 1) > 0)
								                res += tree[l++];

								            if ((r & 1) > 0)
								                res += tree[--r];
								        }

								        return res;
								    }

								static int a=1;

							static class decimal{
								int n;double d;
								public decimal(int n,double d) {

									this.n=n;
									this.d=d;
									// TODO Auto-generated constructor stub
								}
							}

							static	boolean isPossible(List<Integer> C,int m,int k) {
									if(m==0)return false;
									for(int i=0;i<C.size();i++) {

										if(C.get(i)>m) {
											k-=C.get(i)/m;
										}
										if(k<0)return false;
									}
										return true;

								}

								static boolean isSubsetSum(int set[],  int n, int sum)
						{
						// The value of subset[i][j] will be
						// true if there is a subset of
						// set[0..j-1] with sum equal to i
						boolean subset[][] = new boolean[sum + 1][n + 1];

						// If sum is 0, then answer is true
						for (int i = 0; i <= n; i++)
						subset[0][i] = true;

						// If sum is not 0 and set is empty,
						// then answer is false
						for (int i = 1; i <= sum; i++)
						subset[i][0] = false;

						// Fill the subset table in botton
						// up manner
						for (int i = 1; i <= sum; i++) {
						for (int j = 1; j <= n; j++) {
						    subset[i][j] = subset[i][j - 1];
						    if (i >= set[j - 1])
						        subset[i][j] = subset[i][j]
						                       || subset[i - set[j - 1]][j - 1];
						}
						}

						return subset[sum][n];
						}

								static boolean search(ArrayList<Integer> a,int m,int h) {
									PriorityQueue<Integer> p=new PriorityQueue<Integer>(new Comparator<Integer>() {
											@Override
											public int compare(Integer o1, Integer o2) {
												// TODO Auto-generated method stub
												return o2-o1;
											}
									});

									for(int i=0;i<m;i++) {
										p.add(a.get(i));
									}
									int c=0;
									//System.out.println(p);
									while(!p.isEmpty()) {
										//System.out.println(p);

										h-=p.poll();
										if(h<0)break;

										c++;

									//	System.out.println(h+" "+p+" "+c+" "+m);

										if(!p.isEmpty()) {
										p.poll();
										c++;}

									}
								//	System.out.println(c!=m);
									if(c!=m)return false;

									return true;
								}
							//	static int  key=-1;

								static boolean isPowerOfTwo(long n)
							    {
							        if (n == 0)
							            return false;

							        while (n != 1)
							        {
							            if (n % 2 != 0)
							                return false;
							            n = n / 2;
							        }
							        return true;
							    }

								 static boolean isPerfectSquare(double x)
								    {

								        // Find floating point value of
								        // square root of x.
								        double sr = Math.sqrt(x);

								        // If square root is an integer
								        return ((sr - Math.floor(sr)) == 0);
								    }

							static	int binarySearch(ArrayList<Long> arr, int l, int r, long x,int key)
							    {
							        if (r >= l) {
							            int mid =  (r + l) / 2;
							//  if(mid>=arr.size())return -1;
							            // If the element is present at the
							            // middle itself

							            if (arr.get(mid) == x)
							                return mid+1;

							            // If element is smaller than mid, then
							            // it can only be present in left subarray
							            if (arr.get(mid) > x) {
							                return binarySearch(arr, l, mid - 1, x,key);
							            }
							            // Else the element can only be present
							            // in right subarray
							            key=mid+1;
							            return binarySearch(arr, mid + 1, r, x,key);
							        }

							        // We reach here when element is not present
							        // in array
							        return key;
							    }

							    static boolean isPrime(long n)
							    {
							         // Corner cases
							        if (n <= 1)
							            return false;
							        if (n <= 3)
							            return true;

							        // This is checked so that we can skip
							        // middle five numbers in below loop
							        if (n % 2 == 0 || n % 3 == 0)
							            return false;

							        for (int i = 5; i * i <= n; i = i + 6)
							            if (n % i == 0 || n % (i + 2) == 0)
							                return false;

							        return true;
							    }

							    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;
							        }
							    }

							}

							class Sol {

								int value;int in;int i;
								Sol(int value,int in,int i){
									this.value=value;
									this.in=in;
									this.i=i;
								}

						}

					 class SegmentTree {

						SegNode st[];
						int n;
					//	long arr[];
						public SegmentTree(int n) {
								this.n=n;
							//this.arr=arr;
							st=new SegNode[4*n];

						}

						void init(long n) {

							for(int i=0;i<4*n;i++) {
								st[i]=new SegNode();
							}

						}

						void set(int i,long v,int x,int lx,int rx) {

							if(rx-lx==1) {
								st[x].sum=v;
								st[x].max=v;
								return ;
							}
							int m=lx+(rx-lx)/2;
							if(i<m) set(i,v,2*x+1,lx,m);
							else set(i,v,2*x+2,m,rx);

							st[x].sum=st[2*x+1].sum+st[2*x+2].sum;
							long ma=Math.max(st[2*x+1].max,st[2*x+2].max);
								st[x].max=Math.max(ma,st[x].sum);
						}

						void set (int i,long v) {
							set(i,v,0,0,n);
						}

						long max=Integer.MIN_VALUE;
					long sum(int l,int r,int x,int lx,int rx) {
						if(lx>=r||l>=rx)return 0;
						if(lx>=l||rx<=r)return st[x].sum;

							int m=(rx+lx)/2;

							long s1=sum(l,r,2*x+1,lx,m);
							long s2=sum(l,r,2*x+2,m,rx);

							return max= Math.max(Math.max(Math.max(s1,s2),s1+s2),max);

						}
						long sum(int l,int r) {
							return sum(l,r,0,0,n);
						}

					}
						class SegNode{
							long max;long sum;
							public SegNode() {
								// TODO Auto-generated constructor stub
								sum=0;
								max=Integer.MIN_VALUE;

							}
						}


		 					import java.math.BigInteger;
							import java.util.ArrayList;
							import java.util.Arrays;
						import java.util.Collection;
						import java.util.Collections;
						import java.util.Comparator;
						import java.util.HashMap;
							import java.util.Iterator;
						import java.util.LinkedList;
						import java.util.List;
						import java.util.Map;
					import java.util.Map.Entry;
					import java.util.PriorityQueue;
						import java.util.Scanner;
						import java.util.Set;
						import java.util.Stack;
						import java.io.BufferedReader;
						import java.io.IOException;
						import java.io.InputStreamReader;
						import java.util.Scanner;
						import java.util.StringTokenizer;
						import java.util.TreeMap;
						import java.util.TreeSet;

							public class Main {

								static FastReader sc= new FastReader();
								static List<Integer> C;
								static List<Integer> B;
							static	int mod=(int)1e9+9;
								static long dp[];
								static boolean prime[] = new boolean[100001];
								public static void main(String[] args) {

										int t=sc.nextInt();
										while(t-->0) {
									int n=sc.nextInt();
									ArrayList<help> h=new ArrayList<Main.help>();
											C=new ArrayList<>();
											for (int i = 0; i < n; i++) {
												C.add(sc.nextInt());
												h.add(new help(C.get(i),i));
											}
											int m=Collections.min(C);
											Collections.sort(h,new Comparator<help>() {
												@Override
												public int compare(help o1, help o2) {
													// TODO Auto-generated method stub
													return o1.v-o2.v;
												}
											});
											boolean flag =true;

											for(int i=0;i<n;i++) {

												help temp=h.get(i);
											//	System.out.print(temp.v+" ");

												if(temp.i!=i&&C.get(i)!=temp.v) {
													//System.out.println(gcd(C.get(i), temp.v));
													if(gcd(C.get(i), temp.v)==m) {

														int k=C.get(i);
														C.set(i, temp.v);
														C.set(temp.i,k);

													}
													else flag=false;
												}

											}

											if(flag)System.out.println("YES");
											else System.out.println("NO");

										}
								}

								static class help{
									int v;int i;
									public help(int v,int i) {

										this.i=i;
										this.v=v;
										// TODO Auto-generated constructor stub
									}

										// TODO Auto-generated method stub

								}

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

								 static final int MAXN = 100001;

								    // stores smallest prime factor for every number
								    static int spf[] = new int[MAXN];

								    // Calculating SPF (Smallest Prime Factor) for every
								    // number till MAXN.
								    // Time Complexity : O(nloglogn)
								    static void sieve()
								    {
								        spf[1] = 1;
								        for (int i=2; i<MAXN; i++)

								            // marking smallest prime factor for every
								            // number to be itself.
								            spf[i] = i;

								        // separately marking spf for every even
								        // number as 2
								        for (int i=4; i<MAXN; i+=2)
								            spf[i] = 2;

								        for (int i=3; i*i<MAXN; i++)
								        {
								            // checking if i is prime
								            if (spf[i] == i)
								            {
								                // marking SPF for all numbers divisible by i
								                for (int j=i*i; j<MAXN; j+=i)

								                    // marking spf[j] if it is not
								                    // previously marked
								                    if (spf[j]==j)
								                        spf[j] = i;
								            }
								        }
								    }

							static	 void sieveOfEratosthenes(int n)
								    {
								        // Create a boolean array "prime[0..n]" and initialize
								        // all entries it as true. A value in prime[i] will
								        // finally be false if i is Not a prime, else true.
								        //boolean prime[] = new boolean[n+1];
								        for(int i=0;i<n;i++)
								            prime[i] = true;

								        for(int p = 2; p*p <=n; p++)
								        {
								            // If prime[p] is not changed, then it is a prime
								            if(prime[p] == true)
								            {
								                // Update all multiples of p
								                for(int i = p*p; i <= n; i += p)
								                    prime[i] = false;
								            }
								        }

								       // System.out.println(n);
								    }

								static long  solve(List<Integer> A,int n,long cost,int k,HashMap<Integer,  Integer> map) {
									//if(!map.containsKey(curr))map.put(curr,);
									//if(n==0&&curr!=0)return 1;
									if(dp[n]!=-1)return dp[n];
									if(n==0)return cost;
					//				if(dp[n]!=-1)return dp[n];
								//	if(dp[n]!=-1)return dp[n];

									if(map.containsKey(C.get(n-1))) {

										long addon=0l;
										if(map.get(C.get(n-1))==1)addon=2l;
										else addon=1l;

										map.put(C.get(n-1),map.get(C.get(n-1))+1);
										//System.out.println(cost+" "+n+" "+map);
										return dp[n]=Math.min(solve(A,n-1,cost+addon,k,map),solve(A, n-1,k+cost,k, new HashMap<Integer,Integer>(C.get(n-1),1)));

									}
									else {map.put(C.get(n-1), 1); return dp[n] =solve(A,  n-1, cost,k, map);
									}

								}

							static int exponentMod(int A,
					                       int B, int C)
					{

					    // Base cases
					    if (A == 0)
					        return 0;
					    if (B == 0)
					        return 1;

					    // If B is even
					    long y;
					    if (B % 2 == 0)
					    {
					        y = exponentMod(A, B / 2, C);
					        y = (y * y) % C;
					    }

					    // If B is odd
					    else
					    {
					        y = A % C;
					        y = (y * exponentMod(A, B - 1,
					                             C) % C) % C;
					    }

					    return (int)((y + C) % C);
					}

							static	int modulo(int a,int b,int c) {
								    long x=1;
								    long y=a;
								    while(b > 0){
								        if(b%2 == 1){
								            x=(x*y)%c;
								        }
								        y = (y*y)%c; // squaring the base
								        b /= 2;
								    }
								    return (int) x%c;
								}

								 static int power(int x, int y, int p)
								    {
								        // Initialize result
								        int res = 1;

								        // Update x if it is more
								        // than or equal to p
								        x = x % p;

								       if (x == 0) return 0; // In case x is divisible by p;

								        while (y > 0)
								        {
								            // If y is odd, multiply x
								            // with result
								            if((y & 1)==1)
								                res = (res * x) % p;

								            // y must be even now
								            // y = y / 2
								            y = y >> 1;
								            x = (x * x) % p;
								        }
								        return res;
								    }
								static int N = 100000;

							    static int n; // array size

							    // Max size of tree
							    static int []tree = new int[2 * N];

								 static void build( int []arr)
								    {

								        // insert leaf nodes in tree
								        for (int i = 0; i < n; i++)
								            tree[n + i] = arr[i];

								        // build the tree by calculating
								        // parents
								        for (int i = n - 1; i > 0; --i)
								            tree[i] = tree[i << 1] +
								                      tree[i << 1 | 1];
								    }

								    // function to update a tree node
								    static void updateTreeNode(int p, int value)
								    {

								        // set value at position p
								        tree[p + n] = value;
								        p = p + n;

								        // move upward and update parents
								        for (int i = p; i > 1; i >>= 1)
								            tree[i >> 1] = tree[i] + tree[i^1];
								    }

								    // function to get sum on
								    // interval [l, r)
								    static int query(int l, int r)
								    {
								        int res = 0;

								        // loop to find the sum in the range
								        for (l += n, r += n; l < r;
								                             l >>= 1, r >>= 1)
								        {
								            if ((l & 1) > 0)
								                res += tree[l++];

								            if ((r & 1) > 0)
								                res += tree[--r];
								        }

								        return res;
								    }

								static int a=1;

							static class decimal{
								int n;double d;
								public decimal(int n,double d) {

									this.n=n;
									this.d=d;
									// TODO Auto-generated constructor stub
								}
							}

							static	boolean isPossible(List<Integer> C,int m,int k) {
									if(m==0)return false;
									for(int i=0;i<C.size();i++) {

										if(C.get(i)>m) {
											k-=C.get(i)/m;
										}
										if(k<0)return false;
									}
										return true;

								}

								static boolean isSubsetSum(int set[],  int n, int sum)
						{
						// The value of subset[i][j] will be
						// true if there is a subset of
						// set[0..j-1] with sum equal to i
						boolean subset[][] = new boolean[sum + 1][n + 1];

						// If sum is 0, then answer is true
						for (int i = 0; i <= n; i++)
						subset[0][i] = true;

						// If sum is not 0 and set is empty,
						// then answer is false
						for (int i = 1; i <= sum; i++)
						subset[i][0] = false;

						// Fill the subset table in botton
						// up manner
						for (int i = 1; i <= sum; i++) {
						for (int j = 1; j <= n; j++) {
						    subset[i][j] = subset[i][j - 1];
						    if (i >= set[j - 1])
						        subset[i][j] = subset[i][j]
						                       || subset[i - set[j - 1]][j - 1];
						}
						}

						return subset[sum][n];
						}

								static boolean search(ArrayList<Integer> a,int m,int h) {
									PriorityQueue<Integer> p=new PriorityQueue<Integer>(new Comparator<Integer>() {
											@Override
											public int compare(Integer o1, Integer o2) {
												// TODO Auto-generated method stub
												return o2-o1;
											}
									});

									for(int i=0;i<m;i++) {
										p.add(a.get(i));
									}
									int c=0;
									//System.out.println(p);
									while(!p.isEmpty()) {
										//System.out.println(p);

										h-=p.poll();
										if(h<0)break;

										c++;

									//	System.out.println(h+" "+p+" "+c+" "+m);

										if(!p.isEmpty()) {
										p.poll();
										c++;}

									}
								//	System.out.println(c!=m);
									if(c!=m)return false;

									return true;
								}
							//	static int  key=-1;

								static boolean isPowerOfTwo(long n)
							    {
							        if (n == 0)
							            return false;

							        while (n != 1)
							        {
							            if (n % 2 != 0)
							                return false;
							            n = n / 2;
							        }
							        return true;
							    }

								 static boolean isPerfectSquare(double x)
								    {

								        // Find floating point value of
								        // square root of x.
								        double sr = Math.sqrt(x);

								        // If square root is an integer
								        return ((sr - Math.floor(sr)) == 0);
								    }

							static	int binarySearch(ArrayList<Long> arr, int l, int r, long x,int key)
							    {
							        if (r >= l) {
							            int mid =  (r + l) / 2;
							//  if(mid>=arr.size())return -1;
							            // If the element is present at the
							            // middle itself

							            if (arr.get(mid) == x)
							                return mid+1;

							            // If element is smaller than mid, then
							            // it can only be present in left subarray
							            if (arr.get(mid) > x) {
							                return binarySearch(arr, l, mid - 1, x,key);
							            }
							            // Else the element can only be present
							            // in right subarray
							            key=mid+1;
							            return binarySearch(arr, mid + 1, r, x,key);
							        }

							        // We reach here when element is not present
							        // in array
							        return key;
							    }

							    static boolean isPrime(long n)
							    {
							         // Corner cases
							        if (n <= 1)
							            return false;
							        if (n <= 3)
							            return true;

							        // This is checked so that we can skip
							        // middle five numbers in below loop
							        if (n % 2 == 0 || n % 3 == 0)
							            return false;

							        for (int i = 5; i * i <= n; i = i + 6)
							            if (n % i == 0 || n % (i + 2) == 0)
							                return false;

							        return true;
							    }

							    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;
							        }
							    }

							}

							class Sol {

								int value;int in;int i;
								Sol(int value,int in,int i){
									this.value=value;
									this.in=in;
									this.i=i;
								}

						}

					 class SegmentTree {

						SegNode st[];
						int n;
					//	long arr[];
						public SegmentTree(int n) {
								this.n=n;
							//this.arr=arr;
							st=new SegNode[4*n];

						}

						void init(long n) {

							for(int i=0;i<4*n;i++) {
								st[i]=new SegNode();
							}

						}

						void set(int i,long v,int x,int lx,int rx) {

							if(rx-lx==1) {
								st[x].sum=v;
								st[x].max=v;
								return ;
							}
							int m=lx+(rx-lx)/2;
							if(i<m) set(i,v,2*x+1,lx,m);
							else set(i,v,2*x+2,m,rx);

							st[x].sum=st[2*x+1].sum+st[2*x+2].sum;
							long ma=Math.max(st[2*x+1].max,st[2*x+2].max);
								st[x].max=Math.max(ma,st[x].sum);
						}

						void set (int i,long v) {
							set(i,v,0,0,n);
						}

						long max=Integer.MIN_VALUE;
					long sum(int l,int r,int x,int lx,int rx) {
						if(lx>=r||l>=rx)return 0;
						if(lx>=l||rx<=r)return st[x].sum;

							int m=(rx+lx)/2;

							long s1=sum(l,r,2*x+1,lx,m);
							long s2=sum(l,r,2*x+2,m,rx);

							return max= Math.max(Math.max(Math.max(s1,s2),s1+s2),max);

						}
						long sum(int l,int r) {
							return sum(l,r,0,0,n);
						}

					}
						class SegNode{
							long max;long sum;
							public SegNode() {
								// TODO Auto-generated constructor stub
								sum=0;
								max=Integer.MIN_VALUE;

							}
						}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import static java.util.Collections.sort;

public class ProblemC {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int tt = 0; tt < t; tt++) {
            int n = sc.nextInt();
            int[] arr = new int[n];
            List<Integer> indexes = new ArrayList<>(), values = new ArrayList<>();
            boolean canBeSorted = true;

            int min = Integer.MAX_VALUE;
            for(int i = 0; i < n; i++) {
                arr[i] = sc.nextInt();
                min = Math.min(arr[i], min);
            }

            for(int i = 0; i < n; i++) {
                if(arr[i] % min == 0) {
                    indexes.add(i);
                    values.add(arr[i]);
                }
            }

            sort(values);
            for(int i = 0; i < indexes.size(); i++) arr[indexes.get(i)] = values.get(i);
            for(int i = 1; i < n; i++)  if(arr[i] < arr[i-1]) canBeSorted = false;
            System.out.println(canBeSorted ? "YES" : "NO");
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int k , a , b;
string s;
int main(){
    cin >> k >> a >> b >> s;
    if(k * b < s.size() || s.size() > a * k) { cout << "No solution"; }
    else{
        int sz = s.size();
        int p = sz / k;
        int ps = 0;
        for(int i = k - 1; i >= 0; i --){
            int len = p;
            while(sz - len > i * b){
                len ++;
            }
            for(int pos = ps; pos < ps + len; pos ++) cout << s[pos]; cout << endl; ps += len;
            sz -= len;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int k , a , b;
string s;
int main(){
    cin >> k >> a >> b >> s;
    if(k * b < s.size() && s.size() > a * k) { cout << "No solution"; }
    else{
        int sz = s.size();
        int p = sz / k;
        int ps = 0;
        for(int i = k - 1; i >= 0; i --){
            int len = p;
            while(sz - len > i * b){
                len ++;
            }
            for(int pos = ps; pos < ps + len; pos ++) cout << s[pos]; cout << endl; ps += len;
            sz -= len;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
int k , a , b;
string s;
int main(){
    cin >> k >> a >> b >> s;
    int p = int(s.size()) / k + (int(s.size()) % k != 0);
    if(!(p >= a && p <= b)) { cout << "No solution"; }
    else{
        int pp = p;
        for(int i = 0; i < s.size(); i ++){
            pp --;
            cout << s[i];
            if(!pp) cout << endl, pp = p;
        }
    }
}


//Himanshu Shukla
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define file_input
//#define multitest
//#define measure_time
#define ll long long
#define v32 vector<int>
#define v64 vector<ll>
#define s32 set<int>
#define s64 set<ll>
#define graph vector<s32>
#define p32 pair<int, int>
#define p64 pair<ll, ll>
#define fi first
#define se second
#define ln '\n'
#define debug(x) cerr << (#x) << ": " << (x) << ln
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline ll add(ll a, ll b, ll m) { return (((a % m + b % m) % m + m) % m); }
inline ll mul(ll a, ll b, ll m) { return (((a % m * b % m) % m + m) % m); }
const double EPS = 1e-6;
const ll MOD = 1e9 + 7;
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p)
{
	out << '[' << p.fi << ", " << p.se << ']';
	return out;
}
template <template <typename, typename...> class ContainerType, typename ValueType, typename... Args>
void print_container(const ContainerType<ValueType, Args...> &c)
{
	cout << "{ ";
	for (const auto &v : c)
		cout << v << ' ';
	cout << '}' << ln;
}

int dp[205][205][205];
int dp1[205][205][205];

int go(int i, int j, int k, int a, int b, int n, string &str)
{
	if ((j - i + 1) >= a && (j - i + 1) <= b && k == 0)
	{
		dp[i][j][k] = (j - i + 1);
		dp1[i][j][k] = 1;
		return 1;
	}
	if (k == 0 && ((j - i + 1) < a || (j - i + 1) > b))
	{
		dp[i][j][k] = 0;
		dp1[i][j][k] = 0;
		return 0;
	}
	if (dp1[i][j][k] != -1)
		return dp1[i][j][k];
	int ans = 0;
	for (int l = a; l <= b; l++)
	{
		ans |= go(i + l, n - 1, k - 1, a, b, n, str);
		if (ans){
			dp[i][j][k] = l;
			dp1[i][j][k] = ans;
			return ans;
		}
	}
	dp1[i][j][k] = ans;
	return ans;
}

void solve()
{
	int k, a, b;
	cin >> k >> a >> b;
	string str;
	cin >> str;
	int n = str.length();
	for (int i = 0; i < n + 1; i++)
		for (int j = 0; j < n + 1; j++)
			for (int l = 0; l < k + 1; l++)
				dp[i][j][l] = dp1[i][j][l] = -1;
	bool ans = go(0, n - 1, k - 1, a, b, n, str);
	if (!ans)
		cout << "No solution" << ln;
	else
	{
		int i = 0, j = n - 1, l = k - 1;
		while (i <= j)
		{
			cout << str.substr(i, dp[i][j][l]) << ln;
			i = i + dp[i][j][l];
			l--;
		}
	}
	return;
}

int main()
{
	FASTIO;
#ifdef file_input
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	clock_t start, stop;
	int t = 1;
#ifdef multitest
	cin >> t;
#endif
#ifdef measure_time
	start = clock();
#endif
	for (int tc = 1; tc < t + 1; tc++)
		solve();
#ifdef measure_time
	stop = clock();
	cerr << "Time :" << fixed << setprecision(5) << double(stop - start) / double(CLOCKS_PER_SEC) << "s" << ln;
#endif
	return 0;
}