#include <bits/stdc++.h>
using namespace std;
int n, m;
int dp[4096], dp1[4096], vl[4096];
int val[15][2050];
int mx[2050];
int id[2050];
bool cmp(int a, int b) { return mx[a] > mx[b]; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &m);
    memset(mx, 0, sizeof(mx));
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; ++i)
      for (int j = 1; j <= m; ++j)
        scanf("%d", &val[i][j]), mx[j] = max(mx[j], val[i][j]);
    for (int i = 1; i <= m; ++i) id[i] = i;
    sort(id + 1, id + 1 + m, cmp);
    if (m > n) m = n;
    for (int j = 1; j <= m; ++j) {
      memcpy(dp1, dp, sizeof(dp));
      memset(vl, 0, sizeof(vl));
      int all = (1 << n) - 1;
      for (int s = 0; s <= all; ++s) {
        for (int t = 0; t < n; ++t) {
          int d = 0;
          for (int i = 0; i < n; ++i)
            if (s & 1 << i) d += val[(i + t) % n][id[j]];
          vl[s] = max(vl[s], d);
        }
        for (int S = s; S <= all; S = (S + 1) | s)
          dp[S] = max(dp[S], dp1[S ^ s] + vl[s]);
      }
    }
    printf("%d\n", dp[(1 << n) - 1]);
  }
}
