#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int>(m));
    vector<pair<int, pair<int, int>>> mxs;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> a[i][j];
        mxs.emplace_back(a[i][j], make_pair(i, j));
      }
    }
    sort(mxs.rbegin(), mxs.rend());
    vector<int> p;
    for (int i = 0; p.size() < n && i < mxs.size(); i++) {
      int x = mxs[i].second.second;
      bool here = false;
      for (int y : p) {
        here |= x == y;
      }
      if (!here) {
        p.push_back(x);
      }
    }
    int k = p.size();
    vector<int> ro(k, 0);
    int ans = 0;
    while (true) {
      int cur = 0;
      for (int i = 0; i < n; i++) {
        int onThisLine = 0;
        for (int j = 0; j < k; j++) {
          onThisLine = max(onThisLine, a[(i + ro[j]) % n][p[j]]);
        }
        cur += onThisLine;
      }
      ans = max(ans, cur);
      int i = k - 1;
      while (i >= 0 && ro[i] == n - 1) {
        ro[i] = 0;
        i--;
      }
      if (i >= 0) {
        ro[i]++;
      }
      bool allZeros = true;
      for (int x : ro) {
        if (x != 0) {
          allZeros = false;
        }
      }
      if (allZeros) {
        break;
      }
    }
    cout << ans << endl;
  }
}
