#include <bits/stdc++.h>
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class A, class B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <class A, class B>
istream &operator>>(istream &is, pair<A, B> &p) {
  return is >> p.first >> p.second;
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    long long ans = LONG_LONG_MAX;
    long long X, Y;
    cin >> X >> Y;
    vector<long long> c(6);
    for (int i = 0; i < (int)(6); i++) cin >> c[i];
    swap(c[2], c[5]);
    for (int i = 0; i < (int)(2); i++)
      for (int j = 0; j < (int)(2); j++)
        for (int k = 0; k < (int)(2); k++) {
          long long c1 = c[3 * i];
          long long c2 = c[3 * j + 1];
          long long c3 = c[3 * k + 2];
          long long t = 0;
          long long l = 0LL, r = LONG_LONG_MAX;
          if (i == j) {
            t -= c2;
            if (i == 0)
              chmin(r, Y);
            else
              chmin(r, -Y);
          } else {
            t += c2;
            if (i == 0)
              chmax(l, Y);
            else
              chmax(l, -Y);
          }
          if (i == k) {
            t -= c3;
            if (i == 0)
              chmin(r, X);
            else
              chmin(r, -X);
          } else {
            t += c3;
            if (i == 0)
              chmax(l, X);
            else
              chmax(l, -X);
          }
          if (r < l) continue;
          long long a = 0;
          if (t >= 0)
            a = l;
          else
            a = r;
          long long s1 = (i == 1) ? -1 : 1;
          long long s2 = (j == i) ? -1 : 1;
          long long s3 = (k == i) ? -1 : 1;
          long long ss2 = (j == 1) ? -1 : 1;
          long long ss3 = (k == 1) ? -1 : 1;
          long long sum = 0;
          sum += a * c1;
          sum += (s2 * a + ss2 * Y) * c2;
          sum += (s3 * a + ss3 * X) * c3;
          chmin(ans, sum);
        }
    cout << ans << "\n";
  }
  return 0;
}
