#include <bits/stdc++.h>
using namespace std;
int dp[2][12][12][(1 << 12)], vis[2][12][12][(1 << 12)], two[12], tmp[12][2005],
    id, n, m;
vector<int> arr[2005];
int val_bit(int mask, int bit) { return (mask & two[bit]) > 0; }
int cycle_left(int mask) {
  int val = val_bit(mask, n - 1);
  mask -= val * two[n - 1];
  mask <<= 1;
  mask += val;
  return mask;
}
int cycle_right(int mask) {
  int val = val_bit(mask, 0);
  mask >>= 1;
  mask += val * two[n - 1];
  return mask;
}
int solve(int mask, int col, int move, bool take) {
  if (col == m) return 0;
  if (move == n) return solve(mask, col + 1, 0, 0);
  int &ret = dp[take][col][move][mask];
  if (vis[take][col][move][mask] == id) return ret;
  vis[take][col][move][mask] = id;
  ret = 0;
  if (take) {
    if (move == 0)
      return ret = solve(mask, col + 1, 0, 0);
    else {
      int c1 = solve(cycle_right(mask), col, (move + 1) % n, 1);
      int c2 = 0;
      if (val_bit(mask, 0) == 0)
        c2 = solve(cycle_right(mask | 1), col, (move + 1) % n, 1) +
             arr[col][move];
      return ret = max(c1, c2);
    }
  } else {
    int c1 = solve(cycle_right(mask), col, move + 1, 0);
    int c2 = solve(cycle_right(mask), col, 1 % n, 1);
    if (val_bit(mask, 0) == 0)
      c2 = max(c2, solve(cycle_right(mask | 1), col, 1 % n, 1) + arr[col][0]);
    return ret = max(c1, c2);
  }
}
bool cmp(vector<int> &v1, vector<int> &v2) {
  int mx1 = 0, mx2 = 0;
  for (auto &x : v1) mx1 = max(mx1, x);
  for (auto &x : v2) mx2 = max(mx2, x);
  return mx1 > mx2;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  two[0] = 1;
  for (int i = 1; i < 12; i++) two[i] = two[i - 1] * 2;
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m;
    id++;
    for (int i = 0; i < 2005; i++) arr[i].clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) cin >> tmp[i][j];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) arr[i].push_back(tmp[j][i]);
    }
    sort(arr, arr + m, cmp);
    m = min(m, 12);
    cout << solve(0, 0, 0, 0) << '\n';
  }
  return 0;
}
