#include <bits/stdc++.h>
using namespace std;
long long int maxx(long long int a, long long int b, long long int c,
                   long long int d) {
  return max(a, max(b, max(c, d)));
}
int main() {
  std::ios::sync_with_stdio(false);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    long long int a[n + 1][m + 4];
    for (long long int i = 1; i <= n; i++) {
      for (long long int j = 1; j <= m; j++) cin >> a[i][j];
    }
    for (long long int i = 1; i <= n; i++)
      for (long long int j = m + 1; j <= m + 3; j++) a[i][j] = 0;
    vector<pair<long long int, long long int> > ve;
    for (long long int j = 1; j <= m + 3; j++) {
      long long int val = 0;
      for (long long int i = 1; i <= n; i++) {
        val = max(val, a[i][j]);
      }
      ve.push_back({val, j});
    }
    sort(ve.begin(), ve.end());
    reverse(ve.begin(), ve.end());
    vector<long long int> id;
    long long int szz = ve.size();
    for (long long int i = 0; i < min(n, m); i++) {
      id.push_back(ve[i].second);
    }
    id.push_back(m + 1);
    id.push_back(m + 2);
    id.push_back(m + 3);
    szz = n;
    long long int d1, d2, d3, d4;
    d1 = d2 = d3 = d4 = 0;
    long long int ans = 0;
    long long int b[25][25];
    for (long long int i = 0; i < 4; i++) {
      long long int j = id[i];
      for (long long int k = 1; k <= n; k++) {
        b[k - 1][i + 1] = a[k][j];
      }
    }
    for (long long int j = 0; j < szz; j++) {
      for (long long int k = 0; k < szz; k++) {
        for (long long int w = 0; w < szz; w++) {
          for (long long int e = 0; e < szz; e++) {
            d1 = maxx(b[(j) % n][1], b[k % n][2], b[w % n][3], b[e % n][4]);
            d2 = maxx(b[(j + 1) % n][1], b[(k + 1) % n][2], b[(w + 1) % n][3],
                      b[(e + 1) % n][4]);
            d3 = maxx(b[(j + 2) % n][1], b[(k + 2) % n][2], b[(w + 2) % n][3],
                      b[(e + 2) % n][4]);
            d4 = maxx(b[(j + 3) % n][1], b[(k + 3) % n][2], b[(w + 3) % n][3],
                      b[(e + 3) % n][4]);
            if (n <= 3) d4 = 0;
            if (n <= 2) d3 = 0;
            if (n <= 1) d2 = 0;
            ans = max(ans, d1 + d2 + d3 + d4);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
