#include using std::cin; using std::cout; typedef long long ll; const int cost[3][6] = { {0, 0, 1, 2, 1, 2}, {1, 2, 0, 0, 2, 1}, {2, 1, 2, 1, 0, 0}, }; int n, A[3], B[6]; namespace CF { #define ad(x) ((x - 1 ^ 1) + 1) const int N = 15, M = 100, INF = 0x7f7f7f7f; struct edge { int u, v, c, f; edge (int u0 = 0, int v0 = 0, int c0 = 0, int f0 = 0) : u(u0), v(v0), c(c0), f(f0) {} } e[M]; int V = 2, E = 0, si = 1, ti = 2, flow, cost; int first[N], next[M]; int dep[N], cur[N], que[M << 1]; bool in_que[N], used[N]; inline void reset() {for (; E; --E) first[e[E].u] = 0; V = 0, si = 1, ti = 2;} inline void addedge(int u, int v, int c, int f) { e[++E] = edge(u, v, c, f), next[E] = first[u], first[u] = E; e[++E] = edge(v, u, -c), next[E] = first[v], first[v] = E; } bool bfs() { int h = M, t = h + 1, i, x, y; memset(dep, 127, sizeof dep); que[h] = ti, dep[ti] = 0, in_que[ti] = true; for (; h < t; ) { x = que[h++], in_que[x] = false; for (i = first[x]; i; i = next[i]) if (dep[y = e[i].v] > dep[x] - e[i].c && e[ad(i)].f) { dep[y] = dep[x] - e[i].c; if (!in_que[y]) in_que[y] = true, (dep[y] >= dep[que[h]] ? que[t++] : que[--h]) = y; } } return dep[si] < INF; } int dfs(int x, int lim) { int a, c, f = 0; if (x == ti || !lim) return lim; used[x] = true; for (int &i = cur[x]; i; i = next[i]) if (dep[e[i].v] == dep[x] - e[i].c && e[i].f && !used[e[i].v]) { a = std::min(lim - f, e[i].f); c = dfs(e[i].v, a); e[i].f -= c, e[ad(i)].f += c; if ((f += c) == lim) return f; } return f; } int Dinic() { int f; for (cost = flow = 0; bfs(); ) { memcpy(cur, first, sizeof cur); memset(used, 0, sizeof used); flow += f = dfs(si, INF); cost += dep[si] * f; } return cost; } } void work() { int i, j; std::string s; cin >> n >> A[0] >> A[1] >> A[2], memset(B, 0, 24); for (i = 0; i < n; ++i) { cin >> s; if (s == "012") ++B[0]; else if (s == "021") ++B[1]; else if (s == "102") ++B[2]; else if (s == "120") ++B[3]; else if (s == "201") ++B[4]; else if (s == "210") ++B[5]; else throw "gg"; } CF::reset(); CF::addedge(1, 3, 0, A[0]); CF::addedge(1, 4, 0, A[1]); CF::addedge(1, 5, 0, A[2]); CF::addedge(6, 2, 0, B[0]); CF::addedge(7, 2, 0, B[1]); CF::addedge(8, 2, 0, B[2]); CF::addedge(9, 2, 0, B[3]); CF::addedge(10, 2, 0, B[4]); CF::addedge(11, 2, 0, B[5]); for (i = 0; i < 3; ++i) for (j = 0; j < 6; ++j) CF::addedge(3 + i, 6 + j, 1 + cost[i][j], INT_MAX); cout << 4 * n - CF::Dinic() << '\n'; } int main() { int T; std::ios::sync_with_stdio(false), cin.tie(NULL); for (cin >> T; T; --T) work(); return 0; }