#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 6, MOD = 1e9 + 7, mod = 998244353;
const long long INF = 2e17;
using vi = vector<long long>;
using vp = vector<pair<long long, long long>>;
mt19937 Flash_new_51(515151);
long long a[5000][2000], b[2000][2000];
long long ot = 0;
vector<pair<long long, long long>> ans;
vector<long long> kek;
long long t, n, m;
void ans1(long long pos) {
  if (pos < kek.size()) {
    vector<long long> mem;
    for (long long i = 0; i < n; i++) {
      long long x = kek[pos];
      mem.push_back(a[i][x]);
    }
    for (long long i = 0; i <= n; i++) {
      vector<long long> roflan;
      for (long long j = i; j < n; j++) roflan.push_back(mem[j]);
      for (long long j = 0; j < i; j++) roflan.push_back(mem[j]);
      for (long long j = 0; j < n; j++) {
        b[j][pos] = roflan[j];
      }
      ans1(pos + 1);
    }
    return;
  }
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    long long maxx = -1;
    for (long long j = 0; j < pos; j++) {
      maxx = max(maxx, b[i][j]);
    }
    sum += maxx;
  }
  ot = max(sum, ot);
  return;
}
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> t;
  while (t--) {
    cin >> n >> m;
    ot = 0;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) cin >> a[i][j];
    }
    ans.clear(), kek.clear();
    for (long long j = 0; j < m; j++) {
      long long maxx = -1;
      for (long long i = 0; i < n; i++) {
        maxx = max(maxx, a[i][j]);
      }
      ans.push_back({maxx, j});
    }
    cout << endl;
    sort(ans.rbegin(), ans.rend());
    for (long long i = 0; i < INT_MAX; i++) {
      if (i >= n || i >= m) break;
      kek.push_back(ans[i].second);
    }
    ans1(0);
    cout << ot << "\n";
  }
}
