#include <bits/stdc++.h>
using namespace std;
int T, n, m, sum, tot;
struct P {
  int val, x, y;
} a[500];
struct R {
  int x, y;
} b[10];
bool cmp1(const P& a, const P& b) { return a.val > b.val; }
bool cmp2(const R& a, const R& b) { return a.y > b.y; }
bool Judge(int i, int j, int k, int u) {
  int num[5], tmp = 1;
  memset(num, 0, sizeof(num));
  b[1].x = a[i].x, b[1].y = a[i].y;
  b[2].x = a[j].x, b[2].y = a[j].y;
  b[3].x = a[k].x, b[3].y = a[k].y;
  b[4].x = a[u].x, b[4].y = a[u].y;
  sort(b + 1, b + 4 + 1, cmp2);
  num[1]++;
  for (int l = 2; l <= 4; l++)
    if (b[l].y == b[l - 1].y)
      num[tmp]++;
    else
      num[tmp + 1]++, tmp++;
  if (num[1] > num[2]) swap(num[1], num[2]);
  if (tmp != 2 || num[1] == 1 && num[2] == 3) return true;
  int t1 = abs(b[1].x - b[2].x), t2 = abs(b[3].x - b[4].x);
  if (t1 < t2) swap(t1, t2);
  if (t1 == 3 && t2 == 2 || t1 == 2 && t2 == 1) return false;
  return true;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    sum = tot = 0;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        scanf("%d", &a[++tot].val);
        a[tot].x = i;
        a[tot].y = j;
      }
    sort(a + 1, a + tot + 1, cmp1);
    if (n <= 3 || n == 4 && m == 1) {
      for (int i = 1; i <= n; i++) sum += a[i].val;
      printf("%d\n", sum);
      continue;
    }
    for (int i = 1; i <= 8; i++)
      for (int j = i + 1; j <= 8; j++)
        for (int k = j + 1; k <= 8; k++)
          for (int u = k + 1; u <= 8; u++)
            if (Judge(i, j, k, u))
              sum = max(sum, a[i].val + a[j].val + a[k].val + a[u].val);
    printf("%d\n", sum);
  }
}
