#include //#include //#include //#include //#include //#include //#include //#include //#include //#include //#include //#include #define X first #define Y second #define PB push_back #define MP make_pair #define EB emplace_back #define mset(var,val) memset(var,val,sizeof(var)) #define IOS ios::sync_with_stdio(false);cin.tie(0),cout.tie(0) #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) //using namespace __gnu_pbds; //using namespace __gnu_cxx; using namespace std; namespace ___ { #ifdef local #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0) void err() { cout << endl; } template class T, typename t, typename... Args> void err(T a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); } template void err(T a, Args... args) { cout << a << ' '; err(args...); } #else #define dbg(...) #endif //#ifdef local //typedef long long LL; //#else //typedef __int128 LL; //#endif // local int _ = 0; void testcase() { cout << "Case " << (++_) << ": "; } } template inline bool scan(T&ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-' and (c<'0' or c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0' and c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1; } template inline void out(T x) { if(x>9) out(x/10); putchar(x%10+'0'); } using namespace ___; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; typedef pair pii; typedef pair pll; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3fLL; const double PI = acos(-1.0); const double eps = 1e-6; const int mod = 998244353; const int lim = 1e3; const int N = 2000+10; const int M = 1e6+10; #define lson (rt<<1) #define rson (rt<<1|1) ll qpow(ll a, ll b) { ll ans = 1; for(;b;b>>=1){ if(b&1) ans = ans*a%mod; a=a*a%mod; } return ans; } struct Edge { int from, to, cap, flow, cost; Edge(int u, int v, int c, int f, int w) : from(u), to(v), cap(c), flow(f), cost(w) {} }; const int maxn = 100; struct MCMF { int n, m; vector edges; vector G[maxn]; int inq[maxn]; ll d[maxn]; ll p[maxn]; ll a[maxn]; void init(int n) { this->n = n; for (int i = 0; i < n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int cap, int cost) { edges.emplace_back(from, to, cap, 0, cost); edges.emplace_back(to, from, 0, 0, -cost); m = edges.size(); G[from].push_back(m - 2); G[to].push_back(m - 1); } bool BellmanFord(int s, int t, int& flow, ll& cost) { for (int i = 0; i < n; i++) d[i] = inf; memset(inq, 0, sizeof(inq)); d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = inf; queue q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = 0; for (int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if (e.cap > e.flow && d[e.to] > d[u] + e.cost) { d[e.to] = d[u] + e.cost; p[e.to] = G[u][i]; a[e.to] = min(a[u], (ll)e.cap - e.flow); if (!inq[e.to]) { q.push(e.to); inq[e.to] = 1; } } } } if (d[t] == inf) return false; flow += a[t]; cost += (ll)d[t] * (ll)a[t]; for (int u = t; u != s; u = edges[p[u]].from) { edges[p[u]].flow += a[t]; edges[p[u] ^ 1].flow -= a[t]; } return true; } int MincostMaxflow(int s, int t, ll& cost) { int flow = 0; cost = 0; while (BellmanFord(s, t, flow, cost)); return flow; } }mcmf; map mp; string ss[] = {"012","021","102","120","201","210"}; string str; void init() { rep(i,6) { mp[ss[i]] = i; // dbg(ss[i]); } } int cnt[10]; int s[10]; int t[10]; int a[10]; void work(){ int n; cin >> n; rep(i,3)cin>>a[i]; mset(cnt,0); rep(i,n) { cin >> str; cnt[mp[str]]++; } int S = 0, T = 1; int idx = 2; rep(i,3){ s[i] = idx++; } rep(i,6) { t[i] = idx++; } mcmf.init(idx); rep(i,3) { mcmf.AddEdge(S, s[i], a[i], 0); // rep(j,6) { // int x = ss[j][i]-'0'; //// dbg(j,i,x); // mcmf.AddEdge(s[i], t[j], inf, -x); // } } rep(i,6) { rep(j,3) { int x = ss[i][j]-'0'; mcmf.AddEdge(s[x], t[i], inf, -(3-j)); } mcmf.AddEdge(t[i], T, cnt[i], 0); } ll cost = 0; int f = mcmf.MincostMaxflow(S, T, cost); // dbg(f); cout << -cost << "\n"; } int main() { #ifdef local freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); #endif // local IOS; init(); int t; cin>>t; // for(; t--;) work(); // return 0; }