#include <bits/stdc++.h>
const long long Inf = 1e18;
const long double eps = 1e-7;
long long LINF = (long long)2e18;
using namespace std;
long long mod = 1e9 + 7;
long long mod5 = 1e9 + 9;
long long mod3 = 998244353;
long long mod4 = 1000003;
long long mod2 = 1e9 + 123;
const int MAXN = 10100000;
const int INF = 1000000000;
vector<vector<int> > a;
int n, m;
vector<pair<int, int> > mx;
int rec(int pos, vector<int> mxs) {
  if (pos == m) {
    int ss = 0;
    for (long long(i) = 0; (i) < n; (i)++) {
      ss += mxs[i];
    }
    return ss;
  }
  int cur = 0;
  int ss = 0;
  for (long long(i) = 0; (i) < n; (i)++) {
    cur += mxs[i];
    ss += mxs[i];
  }
  int ans = cur;
  for (long long(i) = 0; (i) < n; (i)++) {
    cur = ss;
    vector<int> lol = mxs;
    for (long long(j) = 0; (j) < n; (j)++) {
      if (a[(i + j) % n][mx[pos].second] > mxs[j]) {
        cur += a[(i + j) % n][mx[pos].second] - mxs[j];
        lol[j] = a[(i + j) % n][mx[pos].second];
      }
    }
    ans = max(ans, rec(pos + 1, lol));
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(20);
  int tt;
  cin >> tt;
  while (tt--) {
    cin >> n >> m;
    a.clear();
    a.resize(n, vector<int>(m));
    for (long long(i) = 0; (i) < n; (i)++) {
      for (long long(j) = 0; (j) < m; (j)++) {
        cin >> a[i][j];
      }
    }
    mx.clear();
    for (long long(i) = 0; (i) < m; (i)++) {
      int cur = -1;
      for (long long(j) = 0; (j) < n; (j)++) {
        cur = max(cur, a[j][i]);
      }
      mx.push_back({cur, i});
    }
    sort((mx).rbegin(), (mx).rend());
    mx.resize(min(4, m));
    m = min(m, 4);
    vector<int> kek(n, 0);
    cout << rec(0, kek) << '\n';
  }
  return 0;
}
