#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 6;
const int M_MAX = 102;
int t;
int n, m;
int ma[N_MAX][M_MAX];
int mx1[M_MAX], mx2[M_MAX];
vector<int> v;
int mxmx2;
bool c1[N_MAX], c2[N_MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) cin >> ma[i][j];
    memset(mx1, 0, sizeof(mx1));
    memset(mx2, 0, sizeof(mx2));
    v.clear();
    mxmx2 = 0;
    for (int j = 1; j <= m; j++) {
      for (int i = 1; i <= n; i++) {
        if (ma[i][j] >= mx1[j]) {
          mx2[j] = mx1[j];
          mx1[j] = ma[i][j];
        } else
          mx2[j] = max(mx2[j], ma[i][j]);
      }
      v.push_back(mx1[j]);
      mxmx2 = max(mxmx2, mx2[j]);
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    int ans = 0;
    int sum = 0;
    if (n <= m) {
      for (int i = 1; i <= n; i++) sum += v[i - 1];
      ans = max(ans, sum);
    }
    if (n - 1 <= m) {
      sum = 0;
      for (int i = 1; i < n; i++) sum += v[i - 1];
      sum += mxmx2;
      ans = max(ans, sum);
    }
    for (int j = 1; j <= m; j++) {
      sum = 0;
      for (int i = 1; i <= n; i++) sum += ma[i][j];
      ans = max(ans, sum);
    }
    if (n == 4 && m >= 2) {
      for (int x = 1; x <= m; x++)
        for (int y = x + 1; y <= m; y++) {
          for (int a = 1; a <= n * 2; a++)
            for (int b = a + 1; b <= n * 2; b++)
              for (int c = b + 1; c <= n * 2; c++)
                for (int d = c + 1; d <= n * 2; d++) {
                  memset(c1, false, sizeof(c1));
                  memset(c2, false, sizeof(c2));
                  if (a <= n)
                    c1[a] = true;
                  else
                    c2[a - n] = true;
                  if (b <= n)
                    c1[b] = true;
                  else
                    c2[b - n] = true;
                  if (c <= n)
                    c1[c] = true;
                  else
                    c2[c - n] = true;
                  if (d <= n)
                    c1[d] = true;
                  else
                    c2[d - n] = true;
                  int s = 0;
                  for (int i = 1; i <= 4; i++)
                    if (c1[i] == true) s += ma[i][x];
                  for (int i = 1; i <= 4; i++)
                    if (c2[i] == true) s += ma[i][y];
                  bool ok = false;
                  for (int i = 1; i <= 4; i++) {
                    if (!(c1[1] && c2[1] || c1[2] && c2[2] || c1[3] && c2[3] ||
                          c1[4] && c2[4])) {
                      ok = true;
                      break;
                    }
                    int first = c2[1];
                    c2[1] = c2[2];
                    c2[2] = c2[3];
                    c2[3] = c2[4];
                    c2[4] = first;
                  }
                  if (ok == true) ans = max(ans, s);
                }
        }
    }
    cout << ans << "\n";
  }
  return 0;
}
