#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
  if (b < a) a = b;
  return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 15;
long long dp[N][1 << 12];
long long mxval[N][1 << 12];
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<vector<long long>> cols(m);
  for (long long i = 0; i < n; ++i) {
    for (long long j = 0; j < m; ++j) {
      long long x;
      cin >> x;
      cols[j].push_back(x);
    }
  }
  sort((cols).begin(), (cols).end(),
       [&](vector<long long>& a, vector<long long>& b) {
         return *max_element((a).begin(), (a).end()) >
                *max_element((b).begin(), (b).end());
       });
  while ((long long)((cols).size()) > n) {
    cols.pop_back();
  }
  m = (long long)((cols).size());
  memset(dp, 0, sizeof dp);
  memset(mxval, 0, sizeof mxval);
  for (long long i = 0; i < m; ++i) {
    for (long long mask = 0; mask < 1 << n; ++mask) {
      for (long long s = 0; s < n; ++s) {
        long long sum = 0;
        for (long long j = 0; j < n; ++j) {
          if (mask >> j & 1) {
            sum += cols[i][(s + j) % n];
          }
        }
        amax(mxval[i][mask], sum);
      }
    }
  }
  for (long long i = 0; i < m; ++i) {
    for (long long mask = 0; mask < 1 << n; ++mask) {
      amax(dp[i + 1][mask], dp[i][mask]);
      long long rem_mask = ((1 << n) - 1) ^ mask;
      for (long long submask = rem_mask; submask > 0;
           submask = (submask - 1) & rem_mask) {
        amax(dp[i + 1][mask ^ submask], dp[i][mask] + mxval[i][submask]);
      }
    }
  }
  cout << dp[m][(1 << n) - 1] << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
