inf = 10**30 # b > c > a def calc1(a,b,c,x,y,z): l = [a,b,c] count = [0,0,0] if a >= c: count[2] += (a-c+1)*z c = a+1 if c >= b: count[1] += (c-b+1)*y b = c+1 for i in range(3): for j in range(3): if i == j: continue l[j] -= count[i] if min(l) > 0: return sum(count) return inf # c > a > b def calc2(a,b,c,x,y,z): l = [a,b,c] count = [0,0,0] if b >= a: count[0] += (b-a+1)*x a = b+1 if a >= c: count[2] += (a-c+1)*z c = a+1 for i in range(3): for j in range(3): if i == j: continue l[j] -= count[i] if min(l) > 0: return sum(count) return inf t = int(input()) for _ in range(t): a,b,c,x,y,z = map(int,input().split()) x,y,z = z,x,y ans = min(calc1(a,b,c,x,y,z),calc2(a,b,c,x,y,z),calc1(c,b,a,z,y,x),calc2(c,b,a,z,y,x)) if ans == inf: ans = -1 print(ans)