#include <bits/stdc++.h>
using namespace std;
const int N = 2002;
vector<int> a[N];
int val[13][1 << 12], dp[13][1 << 12];
int f(int i, int mask) {
  if (!i) return 0;
  if (~dp[i][mask]) return dp[i][mask];
  int res = f(i - 1, mask);
  for (int s = mask; s; s = (s - 1) & mask) {
    res = max(res, f(i - 1, mask ^ s) + val[i][s]);
  }
  return dp[i][mask] = res;
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) a[i].clear();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      int x;
      cin >> x;
      a[j].push_back(x);
    }
  }
  sort(a + 1, a + m + 1, [](vector<int> &p, vector<int> &q) {
    return *max_element(p.begin(), p.end()) > *max_element(q.begin(), q.end());
  });
  for (int i = 1; i <= min(n, m); i++) {
    for (int j = 0; j < n; j++) a[i].push_back(a[i][j]);
    for (int mask = 0; mask < (1 << n); mask++) {
      val[i][mask] = 0;
      for (int j = 0; j < n; j++) {
        int nw_mask = mask << j, sum = 0;
        for (int l = 0; (1 << l) <= nw_mask; l++)
          if ((nw_mask >> l) & 1) sum += a[i][l];
        val[i][mask] = max(val[i][mask], sum);
      }
    }
  }
  memset(dp, -1, sizeof dp);
  cout << f(min(n, m), (1 << n) - 1) << "\n";
}
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) solve();
}
