#include #include #include #include #include using namespace std; const long long INF = 1LL<<60; // (a, b, c) が門松列かどうか 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() { 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; long long sum = (a-A) + (b-B) + (c-C); // 操作回数 if (isok(a-sum, b-sum, c-sum)) { res = min(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; } }