#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template ostream& operator << (ostream &s, pair P) { return s << '<' << P.first << ", " << P.second << '>'; } template ostream& operator << (ostream &s, vector P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, vector > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template ostream& operator << (ostream &s, set P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template ostream& operator << (ostream &s, map P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } template vector make_vec(size_t a) { return vector(a); } template auto make_vec(size_t a, Ts... ts) { return vector(ts...))>(a, make_vec(ts...)); } template typename enable_if::value == 0>::type fill(T &t, const V &v) { t = v; } template typename enable_if::value != 0>::type fill(T &t, const V &v){ for (auto &e : t) fill(e, v); } const long long INF = 1LL<<60; bool isok(long long a, long long b, long long c) { if (a < 1 || b < 1 || c < 1) return false; if (a == b) return false; if (b == c) return false; if (c == a) return false; if (a < b && b < c) return false; if (a > b && b > c) return false; return true; } long long A, B, C, X, Y, Z; long long solve() { if (isok(A, B, C)) return 0; long long mi = min(min(A, B), C); long long ma = max(max(A, B), C); long long res = INF; vector alt; for (int it = 0; it <= 2; ++it) { alt.push_back(A + it); alt.push_back(B + it); alt.push_back(C + it); } if (ma - mi <= 5) { long long res = INF; for (int x = 0; x <= 10; ++x) { for (int y = 0; y <= 10; ++y) { for (int z = 0; z <= 10; ++z) { if (isok(A-x-z, B-x-y, C-y-z)) { chmin(res, (long long)(x*X + y*Y + z*Z)); } } } } if (res < INF) return res; else return -1; } else { long long res = INF; vector alt; for (int it = 0; it <= 2; ++it) { alt.push_back(A + it); alt.push_back(B + it); alt.push_back(C + it); } for (auto a : alt) { for (auto b : alt) { for (auto c : alt) { if (A > a) continue; if (B > b) continue; if (C > c) continue; // A -> a, B -> b, C -> c long long sum = (a-A) + (b-B) + (c-C); if (isok(a-sum, b-sum, c-sum)) { chmin(res, (a-A) * Y + (b-B) * Z + (c-C) * X); } } } } if (res < INF) return res; else return -1; } } int main() { int T; cin >> T; for (int _ = 0; _ < T; ++_) { cin >> A >> B >> C >> X >> Y >> Z; cout << solve() << endl; } }