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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void Readin(T &X) {
  X = 0;
  register bool F = false;
  register char C = getchar();
  while (C < '0' or C > '9') {
    if (C == '-') F = true;
    C = getchar();
  }
  while (C <= '9' and C >= '0')
    X = (X << 1) + (X << 3) + C - '0', C = getchar();
  if (F) X = -X;
}
template <class T>
inline void Checkmax(T &A, T B) {
  if (A < B) A = B;
}
template <class T>
inline void Checkmin(T &A, T B) {
  if (A > B) A = B;
}
const int MaxN = 12 + 3;
const int MaxD = 1 << 12 | 3;
const int MaxM = 2000 + 3;
int N, M;
int A[MaxN][MaxM], I[MaxD], J[MaxD], K[MaxD];
pair<int, int> Sort[MaxM];
int main() {
  int T;
  Readin(T);
  while (T--) {
    Readin(N);
    Readin(M);
    for (register int i = 0; ++i <= N;)
      for (register int j = 0; ++j <= M;) Readin(A[i][j]);
    for (register int i = 0; ++i <= M;) {
      Sort[i].first = 0;
      Sort[i].second = i;
      for (register int j = 0; ++j <= N;) Checkmin(Sort[i].first, -A[j][i]);
    }
    sort(Sort + 1, Sort + 1 + M);
    register int Min = min(N, M);
    memset(I, 0, sizeof I);
    for (register int p = 0; ++p <= Min;) {
      register int Pos = Sort[p].second;
      memset(K, 0, sizeof K);
      for (register int k = 0; ++k <= N;) {
        A[N + 1][Pos] = A[1][Pos];
        for (register int i = 0; ++i <= N;) A[i][Pos] = A[i + 1][Pos];
        memcpy(J, I, sizeof I);
        for (register int i = 0; ++i <= N;)
          for (register int j = 0; j < (1 << N); ++j)
            if (((1 << (i - 1)) & j) == 0)
              Checkmax(J[(1 << (i - 1)) | j], J[j] + A[i][Pos]);
        for (register int i = 0; i < (1 << N); ++i) Checkmax(K[i], J[i]);
      }
      for (register int i = 0; i < (1 << N); ++i) Checkmax(I[i], K[i]);
    }
    printf("%d\n", I[(1 << N) - 1]);
  }
  return 0;
}