Problem: This is a harder version of the problem. The difference is only in constraints.

You are given a rectangular n × m matrix a. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times.

After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for i-th row it is equal r_i. What is the maximal possible value of r_1+r_2+…+r_n?

Input

The first line contains an integer t (1 ≤ t ≤ 40), the number of test cases in the input.

The first line of each test case contains integers n and m (1 ≤ n ≤ 12, 1 ≤ m ≤ 2000) — the number of rows and the number of columns in the given matrix a. 

Each of the following n lines contains m integers, the elements of a (1 ≤ a_{i, j} ≤ 10^5).

Output

Print t integers: answers for all test cases in the order they are given in the input.

Example

Input


3
2 3
2 5 7
4 2 4
3 6
4 1 5 2 10 4
8 6 6 4 9 10
5 4 9 5 8 7
3 3
9 9 9
1 1 1
1 1 1


Output


12
29
27

Note

In the first test case you can shift the third column down by one, this way there will be r_1 = 5 and r_2 = 7.

In the second case you can don't rotate anything at all, this way there will be r_1 = r_2 = 10 and r_3 = 9.

Solution (in JAVA):

Solution:
import java.util.*;
import java.io.*;

public class Main {
	public static void main(String args[]) {new Main().run();}

	FastReader in = new FastReader();
	PrintWriter out = new PrintWriter(System.out);
	void run(){
		int t=in.nextInt();
		for(int i=0;i<t;i++) {
			out.println(work());
		}
		out.flush();
	}
	long mod=1000000007;
	long gcd(long a,long b) {
		return b==0?a:gcd(b,a%b);
	}
	int work() {
		int n=in.nextInt();
		int m=in.nextInt();
		int[][] A=new int[n][m];
		int[][] B=new int[n][m];
		int[][] R=new int[m][2];
		for(int i=0;i<m;i++)R[i][1]=i;
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				A[i][j]=in.nextInt();
				R[j][0]=Math.max(R[j][0], A[i][j]);
			}
		}
		Arrays.sort(R,new Comparator<int[]>() {
			public int compare(int[] arr1,int[] arr2) {
				return arr2[0]-arr1[0];
			}
		});
		for(int j=0;j<m;j++) {
			int index=R[j][1];
			for(int i=0;i<n;i++) {
				B[i][j]=A[i][index];
			}
		}
		m=Math.min(n, m);
		int[][] dp=new int[m][1<<n];
		int[][] rec=new int[m][1<<n];
		for(int j=0;j<m;j++) {
			for(int s=0;s<n;s++) {//转
				for(int i=1;i<1<<n;i++) {
					int sum=0;
					for(int b=0;b<n;b++) {
						if(((1<<b)&i)>0) {
							sum+=B[(b+s)%n][j];
						}
					}
					rec[j][i]=Math.max(sum, rec[j][i]);
				}
			}
		}
		
		for(int j=0;j<m;j++) {
			for(int i=0;i<1<<n;i++) {
				if(j==0) {
					dp[j][i]=rec[j][i];
				}else {
					for(int p=i+1;;p++) {
						if(p>=1<<n)break;
						p=p|i;
						dp[j][p]=Math.max(dp[j][p], rec[j][i]+dp[j-1][p^i]);
					}
				}
			}
		}
		return dp[m-1][(1<<n)-1];
	}
}



class FastReader
{
	BufferedReader br;
	StringTokenizer st;

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

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

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

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