#include <bits/stdc++.h>
using namespace std;
void print() {
  if constexpr (0) cout << '\n';
}
template <typename T, typename... Ts>
void print(const T& value, const Ts&... values) {
  if constexpr (0) cout << value << ' ', print(values...);
}
vector<vector<int> > a, b;
int n, m;
int dp[2000][(1 << 12)];
int mx[2000][(1 << 12)];
int sum[2000][(1 << 12)];
void prep() {
  for (int lev = (int)0; lev <= (int)m - 1; lev++) {
    fill(mx[lev], mx[lev] + (1 << n), 0);
    fill(dp[lev], dp[lev] + (1 << n), 0);
    fill(sum[lev], sum[lev] + (1 << n), 0);
  }
  for (int lev = (int)0; lev <= (int)m - 1; lev++) {
    for (int mask = (int)1; mask <= (int)(1 << n) - 1; mask++) {
      int i = __builtin_ctzll(mask);
      sum[lev][mask] = sum[lev][mask ^ (1 << i)] + a[lev][i];
    }
  }
  for (int lev = (int)0; lev <= (int)m - 1; lev++) {
    for (int mask = (int)1; mask <= (int)(1 << n) - 1; mask++) {
      for (int shift = (int)0; shift <= (int)n - 1; shift++) {
        int nmask = ((mask << shift) | (mask >> (n - shift))) & ((1 << n) - 1);
        mx[lev][mask] = max(mx[lev][mask], sum[lev][nmask]);
      }
    }
  }
}
void reformat() {
  a.assign(n, vector<int>(m, 0));
  for (int i = (int)0; i <= (int)n - 1; i++)
    for (int j = (int)0; j <= (int)m - 1; j++) cin >> a[i][j];
  b.assign(m, vector<int>(n, 0));
  for (int i = (int)0; i <= (int)n - 1; i++)
    for (int j = (int)0; j <= (int)m - 1; j++) b[j][i] = a[i][j];
  a = b;
  vector<pair<int, int> > all;
  for (int i = (int)0; i <= (int)m - 1; i++)
    all.push_back({*max_element(a[i].begin(), a[i].end()), i});
  sort(all.begin(), all.end(), greater<pair<int, int> >());
  b = vector<vector<int> >();
  for (int i = (int)0; i <= (int)min(n, (int)all.size()) - 1; i++)
    b.push_back(a[all[i].second]);
  a = b;
  n = a[0].size();
  m = a.size();
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    reformat();
    prep();
    for (int i = (int)0; i <= (int)m - 1; i++) {
      if (i == 0) {
        for (int mask = (int)0; mask <= (int)(1 << n) - 1; mask++)
          dp[i][mask] = mx[i][mask];
      } else {
        for (int mask = (int)0; mask <= (int)(1 << n) - 1; mask++) {
          for (int mask2 = mask;; mask2 = (mask2 - 1) & mask) {
            dp[i][mask] =
                max(dp[i][mask], dp[i - 1][mask ^ mask2] + mx[i][mask2]);
            if (mask2 == 0) break;
          }
        }
      }
    }
    cout << dp[m - 1][(1 << n) - 1] << '\n';
  }
}
