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;
const long long inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const long double eps = 1e-6;
const int mod = 1e9 + 7;
const int maxn = 20;
const int N = 1e5 + 10;
const int M = N << 1;
const long long mm = (1LL << 32);
template <class T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= c == '-';
  for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
  x = f ? -x : x;
}
template <class T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  T y = 1;
  int len = 1;
  for (; y <= x / 10; y *= 10) ++len;
  for (; len; --len, x %= y, y /= 10) putchar(x / y + 48);
}
long long qpow(long long a, long long b, long long MOD) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % MOD;
    b >>= 1;
    a = (a * a) % MOD;
  }
  return ans;
}
int n, m;
int a[20][3000];
struct node {
  int id, key;
  inline bool operator<(const node &x) const { return key > x.key; }
} t[3000];
int f[(1 << 20) + 10], t1[(1 << 20) + 10], t2[(1 << 20) + 10];
bool vis[3000];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) t[i].id = i, t[i].key = 0, vis[i] = 0;
    for (int i = 0; i < n; ++i)
      for (int j = 1; j <= m; ++j) {
        cin >> a[i][j];
        t[j].key = max(t[j].key, a[i][j]);
      }
    sort(t + 1, t + 1 + m);
    for (int i = 1; i <= m && i <= n; ++i) vis[t[i].id] = 1;
    memset(f, 0, sizeof(f));
    for (int i = 1; i <= m; ++i) {
      if (!vis[i]) continue;
      for (int s = 0; s < (1 << n); ++s) t1[s] = 0;
      for (int j = 1; j <= n; ++j) {
        for (int s = 0; s < (1 << n); ++s) t2[s] = f[s];
        for (int s = 0; s < (1 << n); ++s)
          for (int k = 0; k < n; ++k)
            if (!(s & (1 << k)))
              t2[s | (1 << k)] = max(t2[s | (1 << k)], t2[s] + a[k][i]);
        int tmp = a[0][i];
        for (int k = 0; k < n - 1; ++k) a[k][i] = a[k + 1][i];
        a[n - 1][i] = tmp;
        for (int s = 0; s < (1 << n); ++s) t1[s] = max(t1[s], t2[s]);
      }
      for (int s = 0; s < (1 << n); ++s) f[s] = t1[s];
    }
    cout << f[(1 << n) - 1] << endl;
  }
  return 0;
}