#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int N = 101;
int mx[N], Q[5][N], n, m;
vector<pii> S;
void read() {
  cin >> n >> m;
  fill(mx + 1, mx + 1 + max(4, m), 0);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= max(4, m); ++j) {
      if (j <= m)
        cin >> Q[i][j];
      else
        Q[i][j] = 0;
      mx[j] = max(mx[j], Q[i][j]);
    }
  }
  m = max(4, m);
  S.clear();
  for (int i = 1; i <= m; ++i) S.emplace_back(mx[i], i);
  sort(S.rbegin(), S.rend());
  while (S.size() > 5) S.pop_back();
}
const int inf = 1e9;
int f(int x) { return (x - 1) % n + 1; }
int q[5];
void solve() {
  int ans = 0;
  for (int a = 1; a <= m; ++a) {
    for (int b = a + 1; b <= m; ++b) {
      for (int c = b + 1; c <= m; ++c) {
        int MX = -1;
        for (auto p : S) {
          if (p.second != a && p.second != b && p.second != c) {
            MX = p.first;
            break;
          }
        }
        assert(MX != -1);
        for (int B = 0; B < n; ++B) {
          for (int C = 0; C < n; ++C) {
            int mn = inf, sum = 0;
            for (int i = 1; i <= n; ++i) {
              q[i] = max({Q[i][a], Q[(i + B - 1) % n + 1][b],
                          Q[(i + C - 1) % n + 1][c]});
              sum += q[i];
              mn = min(mn, q[i]);
            }
            ans = max(ans, sum);
            ans = max(ans, sum - mn + MX);
          }
        }
      }
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  int tt = 1;
  cin >> tt;
  while (tt--) {
    read();
    solve();
    cout << "\n";
  }
}
