#include <bits/stdc++.h>
using namespace std;
int X, Y;
long long f[2][10][2][10];
const long long ooo = 2e18;
struct TEdge {
  int dx, dy;
  long long cost;
};
vector<TEdge> E;
const int _DX[6] = {1, 0, -1, -1, 0, 1}, _DY[6] = {1, 1, 0, -1, -1, 0};
void enter() {
  cin >> X >> Y;
  E.resize(6);
  for (int d = 0; d < 6; ++d) {
    E[d].dx = _DX[d];
    E[d].dy = _DY[d];
    cin >> E[d].cost;
  }
}
const int _M = 3;
bool locked[2][10][2][10];
struct TV {
  int i, j;
  long long cost;
  TV(int i, int j, long long cost) : i(i), j(j), cost(cost) {}
  bool operator<(const TV &h) const { return cost > h.cost; }
};
map<pair<int, int>, long long> m;
long long M(int x, int y) {
  if (m.count({x, y})) return m[{x, y}];
  m[{x, y}] = 0;
  long long &r = m[{x, y}];
  r = ooo;
  if (abs(x) < _M && abs(y) < _M)
    r = f[1][x][1][y];
  else {
    if (abs(x) >= _M && abs(y) >= _M) {
      int x1 = x / 2;
      int x2 = x - x1;
      int y1 = y / 2;
      int y2 = y - y1;
      r = min(r, M(x1, y1) + M(x2, y2));
      r = min(r, M(x1, y2) + M(x2, y1));
    } else if (abs(x) >= _M) {
      int x1 = x / 2;
      int x2 = x - x1;
      r = min(r, M(x1, 0) + M(x2, y));
      r = min(r, M(x1, y) + M(x2, 0));
    } else {
      int y1 = y / 2;
      int y2 = y - y1;
      r = min(r, M(x, y1) + M(0, y2));
      r = min(r, M(0, y1) + M(x, y2));
    }
  }
  return r;
}
long long solve() {
  int z, x, y, u, v;
  for (x = -_M; x <= _M; ++x)
    for (y = -_M; y <= _M; ++y) {
      f[1][x][1][y] = ooo;
      locked[1][x][1][y] = 0;
    }
  f[1][0][1][0] = 0;
  for (z = 26; z >= 0; --z) {
    for (x = -2; x <= 2; ++x) {
      for (y = -2; y <= 2; ++y) {
        for (auto e : E) {
          u = x + e.dx;
          v = y + e.dy;
          f[1][u][1][v] = min(f[1][u][1][v], f[1][x][1][y] + e.cost);
        }
      }
    }
  }
  long long ans = 0;
  m.clear();
  ans = M(X, Y);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  int T;
  cin >> T;
  for (int t = 1; t <= T; ++t) {
    enter();
    cout << solve() << '\n';
  }
}
