#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
int a[100][100];
void solve() {
  int n, m;
  cin >> n >> m;
  if (n < 4 || m == 1) {
    vector<int> v(n * m);
    for (int i = 0; i < n * m; i++) cin >> v[i];
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    int ans = 0;
    for (int i = 0; i < n; i++) ans += v[i];
    cout << ans << '\n';
    return;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) cin >> a[i][j];
  }
  vector<pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) v.emplace_back(i, j);
  }
  sort(v.begin(), v.end(), [](pair<int, int> p1, pair<int, int> p2) {
    return (a[p1.first][p1.second] > a[p2.first][p2.second]);
  });
  int rans = 0;
  vector<int> id = {0, 1, 2, 3, 4, 5, 6, 7};
  do {
    vector<int> c[100];
    for (int i = 0; i < 4; i++) c[v[id[i]].second].push_back(v[id[i]].first);
    vector<int> s2;
    for (int i = 0; i < m; i++) {
      if (c[i].size() == 2) s2.push_back(i);
    }
    if (s2.size() != 2) {
      int ans = 0;
      for (int i = 0; i < 4; i++) {
        ans += a[v[id[i]].first][v[id[i]].second];
      }
      rans = max(ans, rans);
    } else if ((c[s2[0]][0] + c[s2[0]][1] + c[s2[1]][0] + c[s2[1]][1]) % 2 ==
               0) {
      int ans = 0;
      for (int i = 0; i < 4; i++) {
        ans += a[v[id[i]].first][v[id[i]].second];
      }
      rans = max(ans, rans);
    }
  } while (next_permutation(id.begin(), id.end()));
  cout << rans << '\n';
  return;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
