#include <bits/stdc++.h>
using namespace std;
void Main();
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int x;
  cin >> x;
  while (~cin.peek()) Main(), cin.get();
  return 0;
}
int c[2000][1 << 12], dp[2001][1 << 12], vis[1 << 12], g[1 << 12];
void Main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(n, vector<int>(m));
  vector<pair<int, pair<int, int>>> b;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      b.push_back({a[i][j], {i, j}});
    }
  }
  sort(b.begin(), b.end(), greater<pair<int, pair<int, int>>>());
  vector<int> ok(m);
  int cnt = 0;
  for (int i = 0; i < n * m; i++) {
    if (!ok[b[i].second.second]) {
      ok[b[i].second.second] = 1;
      cnt++;
      if (cnt == n) break;
    }
  }
  for (int i = 0; i < n; i++) g[1 << i] = i;
  memset(c, sizeof(c), 0);
  for (int i = 0; i < m; i++) {
    for (int j = 1; j < (1 << n); j++) {
      int low = j & -j;
      c[i][j] = c[i][j ^ low] + a[g[low]][i];
    }
  }
  vector<int> buf(n);
  for (int i = 0; i < m; i++) {
    memset(vis, 0, sizeof(vis));
    for (int j = 0; j < (1 << n); j++) {
      if (vis[j]) continue;
      int jj = j;
      int maxv = 0;
      for (int r = 0; r < n; r++) {
        maxv = max(maxv, c[i][jj]);
        buf[r] = jj;
        jj = jj >> 1 | (jj & 1) << n - 1;
      }
      for (int r = 0; r < n; r++) {
        c[i][buf[r]] = maxv;
        vis[buf[r]] = 1;
      }
    }
  }
  memset(dp, 0, sizeof(dp));
  auto updmax = [](int &a, int b) {
    if (b > a) a = b;
  };
  for (int i = 0; i < m; i++) {
    if (ok[i]) {
      for (int j = 0; j < (1 << n); j++) {
        for (int k = j;; k = (k - 1) & j) {
          updmax(dp[i + 1][j], dp[i][j ^ k] + c[i][k]);
          if (k == 0) break;
        }
      }
    } else {
      memcpy(dp[i + 1], dp[i], sizeof(dp[i]));
    }
  }
  cout << dp[m][(1 << n) - 1] << endl;
}
