#include <bits/stdc++.h>
using namespace std;
long long pre[2005][(1 << 12) + 5], dp[2005][(1 << 12) + 5], a[14][2005];
bool cmp(vector<long long> a, vector<long long> b) {
  long long mx1 = *max_element((a).begin(), (a).end());
  long long mx2 = *max_element((b).begin(), (b).end());
  return mx1 > mx2;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  memset(pre, 0, sizeof pre);
  memset(dp, 0, sizeof dp);
  memset(a, 0, sizeof a);
  for (long long i = 0; i < n; i++)
    for (long long j = 1; j < m + 1; j++) cin >> a[i][j];
  vector<long long> v[m + 1];
  for (long long j = 1; j < m + 1; j++)
    for (long long i = 0; i < n; i++) v[j].push_back(a[i][j]);
  sort(v + 1, v + m + 1, cmp);
  for (long long j = 1; j < min(m, n) + 1; j++)
    for (long long i = 0; i < n; i++) a[i][j] = v[j][i];
  for (long long i = 1; i < min(m, n) + 1; i++) {
    for (long long mask = 0; mask < 1 << n; mask++) {
      for (long long r = 0; r < n; r++)
        if (mask & (1 << r)) pre[i][mask] += a[r][i];
    }
    for (long long mask = 0; mask < 1 << n; mask++) {
      long long cur = mask;
      for (long long cyc = 0; cyc < n; cyc++) {
        long long c = cur >> 1;
        if (cur % 2) c += 1 << (n - 1);
        pre[i][mask] = max(pre[i][mask], pre[i][c]);
        cur = c;
      }
    }
  }
  for (long long i = 1; i < min(m, n) + 1; i++) {
    for (long long mask = 0; mask < 1 << n; mask++) {
      for (long long sub = mask; sub >= 0; sub = (sub - 1) & mask) {
        dp[i][mask] = max(dp[i][mask], dp[i - 1][sub] + pre[i][sub ^ mask]);
        if (sub == 0) break;
      }
    }
  }
  cout << dp[min(m, n)][(1 << n) - 1] << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long TESTS = 1;
  cin >> TESTS;
  while (TESTS--) {
    solve();
  }
  return 0;
}
