#include using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() using u128=__int128_t; using ll=long long; typedef pair pii; const int INF=1e9+7; struct edge{int to,cap,cost,rev;}; int V=8; const int MAX=10; vector G[MAX]; int h[MAX]; int dist[MAX]; int prevv[MAX],preve[MAX]; void add_edge(int from,int to,int cap,int cost){ G[from].push_back((edge){to,cap,cost,(int)G[to].size()}); G[to].push_back((edge){from,0,-cost,(int)G[from].size()-1}); } int min_cost_flow(int s,int t,int f){ int res=0; fill(h,h+V,0); while(f>0){ priority_queue,greater> que; fill(dist,dist+V,INF); dist[s]=0; que.push(pii(0,s)); while(!que.empty()){ pii p=que.top(); que.pop(); int v=p.second; if(dist[v]0 && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){ dist[e.to]=dist[v]+e.cost+h[v]-h[e.to]; prevv[e.to]=v; preve[e.to]=i; que.push(pii(dist[e.to],e.to)); } } } if(dist[t]==INF) return -1; rep(v,V) h[v]+=dist[v]; int d=f; for(int v=t;v!=s;v=prevv[v]) d=min(d,G[prevv[v]][preve[v]].cap); f-=d; res+=d*h[t]; for(int v=t;v!=s;v=prevv[v]){ edge &e=G[prevv[v]][preve[v]]; e.cap-=d; G[v][e.rev].cap+=d; } } return res; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int T; cin>>T; while(T--){ vector A(3),B(3); rep(i,3) cin>>A[i]; rep(i,3) cin>>B[i]; ll ma=0; rep(i,3) ma+=min(A[i],B[(i+1)%3]); rep(i,MAX){ G[i].clear(); h[i]=0; dist[i]=0; prevv[i]=0; preve[i]=0; } int s=6,t=7; rep(i,3) add_edge(s,i,A[i],0); rep(i,3) add_edge(i+3,t,B[i],0); rep(i,3) rep(j,3){ if((i+1)%3==j) continue; add_edge(i,j+3,INF,0); } add_edge(s,t,INF,1); int mi=min_cost_flow(s,t,A[0]+A[1]+A[2]); cout<