#include <bits/stdc++.h>
using namespace std;
int read() {
  int x, f = 1;
  char ch;
  while (ch = getchar(), ch < '0' || ch > '9')
    if (ch == '-') f = -1;
  x = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - 48;
  return x * f;
}
unsigned long long a, b, c, d, e, f, s;
int main() {
  int t, p, q;
  t = read();
  while (t--) {
    s = 0;
    p = read();
    q = read();
    scanf("%lld %lld %lld %lld %lld %lld", &a, &b, &c, &d, &e, &f);
    a = min(a, b + f);
    b = min(b, a + c);
    c = min(c, b + d);
    d = min(d, e + c);
    e = min(e, d + f);
    f = min(f, a + e);
    if (p >= 0 && q >= 0) {
      if (p > q)
        s = q * a + (p - q) * f;
      else
        s = p * a + (q - p) * b;
    } else if (p <= 0 && q <= 0) {
      if (p > q)
        s = (-p) * d + (p - q) * e;
      else
        s = (-q) * d + (q - p) * c;
    } else if (p >= 0 && q <= 0)
      s = (-q) * e + p * f;
    else
      s = q * b + c * (-p);
    printf("%lld\n", s);
  }
  return 0;
}
