#include <bits/stdc++.h>
using namespace std;
int t;
int n, m;
int a[5][101];
int x[21];
int y[21];
int val[21];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    for (int i = 0; i < 21; i++) x[i] = y[i] = val[i] = -1;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) cin >> a[i][j];
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        for (int k = 1; k <= 20; k++) {
          if (a[i][j] >= val[k]) {
            for (int z = 20; z > k; z--) {
              val[z] = val[z - 1];
              x[z] = x[z - 1];
              y[z] = y[z - 1];
            }
            val[k] = a[i][j];
            x[k] = i;
            y[k] = j;
            break;
          }
        }
      }
    int res = 0;
    if (n <= 3) {
      for (int i = 1; i <= n; i++) res += val[i];
      cout << res << '\n';
      continue;
    }
    for (int i = 1; i <= 20; i++)
      for (int j = i + 1; j <= 20; j++)
        for (int k = j + 1; k <= 20; k++)
          for (int l = k + 1; l <= 20; l++) {
            if (y[i] == y[j] && y[k] == y[l]) {
              if ((abs(x[i] - x[j]) == 1 || abs(x[i] - x[j]) == 3) &&
                  (abs(x[k] - x[l]) == 2))
                continue;
              if ((abs(x[k] - x[l]) == 1 || abs(x[k] - x[l]) == 3) &&
                  (abs(x[i] - x[j]) == 2))
                continue;
            }
            if (y[i] == y[k] && y[j] == y[l]) {
              if ((abs(x[i] - x[k]) == 1 || abs(x[i] - x[k]) == 3) &&
                  (abs(x[j] - x[l]) == 2))
                continue;
              if ((abs(x[j] - x[l]) == 1 || abs(x[j] - x[l]) == 3) &&
                  (abs(x[i] - x[k]) == 2))
                continue;
            }
            if (y[i] == y[l] && y[j] == y[k]) {
              if ((abs(x[i] - x[l]) == 1 || abs(x[i] - x[l]) == 3) &&
                  (abs(x[k] - x[j]) == 2))
                continue;
              if ((abs(x[k] - x[j]) == 1 || abs(x[k] - x[j]) == 3) &&
                  (abs(x[i] - x[l]) == 2))
                continue;
            }
            res = max(res, val[i] + val[j] + val[k] + val[l]);
          }
    cout << res << '\n';
  }
}
