#include <bits/stdc++.h>
using namespace std;
long long a[13][2001];
long long n, m, ans = 0;
vector<pair<long long, pair<long long, long long> > > ve;
vector<long long> fix;
long long dfs(long long i, array<long long, 4> arr) {
  if (i == n || i == m) {
    ans = max(ans, arr[0] + arr[1] + arr[2] + arr[3]);
    return 0;
  }
  for (long long j = 0; j < n; j++) {
    array<long long, 4> pass = arr;
    for (long long k = 0; k < n; k++) {
      pass[(k + j) % n] = max(pass[(k + j) % n], a[k + 1][fix[i]]);
    }
    dfs(i + 1, pass);
  }
  return 0;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    ans = 0;
    cin >> n >> m;
    ve.clear();
    fix.clear();
    for (long long i = 1; i < n + 1; i++)
      for (long long j = 1; j < m + 1; j++) {
        cin >> a[i][j];
        ve.push_back({a[i][j], {i, j}});
      }
    sort(ve.begin(), ve.end(),
         greater<pair<long long, pair<long long, long long> > >());
    set<long long> se;
    for (auto i : ve) {
      se.insert(i.second.second);
      if (se.size() >= n) break;
    }
    for (auto i : se) {
      fix.push_back(i);
    }
    array<long long, 4> arr = {0, 0, 0, 0};
    dfs(0, arr);
    cout << ans << '\n';
  }
  return 0;
}
