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>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <typename T, typename U>
inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) {
  _out << _p.first << ' ' << _p.second;
  return _out;
}
template <typename T, typename U>
inline istream &operator>>(istream &_in, pair<T, U> &_p) {
  _in >> _p.first >> _p.second;
  return _in;
}
template <typename T>
inline ostream &operator<<(ostream &_out, const vector<T> &_v) {
  if (_v.empty()) {
    return _out;
  }
  _out << _v.front();
  for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) {
    _out << ' ' << *_it;
  }
  return _out;
}
template <typename T>
inline istream &operator>>(istream &_in, vector<T> &_v) {
  for (auto &_i : _v) {
    _in >> _i;
  }
  return _in;
}
const int MAXN = 3e5;
const int INF = 1e9;
const int MOD = 1e9 + 7;
class E2VrashayaStolbciUslozhnennayaVersiya {
 public:
  void solve(std::istream &in, std::ostream &out) {
    int n, m;
    in >> n >> m;
    vector<vector<int>> a(n, vector<int>(m));
    in >> a;
    vector<pair<int, int>> b(m);
    for (int i = 0; i < m; i++) {
      b[i] = {0, i};
      for (int j = 0; j < n; j++) {
        b[i].first = max(b[i].first, a[j][i]);
      }
    }
    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());
    int ans[(1 << n)];
    int mem[n];
    int getmax[min(n, m)][(1 << n)];
    for (int i = 0; i < min(n, m); i++) {
      for (int d1 = 0; d1 < (1 << n); d1++) {
        int ii = b[i].second;
        fill(mem, mem + n, 0);
        for (int jj = 0; jj < n; jj++) {
          if ((d1 >> jj) & 1) {
            for (int jj1 = jj, jj2 = 0; jj2 < n; jj1++, jj2++) {
              if (jj1 >= n) {
                jj1 -= n;
              }
              mem[jj2] += a[jj1][ii];
            }
          }
        }
        getmax[i][d1] = 0;
        for (int j = 0; j < n; j++) {
          getmax[i][d1] = max(getmax[i][d1], mem[j]);
        }
      }
    }
    fill(ans, ans + (1 << n), 0);
    for (int i = 0; i < min(n, m); i++) {
      int ii = b[i].second;
      for (int d = (1 << n) - 1; d >= 0; d--) {
        for (int d1 = d; d1 > 0; d1 = (d1 - 1) & d) {
          ans[d] = max(ans[d], ans[d ^ d1] + getmax[i][d1]);
        }
      }
    }
    out << ans[(1 << n) - 1] << endl;
  }
};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  E2VrashayaStolbciUslozhnennayaVersiya solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  int n;
  in >> n;
  for (int i = 0; i < n; ++i) {
    solver.solve(in, out);
  }
  return 0;
}