#include <bits/stdc++.h>
using namespace std;
const int maxn = 2010;
struct node {
  int arr[14], mx;
} h[maxn];
bool cmp(const node &x, const node &y) { return x.mx > y.mx; }
int mp[14][maxn];
int dp[14][1 << 14], n, m;
int C[14][1 << 14];
void Cal_init() {
  for (int S = 1; S < (1 << n); S++) {
    int a[14], tot = 0;
    for (int i = 0; i < n; i++) {
      if (S & (1 << i)) {
        a[tot++] = i + 1;
      }
    }
    for (int id = 1; id <= m; id++) {
      int ans = 0;
      for (int pos = 0; pos < n; pos++) {
        int tmp = 0;
        for (int j = 0; j < tot; j++) {
          int p = a[j] + pos;
          if (p > n) p -= n;
          tmp += h[id].arr[p];
        }
        ans = max(tmp, ans);
      }
      C[id][S] = ans;
    }
  }
}
int Cal(int id, int S) { return C[id][S]; }
int main() {
  int t;
  scanf("%d", &t);
  int cas = 1;
  while (t--) {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%d", &mp[i][j]);
      }
    }
    for (int i = 1; i <= m; i++) {
      int mx = 0;
      for (int j = 1; j <= n; j++) {
        h[i].arr[j] = mp[j][i];
        mx = max(mx, mp[j][i]);
      }
      h[i].mx = mx;
    }
    sort(h + 1, h + m + 1, cmp);
    memset(dp, 0, sizeof(dp));
    if (n < m) m = n;
    Cal_init();
    for (int i = 1; i <= m; i++) {
      for (int S = 0; S < (1 << n); S++) {
        dp[i][S] = dp[i - 1][S];
        for (int S2 = S; S2; S2 = (S2 - 1) & S)
          dp[i][S] = max(dp[i][S], dp[i - 1][S - S2] + Cal(i, S2));
      }
    }
    printf("%d\n", dp[m][(1 << n) - 1]);
  }
  return 0;
}
