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>
bool local = false;
using namespace std;
template <class T>
void print(T v) {
  cout << ((int)(v).size()) << "\n";
  for (auto x : v) cout << x << ' ';
  cout << "\n";
};
template <class T>
void print1(T v) {
  cout << ((int)(v).size()) << "\n";
  for (auto x : v) cout << x + 1 << ' ';
  cout << "\n";
};
template <class T1, class T2>
ostream &operator<<(ostream &o, pair<T1, T2> x) {
  return o << x.first << ' ' << x.second;
}
template <class T1, class T2>
istream &operator>>(istream &o, pair<T1, T2> &x) {
  return o >> x.first >> x.second;
}
template <class T>
ostream &operator<<(ostream &o, vector<T> &x) {
  for (auto &el : x) o << el << ' ';
  return o;
}
template <class T>
istream &operator>>(istream &o, vector<T> &x) {
  for (auto &el : x) o >> el;
  return o;
}
template <class T1, class T2>
pair<T1, T2> operator+(pair<T1, T2> a, pair<T1, T2> b) {
  a.first += b.first;
  a.second += b.second;
  return a;
}
template <class T1, class T2>
pair<T1, T2> operator-(pair<T1, T2> a, pair<T1, T2> b) {
  a.first -= b.first;
  a.second -= b.second;
  return a;
}
template <class T1, class T2>
void operator+=(pair<T1, T2> &a, pair<T1, T2> b) {
  a.first += b.first;
  a.second += b.second;
}
template <class T1, class T2>
void operator-=(pair<T1, T2> &a, pair<T1, T2> b) {
  a.first -= b.first;
  a.second -= b.second;
}
int nint() {
  int x;
  cin >> x;
  return x;
}
double getTime() { return clock() / (double)CLOCKS_PER_SEC; };
mt19937 rnd(0);
int rand(int n) { return rnd() % n; }
int rand(int l, int r) { return rnd() % (r - l + 1) + l; }
const int mod = 1000000000 + 7;
void initIO() {
  if (local) {
    freopen("input.txt", "r", stdin);
    srand((int)time(0));
    rnd.seed((int)time(0));
  } else {
    {
      ios::sync_with_stdio(0);
      cin.tie(0);
      cout.tie(0);
    };
  }
}
void solve();
void precalc();
int TID;
signed main() {
  initIO();
  int tc = 1;
  cin >> tc;
  precalc();
  for (int tt = 0; tt < (tc); tt++) {
    TID = tt;
    solve();
  }
  if (local)
    cout << "\n"
         << "time = " << getTime() << "\n";
}
void precalc() {}
int n;
int m;
vector<int> v[2050];
int val[105][1 << 12];
int get(int id, int mask) {
  int ans = 0;
  for (int o = 0; o < (n); o++) {
    int s = 0;
    for (int i = 0; i < (n); i++)
      if (mask & (1 << i)) s += v[id][o + i];
    ans = max(ans, s);
  }
  return ans;
}
int table[105][1 << 12];
int getans(int p, int mask) {
  if (p == m || mask == 0) return 0;
  int &ans = table[p][mask];
  if (ans != -1) return ans;
  ans = getans(p + 1, mask);
  for (int nm = mask; nm; nm = mask & (nm - 1))
    ans = max(ans, getans(p + 1, mask ^ nm) + val[p][nm]);
  return ans;
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i < (m); i++) v[i].clear();
  for (int i = 0; i < (n); i++) {
    for (int j = 0; j < (m); j++) {
      int x;
      cin >> x;
      v[j].emplace_back(x);
    }
  }
  if (m >= n) {
    vector<pair<int, int> > o(m);
    for (int i = 0; i < (m); i++) {
      o[i].first = *max_element(v[i].begin(), v[i].end());
      o[i].second = i;
    }
    sort(o.begin(), o.end());
    reverse(o.begin(), o.end());
    vector<vector<int> > tmp;
    for (pair<int, int> x : o) tmp.emplace_back(v[x.second]);
    for (int i = 0; i < (n); i++) v[i] = tmp[i];
    m = n;
  }
  for (int i = 0; i < (m); i++)
    for (int j = 0; j < (n); j++) v[i].emplace_back(v[i][j]);
  for (int i = 0; i < (m); i++)
    for (int mask = 0; mask < (1 << n); mask++) val[i][mask] = get(i, mask);
  memset(table, -1, sizeof(table));
  cout << getans(0, (1 << n) - 1) << "\n";
}