#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using E = array<int, 3>;
using ve = vector<E>;
template <typename A, typename B>
ostream& operator<<(ostream& out, const pair<A, B>& p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
template <typename Ch, typename Tr, typename C>
basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>& out, const C& xs) {
  out << "[";
  for (const auto& x : xs) out << x << ", ";
  out << "]";
  return out;
}
template <typename T>
void smax(T& val, const T newv) {
  val = max(val, newv);
}
int solve(const vvi& mat) {
  const int r = mat.size();
  const int c = mat[0].size();
  assert(r <= 12);
  assert(c <= 12);
  const unsigned maskcnt = 1 << c;
  vi dp(maskcnt, 0);
  for (int i = 0; i < r; ++i) {
    vi newdp = dp;
    vi masksum(1 << c);
    for (unsigned mask = 0; mask < maskcnt; ++mask) {
      vi sums(c);
      for (int j = 0; j < c; ++j) {
        if (!(mask & (1 << j))) continue;
        for (int off = 0; off < c; ++off) sums[off] += mat[i][(j + off) % c];
      }
      masksum[mask] = *max_element(sums.begin(), sums.end());
    }
    vi masks(c, 0);
    int oldmask = 0;
    int newmask = 0;
    while (true) {
      int j = 0;
      do {
        ++masks[j];
        if (masks[j] == 3) {
          masks[j] = 0;
          newmask ^= (1 << j);
        } else if (masks[j] == 1)
          oldmask |= (1 << j);
        else {
          oldmask ^= (1 << j);
          newmask |= (1 << j);
        }
        if (masks[j] != 0) break;
        ++j;
      } while (j < c);
      if (j == c) break;
      smax(newdp[oldmask | newmask], dp[oldmask] + masksum[newmask]);
    }
    dp = std::move(newdp);
  }
  return *max_element(dp.begin(), dp.end());
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vvi as(m, vi(n));
    vpii colmax(m);
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        colmax[j].second = j;
        cin >> as[j][i];
        smax(colmax[j].first, as[j][i]);
      }
    }
    sort(colmax.begin(), colmax.end(), greater<pii>());
    vvi mat(min(m, n), vi(n));
    for (int i = 0; i < min(m, n); ++i) mat[i] = as[colmax[i].second];
    cout << solve(mat) << "\n";
  }
}
