#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <class T>
inline bool setmin(T &a, T b) {
  if (a > b) return a = b, 1;
  return 0;
}
template <class T>
inline bool setmax(T &a, T b) {
  if (a < b) return a = b, 1;
  return 0;
}
template <class T>
inline T fast(T a, T b, T mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
template <class T>
inline T russian(T a, T b, T mod) {
  long long res = 0;
  while (b) {
    if (b & 1) res = (res + a) % mod;
    a = (a + a) % mod;
    b >>= 1;
  }
  return res;
}
template <class T>
istream &operator>>(istream &os, vector<T> &container) {
  for (auto &u : container) os >> u;
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &container) {
  for (auto &u : container) os << u << " ";
  return os;
}
template <typename T>
inline T gcd(T a, T b) {
  while (b) swap(a %= b, b);
  return a;
}
const long long T = 1 << 20;
const long long INF = 1e9 + 7;
const long long mod = 998244353;
const long long BIG_INF = 1e18 + 7;
const long long N = 1e6 + 7;
const int LOG = 17;
int n, m, k, q;
vector<vector<int> > G2;
int dp[13][1 << 13];
bool cmp(int a, int b) { return G2[a] > G2[b]; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    memset(dp, 0, sizeof dp);
    cin >> n >> m;
    vector<vector<int> > G(m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        int temp;
        cin >> temp;
        assert(temp > 0);
        G[j].push_back(temp);
      }
    G2 = G;
    vector<int> id;
    for (int i = 0; i < m; i++) id.push_back(i);
    for (auto &u : G2) sort((u).rbegin(), (u).rend());
    sort((id).begin(), (id).end(), cmp);
    for (int i = 1; i <= min(n, m); i++)
      for (int j = 0; j < (1 << n); j++)
        for (int I = j; I >= 0; I = (I - 1) & j) {
          int nowe = (j ^ I);
          for (int II = 0; II < n; II++) {
            int teraz = 0;
            for (int J = 0; J < n; J++)
              if (nowe & (1 << J)) teraz += G[id[i - 1]][(J + II) % n];
            setmax(dp[i][j], dp[i - 1][I] + teraz);
          }
          if (I == 0) break;
        }
    cout << dp[min(n, m)][(1 << n) - 1] << '\n';
  }
}
