#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  if (0) return;
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  if (0) return;
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
inline void INP();
const long long N = 3e6 + 7, M = 1000000007, inf = INT_MAX;
const long long INF = LLONG_MAX;
long long n, Q, m, val;
long long mat[14][3001];
vector<long long> col;
void getans(long long id, vector<long long> res) {
  if (id == col.size()) {
    long long sum = 0;
    for (auto i : res) sum += i;
    val = max(val, sum);
    return;
  }
  long long c = col[id];
  for (long long k = 0; k < (long long)n; k++) {
    vector<long long> ans = res;
    for (long long i = 0; i < (long long)n; i++) {
      ans[(i + k) % n] = max(ans[(i + k) % n], mat[i][c]);
    }
    getans(id + 1, ans);
  }
}
void solve() {
  cin >> n >> m;
  val = 0;
  col.clear();
  vector<long long> vec;
  for (long long i = 0; i < (long long)n; i++)
    for (long long j = 0; j < (long long)m; j++)
      cin >> mat[i][j], vec.push_back(mat[i][j]);
  sort(vec.rbegin(), vec.rend());
  multiset<long long> st;
  for (long long i = 0; i < (long long)n; i++) st.insert(vec[i]);
  set<pair<long long, long long> > st1;
  for (long long j = 0; j < (long long)m; j++) {
    long long x = 0;
    for (long long i = 0; i < (long long)n; i++) x = max(x, mat[i][j]);
    if (st.find(x) == st.end())
      st1.insert({x, j});
    else {
      st.erase(st.find(x));
      col.push_back(j);
    }
  }
  long long x = st.size();
  while (x and !st1.empty()) {
    pair<long long, long long> p = *st1.rbegin();
    col.push_back(p.second);
    st1.erase(p);
    x--;
  }
  vector<long long> res(n, 0);
  getans(0, res);
  cout << val << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  INP();
  long long test = 1;
  cin >> test;
  while (test--) solve();
}
inline void INP() {}
