#include <bits/stdc++.h>
using namespace std;
void solve() {
  int64_t n, m;
  cin >> n >> m;
  int64_t a[n][m], at[m][n];
  for (int64_t i = 0; i < n; i++) {
    for (int64_t j = 0; j < m; j++) {
      cin >> a[i][j];
      at[j][i] = a[i][j];
    }
  }
  vector<pair<int64_t, int64_t>> cols;
  for (int64_t i = 0; i < m; i++) {
    cols.emplace_back(*max_element(at[i], at[i] + n), i);
  }
  sort(begin(cols), end(cols), greater<pair<int64_t, int64_t>>());
  m = min(n, m);
  int64_t p[m];
  for (int64_t i = 0; i < m; i++) {
    p[i] = cols[i].second;
  }
  int64_t sz = 1 << n;
  int64_t dp[m + 1][sz];
  memset(dp, 0, sizeof(dp));
  for (int64_t i = 0; i < m; i++) {
    for (int64_t z = 0; z < n; z++) {
      int64_t ndp[n + 1][sz];
      memset(ndp, 0, sizeof(ndp));
      for (int64_t mask = 0; mask < sz; mask++) {
        ndp[0][mask] = dp[i][mask];
      }
      for (int64_t j = 0; j < n; j++) {
        for (int64_t mask = 0; mask < sz; mask++) {
          if ((mask >> j) & 1) {
            int64_t nmask = mask ^ (1 << j);
            ndp[j + 1][nmask] =
                max(ndp[j][nmask], ndp[j][mask] + at[p[i]][(z + j) % n]);
            ndp[j + 1][mask] = ndp[j][mask];
          }
        }
      }
      for (int64_t mask = 0; mask < sz; mask++) {
        dp[i + 1][mask] = max(dp[i + 1][mask], ndp[n][mask]);
        assert(dp[i + 1][mask] >= dp[i][mask]);
      }
    }
  }
  cout << dp[m][0] << "\n";
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int64_t t;
  cin >> t;
  while (t--) {
    solve();
  }
}
