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.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;

public class Test {
  static PrintWriter writer =
      new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  int[][] a = new int[12][2000];
  int[][] e = new int[12 * 2000][3];
  Integer[] se = new Integer[12 * 2000];
  boolean[] used = new boolean[2000];
  int[] dp = new int[1 << 12];
  int[] one = new int[1 << 12];

  static int readInt() {
    int ans = 0;
    boolean neg = false;
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (c == '-') {
          start = true;
          neg = true;
          continue;
        } else if (c >= '0' && c <= '9') {
          start = true;
          ans = ans * 10 + c - '0';
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return neg ? -ans : ans;
  }

  static long readLong() {
    long ans = 0;
    boolean neg = false;
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (c == '-') {
          start = true;
          neg = true;
          continue;
        } else if (c >= '0' && c <= '9') {
          start = true;
          ans = ans * 10 + c - '0';
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return neg ? -ans : ans;
  }

  static String readLine() {
    StringBuilder b = new StringBuilder();
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (Character.isLetterOrDigit(c)) {
          start = true;
          b.append((char) c);
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return b.toString();
  }

  public static void main(String[] args) {
    Test te = new Test();
    te.start();
    writer.flush();
  }

  void start() {
    int t = readInt();
    while (t-- > 0) {
      int n = readInt(), m = readInt();
      for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
          a[i][j] = readInt();
          int k = i * m + j;
          e[k][0] = a[i][j];
          e[k][1] = j;
          e[k][2] = i;
        }
      for (int i = n * m - 1; i >= 0; i--) se[i] = i;
      Arrays.sort(
          se,
          0,
          n * m,
          (i, j) -> {
            int[] x = e[i], y = e[j];
            return x[0] == y[0]
                ? (x[1] == y[1] ? Integer.compare(x[2], y[2]) : Integer.compare(x[1], y[1]))
                : Integer.compare(x[0], y[0]);
          });
      Arrays.fill(used, 0, m, false);
      Arrays.fill(dp, 0, 1 << n, -1);
      dp[0] = 0;
      int cc = 0;
      for (int x = n * m - 1; x >= 0; x--) {
        int c = e[se[x]][1];
        if (used[c]) continue;
        used[c] = true;
        cc++;
        if (cc > n) break;

        Arrays.fill(one, 0, 1 << n, 0);

        for (int i = (1 << n) - 1; i > 0; i--) {
          for (int r = 0; r < n; r++) {
            int sum = 0;
            for (int j = 0; j < n; j++) if (((i >> j) & 1) != 0) sum += a[(j + r) % n][c];
            one[i] = Math.max(one[i], sum);
          }
        }

        for (int i = (1 << n) - 1; i >= 0; i--) {
          int u = (1 << n) - 1 - i;
          int p = u;
          if (dp[i] >= 0)
            while (p > 0) {
              dp[i | p] = Math.max(dp[i | p], dp[i] + one[p]);
              p = (p - 1) & u;
            }
        }
      }
      writer.println(dp[(1 << n) - 1]);
    }
  }
}