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.*;

/*
6
2 2
5 2
1 5
1 1
3
1 2
1 1
1 2
1 1
1 2
2 3
2 1
1
1

*/

public class e2 {

	static int n;
	static int m;
	static int[][] mat;

	public static void main(String[] args){
		JS scan = new JS();
		PrintWriter out = new PrintWriter(System.out);
		int t = scan.nextInt();
		long start = System.currentTimeMillis();
		for(int q = 1; q <= t; q++){
			n = scan.nextInt();
			m = scan.nextInt();
			// n = 12;
			// m = 20000;
			mat = new int[n][m];
			for(int i = 0; i < n; i++){
				for(int j = 0; j < m; j++){
					mat[i][j] = scan.nextInt();
					// mat[i][j] = 100000;
				}
			}
			int[] max = new int[m];
			PriorityQueue<Item> pq = new PriorityQueue<Item>();
			for(int i = 0; i < m; i++){
				for(int j = 0; j < n; j++){
					max[i] = max(max[i], mat[j][i]);
				}
				pq.add(new Item(i, max[i]));
			}
			ArrayList<Item> guys = new ArrayList<Item>();
			while(!pq.isEmpty() && guys.size() < n){
				Item tt = pq.poll();
				guys.add(tt);
			}

			int[][] cost = new int[guys.size()][1 << n];
			// long time = System.currentTimeMillis();
			for(int i = 0; i < guys.size(); i++){
				int g = guys.get(i).a;
				for(int s = 0; s < n; s++){
					for(int j = 0; j < (1 << n); j++){
						int sum = 0;
						for(int k = 0; k < n; k++){
							if((j & (1 << k)) > 0){
								sum += mat[(k+s)%n][g];
							}
						}
						cost[i][j] = max(cost[i][j], sum);
					}
				}
			}

			// for(int i = 0; i < guys.size(); i++) System.out.println(Arrays.toString(cost[i]));

			// System.out.println(System.currentTimeMillis()-time);
			int full = (1 << n)-1;
			// time = System.currentTimeMillis();
			int[][] dp = new int[guys.size()+1][1 << n];
			int ans = 0;

			
	
			for(int c = 0; c < guys.size(); c++){
					for(int j = 0; j < (1 << n); j++){
						for(int i = j; i < (1 << n); i = (i+1)|j){
							dp[c+1][i] = 
								max(dp[c+1][i], cost[c][j]+dp[c][i^j]);
							
							ans = max(ans, dp[c+1][i]);
						}
					}
			}
			// for(int i = 0; i < dp.length; i++) System.out.println(Arrays.toString(dp[i]));
			// System.out.println(System.currentTimeMillis()-time);

			out.println(ans);
		}
		// System.out.println(System.currentTimeMillis()-start);
		out.flush();
	}

	static int max(int a, int b){
		return a > b? a : b;
	}

	static class Item implements Comparable<Item>{

		int a;
		int b;

		public Item(int a, int b){
			this.a = a;
			this.b = b;
		}

		public int compareTo(Item o){
			return o.b-this.b;
		}
	}

	static class JS{
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;
 
		public JS() {
			in = new BufferedInputStream(System.in, BS);
		}
 
		public JS(String s) throws FileNotFoundException {
			in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
		}
 
		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}				
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}
 
		public int nextInt() {
			return (int)nextLong();
		}
 
		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}
 
		public double nextDouble() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}
 
		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}
 
		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}
 
		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}
	}
}