#include <bits/stdc++.h>
int c[6];
long long solvesystem(int x, int y, int a1, int a2, int b1, int b2, int c1plus,
                      int c1minus, int c2plus, int c2minus) {
  int A, B;
  if (a1 == 0) {
    B = x / b1;
    A = (y - b2 * B) / a2;
  } else if (a2 == 0) {
    B = y / b2;
    A = (x - b1 * B) / a1;
  } else if (b1 == 0) {
    A = x / a1;
    B = (y - A * a2) / b2;
  } else {
    A = y / a2;
    B = (x - a1 * A) / b1;
  }
  long long res = 0LL;
  if (A > 0)
    res = res + (long long)c1plus * A;
  else
    res = res - (long long)c1minus * A;
  if (B > 0)
    res = res + (long long)c2plus * B;
  else
    res = res - (long long)c2minus * B;
  return res;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int x, y;
    scanf("%d%d", &x, &y);
    for (int i = 0; i < 6; ++i) scanf("%d", &c[i]);
    long long res = std::min(
        std::min(solvesystem(x, y, 1, 1, 0, 1, c[0], c[3], c[1], c[4]),
                 solvesystem(x, y, 1, 1, -1, 0, c[0], c[3], c[2], c[5])),
        solvesystem(x, y, 0, 1, -1, 0, c[1], c[4], c[2], c[5]));
    printf("%lld\n", res);
  }
  return 0;
}
