#define _USE_MATH_DEFINES #include using namespace std; const long long INF = LLONG_MAX; signed main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; int opr[3][3] = {{-1, -1, 0}, {0, -1, -1}, {-1, 0, -1}}; auto is_valid = [] (long long g[], long long h[]) -> bool { if (h[0] < g[0] || h[1] < g[1] || h[2] < g[2]) return false; if (g[0] <= 0 || g[1] <= 0 || g[2] <= 0) return false; if (g[0] == g[1] || g[1] == g[2] || g[2] == g[0]) return false; if (g[1] > g[0] && g[1] > g[2]) return true; if (g[1] < g[0] && g[1] < g[2]) return true; return false; }; auto calc = [&] (long long g[], long long h[], long long c[]) -> long long { long long gg[3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) { gg[0] = h[0]; gg[1] = h[1], gg[2] = h[2]; gg[0] += opr[0][0] * i + opr[1][0] * j + opr[2][0] * k; gg[1] += opr[0][1] * i + opr[1][1] * j + opr[2][1] * k; gg[2] += opr[0][2] * i + opr[1][2] * j + opr[2][2] * k; bool ok = true; for (int l = 0; l < 3; l++) ok &= (gg[l] == g[l]); if (ok) return c[0] * i + c[1] * j + c[2] * k; } return INF; }; auto solve = [&] () -> long long { long long h[3]; cin >> h[0] >> h[1] >> h[2]; long long c[3]; cin >> c[0] >> c[1] >> c[2]; long long t[] = {h[0], h[0] - 1, h[0] - 2, h[1], h[1] - 1, h[1] - 2, h[2], h[2] - 1, h[2] - 2}; long long g[3]; long long res = INF; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) for (int k = 0; k < 9; k++) { g[0] = t[i], g[1] = t[j], g[2] = t[k]; if (is_valid(g, h)) res = min(res, calc(g, h, c)); } return (res == INF ? -1 : res); }; while (t--) cout << solve() << '\n'; return 0; }