I,R,=input,range S,N,B,x=R(3),R(2),[0]*3,[0]*4 def J(): return list(map(int,I().split())) def U(): global a s,b,c=sum(x),1,0 for j in R(4):b*=x[j]>=0 for j in S:b*=s-x[j]<=B[(j+2)%3] if b: for j in R(4):c+=x[j]*X[j] a=max(a,c) def V(): for k in S:B[k]=A[k]if k!=j else y for i in S: x[i],s=0,B[(i+2)%3] for k in N:x[(i+1+k)%3]=s-B[(i+k)%3] x[3]=s-x[(i+1)%3]-x[(i+2)%3] U() for w in N: x[3],x[0]=w,(B[0]+B[1]-B[2]-w)//2 for k in N:x[1+k]=B[1-k]-x[0]-w U() for t in R(int(I())): A,X,a=J(),J(),0 for j in S: for y in R(A[j]+1):V() print(a)