#include typedef unsigned int u32; const int N = 100054; int R, n; int cnt[N]; int f[N][2][3][3], g[N][2][3][3]; inline int min(const int x, const int y) {return x < y ? x : y;} inline void up(int &x, const int y) {x < y ? x = y : 0;} void work() { int i, j, k, l, c, d, ans = 0; scanf("%d%d", &R, &n); memset(cnt, 0, (R + 1) << 2); for (i = 0; i <= 3 * n; ++i) scanf("%d", &j), ++cnt[j]; memset(f, 192, sizeof f), f[0][0][0][0] = 0; for (i = 1; i <= R; ++i) for (j = 0; j < 2; ++j) for (k = 0; k < 3; ++k) for (l = 0; l < 3; ++l) if ((c = f[i - 1][j][k][l]) >= 0) { // printf("f[%d][%d][%d][%d] = %d\n", i-1, j, k, l, c); if ((u32)(d = cnt[i] - k - l) < 3) up(f[i][j][l][d], c + k); if ((u32)(d = cnt[i] - k - l - 3) < 3) up(f[i][j][l][d], c + k + 1); if (!j && (u32)(d = cnt[i] - k - l - 2) < 3) up(f[i][1][l][d], c + k); } memset(g, 192, sizeof g), g[R + 1][0][0][0] = 0; for (i = R; i; --i) for (j = 0; j < 2; ++j) for (k = 0; k < 3; ++k) for (l = 0; l < 3; ++l) if ((c = g[i + 1][j][k][l]) >= 0) { // printf("g[%d][%d][%d][%d] = %d\n", i+1, j, k, l, c); if ((u32)(d = cnt[i] - k - l) < 3) up(g[i][j][l][d], c + k); if ((u32)(d = cnt[i] - k - l - 3) < 3) up(g[i][j][l][d], c + k + 1); if (!j && (u32)(d = cnt[i] - k - l - 2) < 3) up(g[i][1][l][d], c + k); } for (i = 1; i <= R; ++i) { int l, m, r, ki, A = cnt[i] + 1; bool succ = false; // printf("%d:\n", i); for (l = 0; l <= A && !succ; ++l) for (m = 0; l + m <= A && !succ; ++m) for (r = 0; l + m + r <= A && !succ; ++r) { ki = (A - (l + m + r)) / 3; if (l + m + r + ki * 3 == A) { // printf("[%d] l = %d, m = %d, r = %d, ki = %d => ", succ, l, m, r, ki); succ |= f[i - 1][0][l][m] + g[i + 1][1][r][m] + l + m + r + ki == n; succ |= f[i - 1][1][l][m] + g[i + 1][0][r][m] + l + m + r + ki == n; // printf("[%d]\n", succ); } if (l + m + r + ki * 3 + 2 == A) { // printf("[%d] l = %d, m = %d, r = %d, ki = %d, OO => ", succ, l, m, r, ki); succ |= f[i - 1][0][l][m] + g[i + 1][0][r][m] + l + m + r + ki == n; // printf("[%d]\n", succ); } } ans += succ; } printf("%d\n", ans); } int main() { int T; for (scanf("%d", &T); T; --T) work(); return 0; }