#include <bits/stdc++.h>
using namespace std;
int T;
int n, m;
int memo[2010][1 << 12];
int a[15][2010];
int mval[2010][1 << 12];
vector<int> nxt[1 << 12];
vector<int> cols;
int chk[2010];
void init() {
  for (int i = 0; i < (1 << 12); i++) {
    for (int j = 0; j < (1 << 12); j++) {
      if (i & j) continue;
      nxt[i].push_back(j);
    }
  }
}
int solve(int in, int mask) {
  if (in == cols.size() && mask == ((1 << n) - 1)) return 0;
  if (in == cols.size()) return -1e9 - 1;
  if (memo[in][mask] != -1) return memo[in][mask];
  int cur = 0;
  for (int nmask : nxt[mask]) {
    if ((mask & nmask) == 0) {
      cur = max(cur, solve(in + 1, (mask | nmask)) + mval[cols[in]][nmask]);
    }
  }
  return memo[in][mask] = cur;
}
bool cmp(pair<int, int> a, pair<int, int> b) { return a.first > b.first; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  init();
  cin >> T;
  while (T--) {
    cols.clear();
    memset(mval, 0, sizeof(mval));
    memset(memo, -1, sizeof(memo));
    memset(chk, 0, sizeof(chk));
    cin >> n >> m;
    vector<pair<int, int> > temp;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> a[i][j];
        temp.push_back(pair<int, int>(a[i][j], j));
      }
    }
    sort(temp.begin(), temp.end(), cmp);
    int Count = 0;
    for (auto x : temp) {
      int col = x.second;
      if (chk[col]) continue;
      chk[col] = 1;
      cols.push_back(col);
      Count++;
      if (Count == n) break;
    }
    for (int jd = 0; jd < cols.size(); jd++) {
      int j = cols[jd];
      for (int mask = 0; mask < (1 << n); mask++) {
        for (int f = 0; f < n; f++) {
          int temp = 0;
          for (int k = 0; k < n; k++) {
            if (mask & (1 << k)) {
              int val = a[(f + k) % n][j];
              temp += val;
            }
          }
          mval[j][mask] = max(mval[j][mask], temp);
        }
      }
    }
    cout << solve(0, 0) << "\n";
  }
}
