#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
constexpr int32_t mod = 1009, m1 = 1e9 + 123, m2 = 1e9 + 321;
static const int base = uniform_int_distribution<int>(300, (int)1e9 + 122)(rng);
static inline int add(int a, int b, int Mod) {
  return ((long long)a + b) % Mod;
}
static inline int sub(int a, int b, int Mod) {
  return (((long long)a - b) % Mod + Mod) % Mod;
}
static inline int mul(int a, int b, int Mod) { return 1ll * a * b % Mod; }
int t, n, m, arr[5][110];
vector<pair<int, int>> v[100100];
int brute(int i, vector<vector<int>> vec) {
  if (i == vec[0].size()) {
    int ret = 0;
    for (int i = 0; i < n; i++) {
      int mx = -1;
      for (int j = 0; j < vec[i].size(); j++) mx = max(mx, vec[i][j]);
      ret += mx;
    }
    return ret;
  }
  int ret = brute(i + 1, vec);
  for (int j = 0; j < n; j++) {
    int cur = vec[n - 1][i];
    for (int k = n - 1; k > 0; k--) vec[k][i] = vec[k - 1][i];
    vec[0][i] = cur;
    ret = max(ret, brute(i + 1, vec));
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> t;
  while (t--) {
    cin >> n >> m;
    vector<int> mx(m, -1);
    set<int, greater<int>> s;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++)
        cin >> arr[i][j], mx[j] = max(mx[j], arr[i][j]), s.insert(arr[i][j]);
    }
    for (int j = 0; j < m; j++) {
      bool bo[] = {0, 0};
      int cnt = 0;
      for (int i = 0; i < n; i++) {
        if (arr[i][j] == mx[j]) bo[i % 2] = 1, cnt++;
      }
      if ((bo[0] ^ bo[1]) && cnt > 1) {
        v[mx[j]].push_back({1, j});
      } else
        v[mx[j]].push_back({0, j});
    }
    for (auto x : s) sort(v[x].begin(), v[x].end());
    vector<vector<int>> vec(n);
    for (auto x : s) {
      if (vec[0].size() == n) break;
      for (int j = 0; j < v[x].size(); j++) {
        for (int k = 0; k < n; k++) {
          vec[k].push_back(arr[k][v[x][j].second]);
        }
        if (vec[0].size() == n) break;
      }
    }
    for (auto x : s) v[x].clear();
    int ans = brute(0, vec);
    cout << ans << '\n';
  }
  return 0;
}
