#include <bits/stdc++.h>
using namespace std;
template <typename T>
using vec = vector<T>;
auto _fast_io = []() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  return cout.tie(nullptr);
}();
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
  for (size_t i = 0; i < v.size(); ++i) {
    in >> v[i];
  }
  return in;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T>& v) {
  if (!v.empty()) {
    out << v.front();
    for (size_t i = 1; i < v.size(); ++i) {
      out << " " << v[i];
    }
  }
  return out;
}
template <typename T, size_t Size>
istream& operator>>(istream& in, array<T, Size>& v) {
  for (size_t i = 0; i < Size; ++i) {
    in >> v[i];
  }
  return in;
}
template <typename T, size_t Size>
ostream& operator<<(ostream& out, array<T, Size>& v) {
  if (!v.empty()) {
    out << v.front();
    for (size_t i = 1; i < v.size(); ++i) {
      out << " " << v[i];
    }
  }
  return out;
}
inline int64_t gcd(int64_t a, int64_t b) {
  if (a < b) std::swap(a, b);
  while (b != 0) {
    int64_t tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}
struct fraction {
  fraction() = default;
  fraction(int64_t nom, int64_t denom)
      : nom(nom), denom(denom), full_denom(denom) {
    normalize();
  }
  void normalize() {
    auto g = gcd(nom, denom);
    nom /= g;
    denom /= g;
  }
  int64_t nom;
  int denom;
  int full_denom;
};
inline bool operator==(const fraction& left, const fraction& right) {
  return left.nom == right.nom && left.denom == right.denom;
}
inline bool operator<(const fraction& left, const fraction& right) {
  auto left_w = left.nom * right.denom;
  auto right_w = right.nom * left.denom;
  return left_w < right_w ||
         left_w == right_w && left.full_denom < right.full_denom;
}
inline bool operator<(int64_t left, const fraction& right) {
  return left * right.denom < right.nom;
}
int64_t calculate_cost(int64_t x, int64_t y, int64_t c, int64_t cx, int64_t cy,
                       int s, int sx, int sy) {
  int64_t q_lower = 0;
  int64_t q_upper = std::numeric_limits<int64_t>::max();
  if (s * sx > 0)
    q_upper = min(q_upper, s * x);
  else
    q_lower = max(q_lower, s * x);
  if (s * sy > 0)
    q_upper = min(q_upper, s * y);
  else
    q_lower = max(q_lower, s * y);
  if (q_lower > q_upper) return -1;
  auto coef = c - s * sx * cx - s * sy * cy;
  if (coef >= 0) {
    return cx * sx * x + coef * q_lower + cy * sy * y;
  } else {
    return cx * sx * x + coef * q_upper + cy * sy * y;
  }
}
void run_test(size_t t) {
  int64_t x, y;
  int64_t c[7];
  cin >> x >> y;
  cin >> c[1] >> c[2] >> c[3] >> c[4] >> c[5] >> c[6];
  int64_t max_cost = std::numeric_limits<int64_t>::max();
  {
    auto cost = calculate_cost(x, y, c[1], c[6], c[2], 1, 1, 1);
    if (cost != -1) max_cost = min(max_cost, cost);
  }
  {
    auto cost = calculate_cost(x, y, c[1], c[6], c[5], 1, 1, -1);
    if (cost != -1) max_cost = min(max_cost, cost);
  }
  {
    auto cost = calculate_cost(x, y, c[1], c[3], c[2], 1, -1, 1);
    if (cost != -1) max_cost = min(max_cost, cost);
  }
  {
    auto cost = calculate_cost(x, y, c[1], c[3], c[5], 1, -1, -1);
    if (cost != -1) max_cost = min(max_cost, cost);
  }
  {
    auto cost = calculate_cost(x, y, c[4], c[6], c[2], -1, 1, 1);
    if (cost != -1) max_cost = min(max_cost, cost);
  }
  {
    auto cost = calculate_cost(x, y, c[4], c[6], c[5], -1, 1, -1);
    if (cost != -1) max_cost = min(max_cost, cost);
  }
  {
    auto cost = calculate_cost(x, y, c[4], c[3], c[2], -1, -1, 1);
    if (cost != -1) max_cost = min(max_cost, cost);
  }
  {
    auto cost = calculate_cost(x, y, c[4], c[3], c[5], -1, -1, -1);
    if (cost != -1) max_cost = min(max_cost, cost);
  }
  cout << max_cost << endl;
}
int main() {
  size_t T;
  cin >> T;
  for (size_t t = 1; t <= T; ++t) {
    run_test(t);
  }
}
