#include <bits/stdc++.h>
using namespace std;
ifstream in;
ofstream out;
const long long kk = 1000;
const long long ml = kk * kk;
const long long mod = ml * kk + 7;
const long long inf = ml * ml * ml + 7;
long long n, i, j;
vector<long long> m;
bool viv = false;
long long power(long long k, long long st) {
  long long res = 1;
  while (st--) res *= k;
  return res;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<vector<long long>> f(n, vector<long long>(m, 0));
  for (auto& l : f)
    for (auto& e : l) cin >> e;
  vector<long long> v(m);
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) v[j] = max(v[j], f[i][j]);
  vector<pair<long long, long long>> tos;
  for (long long i = 0; i < m; i++) tos.push_back({-v[i], i});
  sort(tos.begin(), tos.end());
  vector<long long> ind;
  for (long long i = 0; i < min(n, m); i++) ind.push_back(tos[i].second);
  if (viv) {
    cout << "Cols: ";
    for (auto i : ind) cout << i << ' ';
    cout << endl;
  }
  long long k = min(n, m);
  long long ans = 0;
  for (long long i = 0; i < power(n, k); i++) {
    vector<long long> sh;
    long long x = i;
    for (long long i = 0; i < k; i++) sh.push_back(x % n), x /= n;
    vector<long long> res(n, 0);
    for (auto id : ind) {
      for (long long i = 0; i < n; i++)
        res[i] = max(res[i], f[(i + sh.back()) % n][id]);
      sh.pop_back();
    }
    long long lans = 0;
    for (auto i : res) lans += i;
    ans = max(ans, lans);
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
