#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> second) {
  os << "{ ";
  for (auto s : second) os << s << " ";
  return os << "}";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R>
ostream& operator<<(ostream& os, map<L, R> M) {
  os << "{ ";
  for (auto m : M) os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << 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...);
}
const long long maxn = 1e6 + 100;
const long long MaxN = 1e5 + 100;
const long long mod = 1e9 + 7;
const long long INF = 1e16;
inline long long fast_expo(long long base, long long power,
                           long long modulo = mod) {
  base %= modulo;
  if (base < 0) base += modulo;
  long long x = base, cnt = power, ans = 1;
  while (cnt) {
    if (cnt & 1) ans = (ans * x) % modulo;
    x = (x * x) % modulo;
    cnt >>= 1;
  }
  return ans;
}
inline long long inv(long long base, long long modulo = mod) {
  return fast_expo(base, modulo - 2, modulo);
}
long long a[20][2005];
vector<long long> pos[2005];
long long idx[2005];
bool cmp(long long i, long long j) {
  for (long long k = 0; k < (long long)pos[i].size(); k++) {
    if (k >= pos[j].size()) return false;
    if (pos[i][k] == pos[j][k]) continue;
    return pos[i][k] < pos[j][k];
  }
  return false;
}
long long sol(vector<long long> ids, long long n) {
  long long res = 0;
  long long msk = 1;
  for (long long i = 0; i < (long long)ids.size() - 1; i++) {
    msk *= n;
  }
  for (long long k = 0; k < (long long)msk; k++) {
    vector<long long> r(n, 0);
    long long K = k;
    for (long long j = 0; j < (long long)ids.size() - 1; j++) {
      long long rot = K % n;
      K /= n;
      for (long long z = 0; z < (long long)n; z++) {
        r[(z + rot) % n] = max(r[(z + rot) % n], a[z][ids[j]]);
      }
    }
    for (long long z = 0; z < (long long)n; z++) {
      r[z] = max(r[z], a[z][ids.back()]);
    }
    long long b = 0;
    for (long long z = 0; z < (long long)n; z++) b += r[z];
    res = max(res, b);
  }
  return res;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < (long long)n; i++) {
    for (long long j = 0; j < (long long)m; j++) {
      cin >> a[i][j];
    }
  }
  for (long long j = 0; j < (long long)m; j++) {
    pos[j].clear();
    for (long long i = 0; i < (long long)n; i++) pos[j].push_back(a[i][j]);
    idx[j] = j;
    sort((pos[j]).begin(), (pos[j]).end());
    reverse((pos[j]).begin(), (pos[j]).end());
  }
  sort(idx, idx + m, cmp);
  vector<long long> ids;
  for (long long i = m - 1; i >= 0 && ids.size() < 9; --i) {
    ids.push_back(idx[i]);
  }
  cout << (sol(ids, n)) << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
