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 E1 {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		//optimizaciones que parece que solo son necesarias para java
		ArrayList<Integer>[] reps = new ArrayList[13]; //representantes de las clases
		int[][] index = new int[13][]; // mapea para cada representante un indice de 0 a |clases|-1
		int[][] eqcl = new int[13][]; //mapea para cada mask, su clase
		ArrayList<Integer>[][] nexts = new ArrayList[13][];	//para cada clase, los masks compatibles
		for(int n = 1; n <= 12; n++) {
			eqcl[n] = new int[(1 << n)];
			reps[n] = new ArrayList<Integer>();
			index[n] = new int[(1 << n)];
			int ind = 0;
			for(int mask = 0; mask < (1 << n); mask++) {
				boolean add = true;
				for(int k = 0; k < n; k++) {
					if(rot(mask, k, n) < mask) add = false;
				}
				if(add) {
					reps[n].add(mask);
					index[n][mask] = ind; ind++;
				}
			}
			nexts[n] = new ArrayList[reps[n].size()];
			for(int i = 0; i < reps[n].size(); i++) {
				int mask = reps[n].get(i);
				for(int k = 0; k < n; k++) {
					eqcl[n][rot(mask, k, n)] = i;
				}
				nexts[n][i] = new ArrayList<>();
				for(int y = 0; y < (1 << n); y++) {
					if((mask & y) == 0) {
						nexts[n][i].add(y);
					}
				}
			}
		}
		int T = Integer.parseInt(br.readLine());
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		for(int test = 0; test < T; test++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int n = Integer.parseInt(st.nextToken());
			int m = Integer.parseInt(st.nextToken());
			int[][] arrt = new int[m][n];
			for(int i = 0; i < n; i++) {
				st = new StringTokenizer(br.readLine());
				for(int j = 0; j < m; j++) {
					arrt[j][i] = Integer.parseInt(st.nextToken());
				}
			}
			Column[] cols = new Column[m];
			for(int j = 0; j < m; j++) {
				cols[j] = new Column(arrt[j]);
			}
			Arrays.sort(cols, Collections.reverseOrder());
			m = Integer.min(n, m);
			int[][] arr = new int[n][m];
			for(int i = 0; i < n; i++) {
				for(int j = 0; j < m; j++) {
					arr[i][j] = cols[j].arr[i];
				}
			}
			int[][] max = new int[m][reps[n].size()];
			for(int c = 0; c < m; c++) {
				for(int mask = 0; mask < (1 << n); mask++) {
					int curr = 0;
					for(int i = 0; i < n; i++) {
						if((mask & (1 << i)) > 0) curr += arr[i][c];
					}
					int cl = eqcl[n][mask];
					max[c][cl] = Integer.max(max[c][cl], curr);
				}
			}
			int[][] dp = new int[m+1][reps[n].size()];
			for(int c = 0; c < m; c++) {
				for(int i = 0; i < reps[n].size(); i++) {
					int mask = reps[n].get(i);
					for(int next: nexts[n][i]) { //opt
						int cl = eqcl[n][next];
						int dl = eqcl[n][mask | next];
						if(dp[c][i] + max[c][cl] > dp[c+1][dl]) { // el dp
							dp[c+1][dl] = dp[c][i] + max[c][cl]; 
						}
					}
				}
			}
			bw.write(dp[m][reps[n].size() - 1]+"\n");
		}
		bw.flush();
	}
	static int rot(int x, int k, int n) {
		int a = x << k;
		int b = x >> (n - k);
		return (a + b) & ((1 << n) - 1);
	}
	static class Column implements Comparable<Column>{
		int[] arr;
		int max;
		public Column(int[] arr) {
			this.arr = arr;
			max = 0;
			for(int k: arr) {
				max = Integer.max(max, k);
			}
		}
		@Override
		public int compareTo(Column col) {
			return max - col.max;
		}
	}
}