#include <bits/stdc++.h>
using namespace std;
long long infy = 1e30;
void solve() {
  long long x, y, c[8], ax, ay;
  cin >> x >> y;
  ax = abs(x);
  ay = abs(y);
  for (long long i = 1; i <= 6; i++) cin >> c[i];
  c[7] = c[1];
  c[0] = c[6];
  for (long long i = 1; i <= 6; i++) c[i] = min(c[i], c[i - 1] + c[i + 1]);
  c[7] = c[1];
  c[0] = c[6];
  long long xy[7][2] = {{1, 0},   {1, 1},  {0, 1}, {-1, 0},
                        {-1, -1}, {0, -1}, {1, 0}},
            ans = infy;
  for (long long i = 1; i <= 6; i++) {
    long long x1, x2, y1, y2, temp;
    x1 = xy[i - 1][0];
    y1 = xy[i - 1][1];
    x2 = xy[i][0];
    y2 = xy[i][1];
    long long z1 = ((x1 * y2) - (x2 * y1));
    long long z2 = ((x * y2) - (y * x2));
    long long z3 = ((x2 * y1) - (y2 * x1));
    long long z4 = ((x * y1) - (y * x1));
    if (z1 == 0 || z3 == 0) continue;
    long long n1 = (z2 / z1);
    long long n2 = (z4 / z3);
    temp = ((n1 * c[i - 1]) + (n2 * c[i]));
    if (n1 >= 0 && n2 >= 0) {
      ans = min(ans, temp);
    }
  }
  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
