#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1.0);
vector<pair<int, int> > occ[100005];
int a[4][104];
void solve() {
  for (int i = 1; i < 100000 + 1; ++i) {
    occ[i].clear();
  }
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      int x;
      cin >> x;
      a[i][j] = x;
      occ[x].push_back(make_pair(i, j));
    }
  }
  set<int> main_columns;
  for (int i = 100000; i >= 1; --i) {
    for (auto j : occ[i]) {
      if ((int)main_columns.size() < n) {
        main_columns.insert(j.second);
      }
    }
  }
  vector<int> cand_cols;
  for (int i : main_columns) cand_cols.push_back(i);
  int lim = 1;
  int ans = 0;
  for (int i = 0; i < (int)cand_cols.size(); ++i) lim *= n;
  for (int j = 0; j < lim; ++j) {
    vector<int> k(cand_cols);
    int ff = j;
    for (int i = 0; i < (int)cand_cols.size(); ++i) {
      k[i] = ff % n;
      ff /= n;
    }
    vector<vector<int> > new_matrix(n, vector<int>(cand_cols));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < (int)cand_cols.size(); ++j) {
        new_matrix[(i + k[j]) % n][j] = a[i][cand_cols[j]];
      }
    }
    int cur = 0;
    for (int i = 0; i < n; ++i) {
      int mx = 0;
      for (int j = 0; j < (int)cand_cols.size(); ++j) {
        mx = max(mx, new_matrix[i][j]);
      }
      cur += mx;
    }
    ans = max(ans, cur);
  }
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
