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;
using ll = long long;
using pII = pair<int, int>;
using pLL = pair<ll, ll>;
using VI = vector<int>;
using VL = vector<ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2>
inline void chadd(T1 &x, T2 y) {
  x += y;
  while (x >= mod) x -= mod;
  while (x < 0) x += mod;
}
template <class T1, class T2>
inline void chmax(T1 &x, T2 y) {
  if (x < y) x = y;
}
template <class T1, class T2>
inline void chmin(T1 &x, T2 y) {
  if (x > y) x = y;
}
inline int rd() {
  int x;
  cin >> x;
  return x;
}
template <class T>
inline void rd(T &x) {
  cin >> x;
}
template <class T>
inline void rd(vector<T> &vec) {
  for (auto &it : vec) cin >> it;
}
template <class T>
inline void out(T s) {
  cout << s << "\n";
}
template <class T>
inline void out(vector<T> &vec) {
  for (auto &it : vec) cout << it << " ";
  cout << "\n";
}
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1) res = res * base % mod;
    base = base * base % mod;
    n >>= 1;
  }
  return res;
}
constexpr int N = 2e3 + 10;
constexpr int M = 4e3 + 200;
int n, m, a[N][20], b[20], f[M], g[M], bf[M];
vector<pII> vec;
void Sort() {
  vec.clear();
  for (int i = 1; i <= m; ++i) {
    int Max = a[i][1];
    for (int j = 2; j <= n; ++j) Max = max(Max, a[i][j]);
    vec.push_back(pII(Max, i));
  }
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
}
inline void shift() {
  int t = b[n];
  for (int i = n; i >= 2; --i) b[i] = b[i - 1];
  b[1] = t;
}
void run() {
  cin >> n >> m;
  for (int j = 1; j <= n; ++j)
    for (int i = 1; i <= m; ++i) cin >> a[i][j];
  Sort();
  memset(f, 0, sizeof f);
  m = min(n, m);
  for (int i = 1; i <= m; ++i) {
    int id = vec[i - 1].second;
    for (int j = 1; j <= n; ++j) b[j] = a[id][j];
    memset(g, 0, sizeof g);
    for (int j = 1; j <= n; ++j) {
      shift();
      for (int mask = 0; mask < (1 << n); ++mask) {
        int sum = 0;
        for (int k = 0; k < n; ++k)
          if ((mask >> k) & 1) sum += b[k + 1];
        chmax(g[mask], sum);
      }
    }
    for (int mask = 0; mask < (1 << n); ++mask) bf[mask] = f[mask];
    for (int mask = 0; mask < (1 << n); ++mask) {
      int fmask = ((1 << n) - 1) ^ mask;
      for (int o = fmask; o > 0; o = (o - 1) & fmask) {
        f[mask ^ o] = max(f[mask ^ o], bf[mask] + g[o]);
      }
    }
    for (int mask = 0; mask < (1 << n); ++mask)
      f[mask] = max(f[mask], bf[mask]);
  }
  cout << f[(1 << n) - 1] << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << fixed << setprecision(20);
  int _T;
  cin >> _T;
  while (_T--) run();
  return 0;
}