#!/usr/bin/env python3 import math def iterate(a, b, c): for a in range(a, a + 4): for b in range(b, b + 4): for c in range(c, c + 4): yield a, b, c yield max(b + 1, a), b, max(b + 2, c) yield max(b + 2, a), b, max(b + 1, c) yield a, max(a + 1, b), c yield a, max(c + 1, b), c def solve(a, b, c, x, y, z): ans = math.inf for na, nb, nc in iterate(a, b, c): if na != nc and ((na < nb and nb > nc) or (na > nb and nb < nc)): da = na - a db = nb - b dc = nc - c if a - db - dc >= 1 and b - dc - da >= 1 and c - da - db >= 1: ans = min(ans, y * da + z * db + x * dc) if math.isinf(ans): return -1 return ans for _ in range(int(input())): print(solve(*map(int, input().split())))