#include using namespace std; const int INF=0x3f3f3f3f; const int N=(int) 1e4+20; struct MCMF { struct Edge { int from,to; int cap,flow,cost; Edge() {} Edge(int _from,int _to,int _cap,int _flow,int _cost): from(_from),to(_to),cap(_cap),flow(_flow),cost(_cost) {} }; int n,m,s,t; vector edge; vector G[N]; bool inq[N]; int d[N],p[N],fc[N]; void init(int _n,int _s,int _t) { n=_n; s=_s; t=_t; m=0; edge.clear(); for(int i=0;i<=n;i++) G[i].clear(); } void add_edge(int from,int to,int cap,int cost) { edge.emplace_back(Edge(from,to,cap,0,cost)); edge.emplace_back(Edge(to,from,0,0,-cost)); m+=2; G[from].emplace_back(m-2); G[to].emplace_back(m-1); } bool spfa(int &flow,int &cost) { // cout <<"stick" < Q; d[s]=0; inq[s]=1; p[s]=0; fc[s]=INF; Q.emplace(s); while(!Q.empty()) { int x=Q.front(); Q.pop(); inq[x]=0; for(int i=0;ie.flow && d[e.to]>d[x]+e.cost) { d[e.to]=d[x]+e.cost; p[e.to]=G[x][i]; fc[e.to]=min(fc[x],e.cap-e.flow); if(!inq[e.to]) { Q.emplace(e.to); inq[e.to]=1; } } } } if(d[t]==INF) return 0; flow+=fc[t]; cost+=fc[t]*d[t]; int x=t; while(x!=s) { edge[p[x]].flow+=fc[t]; edge[p[x]^1].flow-=fc[t]; x=edge[p[x]].from; } return 1; } int Mincost() { int flow=0, cost=0; while(spfa(flow,cost)) ; return cost; } }mcf; int main(void) { // freopen("e.in","r",stdin); int T; scanf("%d",&T); while (T--) { int n,a[3],num[6]; scanf("%d%d%d%d",&n,&a[0],&a[1],&a[2]); for (int i=0;i<6;i++) num[i]=0; for (int i=0;is[2])]++; } // for (int i=0;i<6;i++) printf("%d ",num[i]); puts(""); mcf.init(11,1,11); for (int i=0;i<3;i++) { mcf.add_edge(1,i+2,a[i],0); } int rk[3]; for (int i=0;i<3;i++) rk[i]=i; for (int i=0;i<6;i++) { // for (int j=0;j<3;j++) printf("%d ",rk[j]); puts(""); for (int j=0;j<3;j++) { mcf.add_edge(rk[j]+2,i+5,INF,j-3); } next_permutation(rk,rk+3); } for (int i=0;i<6;i++) { mcf.add_edge(i+5,11,num[i],0); } int ans = -mcf.Mincost(); printf("%d\n",ans); } return 0; }