#include <bits/stdc++.h>
using namespace std;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) is >> x;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
  for (long long i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i != v.size() - 1) cout << endl;
  };
  return os;
}
template <typename T>
void Out(T x) {
  cout << x << endl;
}
template <typename T1, typename T2>
void Ans(bool first, T1 y, T2 n) {
  if (first)
    Out(y);
  else
    Out(n);
}
using vec = vector<long long>;
using mat = vector<vec>;
using Pii = pair<long long, long long>;
using PiP = pair<long long, Pii>;
using PPi = pair<Pii, long long>;
using bools = vector<bool>;
using pairs = vector<Pii>;
const long long mod = 1000000007;
signed main() {
  long long t;
  cin >> t;
  vec ans(t, 0);
  for (long long q = (0); q < (t); q++) {
    long long n, m;
    cin >> n >> m;
    mat a(n, vec(m));
    cin >> a;
    long long k = min(n, m);
    mat a0(n, vec(k));
    pairs p(m);
    for (long long j = (0); j < (m); j++) {
      long long mm = 0;
      for (long long i = (0); i < (n); i++) mm = max(mm, a[i][j]);
      p[j] = Pii(mm, j);
    }
    sort(p.begin(), p.end());
    reverse(p.begin(), p.end());
    for (long long j = (0); j < (k); j++) {
      long long j0 = p[j].second;
      for (long long i = (0); i < (n); i++) a0[i][j] = a[i][j0];
    }
    vec I(k, 0);
    while (I[0] == 0) {
      long long tmp = 0;
      for (long long i = (0); i < (n); i++) {
        long long mm = 0;
        for (long long j = (0); j < (k); j++)
          mm = max(mm, a0[(i + I[j]) % n][j]);
        tmp += mm;
      }
      ans[q] = max(ans[q], tmp);
      I[k - 1]++;
      for (long long i = (k)-1; i >= (1); i--) {
        if (I[i] == n) {
          I[i] = 0;
          I[i - 1]++;
        } else
          break;
      }
    }
  }
  Out(ans);
  return 0;
}
