#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); if (ma - mi <= 5) { long long res = INF; for (int a = 0; a <= 10; ++a) { for (int b = 0; b <= 10; ++b) { for (int c = 0; c <= 10; ++c) { if (isok(A-a, B-b, C-c)) { chmin(res, (long long)(a*X + b*Y + c*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; if (isok(a, b, c)) { chmin(res, (A - a) * X + (B - b) * Y + (C - c) * Z); } } } } 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; } }