#include <bits/stdc++.h>
using namespace std;
constexpr long long MOD = 1e9 + 7;
int a[14][2010];
int cost[1 << 12][14];
int dp[1 << 12][14];
void solve() {
  int n, m;
  scanf("%d%d", &n, &m);
  memset(cost, 0, sizeof(cost));
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      scanf("%d", &a[i][j]);
    }
  }
  vector<pair<int, int>> p;
  for (int i = 0; i < m; ++i) {
    int M = 0;
    for (int j = 0; j < n; ++j) {
      M = max(a[j][i], M);
    }
    p.push_back({M, i});
  }
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  m = min(m, n);
  int x[12];
  for (int i = 0; i < m; ++i) {
    int col = p[i].second;
    for (int shift = 0; shift < n; ++shift) {
      for (int j = 0; j < n; ++j) {
        x[j] = a[(j + shift) % n][col];
      }
      for (int s = 0; s < (1 << n); ++s) {
        int cur = 0;
        for (int k = 0; k < n; ++k) {
          if (s & (1 << k)) {
            cur += x[k];
          }
        }
        cost[s][i] = max(cost[s][i], cur);
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    for (int s = 0; s < (1 << n); ++s) {
      for (int t = s; t >= 0; t = (t - 1) & s) {
        dp[s][i] = max(dp[s][i], (i ? dp[t][i - 1] : 0) + cost[t ^ s][i]);
        if (t == 0) break;
      }
    }
  }
  printf("%d\n", dp[(1 << n) - 1][m - 1]);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}
