#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<deque<int>> v;
vector<int> g;
int f(int d) {
  if (d == g.size()) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int ax = 0;
      for (int j = 0; j < m; j++) {
        ax = max(ax, v[i][j]);
      }
      ans += ax;
    }
    return ans;
  }
  int cur = g[d];
  int ans = 0;
  deque<int> l;
  for (int i = 0; i < n; i++) {
    l.push_back(v[i][cur]);
  }
  ans = max(ans, f(d + 1));
  for (int i = 0; i < n; i++) {
    l.push_front(l.back());
    l.pop_back();
    for (int j = 0; j < n; j++) v[j][cur] = l[j];
    ans = max(ans, f(d + 1));
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n >> m;
    v.clear();
    v.resize(n);
    vector<pair<int, int>> l(m);
    for (auto &j : v) {
      j.resize(m);
      int ax = 0;
      int e = 0;
      for (auto &k : j)
        cin >> k, ax = max(ax, k), l[e].first = max(k, l[e].first),
                  l[e].second = e, e++;
    }
    sort(l.begin(), l.end());
    reverse(l.begin(), l.end());
    g.clear();
    g.resize(min(4, m));
    for (int j = 0; j < g.size(); j++) g[j] = l[j].second;
    cout << f(0) << "\n";
  }
  return 0;
}
