#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
const int M = 2e5 + 50;
int n, m;
int a[10][200];
int v[(1 << 4) + 1][200];
int dp[(1 << 4) + 1][200];
int* max(int* a, int* b) {
  int mx = *a, *mxi = a;
  while (a != b) {
    if (*a > mx) {
      mx = *a;
      mxi = a;
    }
    a++;
  }
  return mxi;
}
void solve(int i) {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      a[i + n][j] = a[i][j];
    }
  }
  memset(v, 0, sizeof(v));
  memset(dp, 0, sizeof(dp));
  int state = (1 << n) - 1;
  for (int j = 1; j <= m; j++) {
    for (int i = 0; i < n; i++) {
      for (int s = 1; s <= state; s++) {
        int tmp = 0;
        for (int k = 0; k < n; k++) {
          if ((s >> k) & 1) {
            tmp += a[i + k][j];
          }
        }
        if (tmp > v[s][j]) v[s][j] = tmp;
      }
    }
  }
  for (int j = 1; j <= m; j++) {
    for (int s = 0; s <= state; s++) {
      for (int lasts = 0; lasts <= state; lasts++) {
        if ((lasts & s) == 0) {
          dp[s | lasts][j] = max(dp[s | lasts][j], dp[lasts][j - 1] + v[s][j]);
        }
      }
    }
  }
  int ans = 0;
  for (int s = 0; s <= state; s++) ans = max(ans, dp[s][m]);
  printf("%d\n", ans);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
  return 0;
}
