#include <bits/stdc++.h>
namespace IO {
char ch = 0;
template <class T>
inline T ReadInt() {
  T x(0);
  bool f(1);
  while (ch < '0' || '9' < ch) {
    if (ch == '-') {
      f = 0;
    }
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return f ? x : -x;
}
inline void ReadT(int &x) { x = ReadInt<int>(); }
inline void ReadT(long long &x) { x = ReadInt<long long>(); }
inline void ReadT(char &s) {
  for (; isspace(ch) && (~ch); ch = getchar())
    ;
  s = ch;
  ch = 0;
}
inline void ReadT(char *s) {
  int len(0);
  for (; isspace(ch) && (~ch); ch = getchar())
    ;
  for (; !isspace(ch) && (~ch); ch = getchar()) {
    s[len++] = ch;
  }
  s[len] = '\0';
  ch = 0;
}
template <typename T>
inline void Read(T &t) {
  ReadT(t);
}
template <typename T, typename... Args>
void Read(T &t, Args &...args) {
  ReadT(t);
  Read(args...);
}
template <typename T>
void WriteInt(T x) {
  if (x < 0) {
    printf("-");
    x = -x;
  }
  if (9 < x) {
    WriteInt(x / 10);
  }
  putchar(x % 10 + '0');
}
template <typename T>
inline void WriteT(T x);
template <>
inline void WriteT<long long>(long long x) {
  WriteInt(x);
}
template <>
inline void WriteT<int>(int x) {
  WriteInt(x);
}
template <>
inline void WriteT<char *>(char *s) {
  int len(strlen(s));
  while (len--) {
    putchar(*s++);
  }
}
template <>
inline void WriteT<const char *>(const char *s) {
  while (*s) {
    putchar(*s++);
  }
}
template <>
inline void WriteT<char>(char x) {
  putchar(x);
}
template <typename T>
inline void Write(T t) {
  WriteT(t);
}
template <typename T, typename... Args>
void Write(T t, Args... args) {
  WriteT(t);
  Write(args...);
}
template <typename T>
inline void Writeln(T t) {
  WriteT(t);
  puts("");
}
template <typename T, typename... Args>
inline void Writeln(T t, Args... args) {
  WriteT(t);
  Write(args...);
  puts("");
}
inline void Writeln() { puts(""); }
const char NUMBER[36] = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
                         '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                         'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
template <typename T>
void WriteK(T x, int p) {
  if (x < 0) {
    printf("-");
    x = -x;
  }
  if (p <= x) {
    WriteK(x / p, p);
  }
  putchar(NUMBER[x % p]);
}
}  // namespace IO
using namespace IO;
using namespace std;
const int MAXN = 4e3 + 100;
int n, m;
int arr[MAXN][20];
int f[MAXN][MAXN], dp[MAXN][MAXN];
struct Line {
  int id, val;
} line[MAXN];
bool Cmp(Line a, Line b) { return b.val < a.val; }
void Work() {
  Read(n, m);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      Read(arr[j][i]);
    }
  }
  for (int i = 1; i <= m; ++i) {
    int val = 0;
    for (int j = 1; j <= n; ++j) {
      val = max(val, arr[i][j]);
    }
    line[i].id = i;
    line[i].val = val;
  }
  sort(line + 1, line + 1 + m, Cmp);
  m = min(n, m);
  int all = (1 << n) - 1;
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j <= all; ++j) {
      f[i][j] = 0;
      dp[i][j] = 0;
    }
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j <= all; ++j) {
      for (int k = 1; k <= n; ++k) {
        if (j & (1 << (k - 1))) {
          f[i][j] += arr[line[i].id][k];
        }
      }
    }
    for (int j = 0; j <= all; ++j) {
      for (int k = 1; k <= n; ++k) {
        f[i][j] =
            max(f[i][j], f[i][((j << (k - 1)) | (j >> (n - k + 1))) & all]);
      }
    }
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j <= all; ++j) {
      dp[i][j] = dp[i - 1][j];
      for (int t = j; t; t = (t - 1) & j) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j ^ t] + f[i][t]);
      }
    }
  }
  Writeln(dp[m][all]);
}
int main() {
  int t;
  scanf("%d", &t);
  for (int i = 1; i <= t; ++i) {
    Work();
  }
  return 0;
}
