#include <bits/stdc++.h>
using namespace std;
const int MX = 1005;
const int mod = 1000000007;
const long long inf = 1000000000000000069ll;
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int tc;
  cin >> tc;
  vector<int> cc(6, 0);
  for (int xx, yy; tc--;) {
    cin >> xx >> yy;
    for (int i = 0; i < 6; i++) cin >> cc[i];
    for (;;) {
      vector<int> tmp = cc;
      for (int j = 0; j < 6; j++)
        cc[j] = min(cc[j], cc[(j + 1) % 6] + cc[(j + 5) % 6]);
      if (tmp == cc) break;
    }
    vector<__int128_t> c;
    for (int i = 0; i < 6; i++) c.push_back(cc[i]);
    __int128_t ans = (__int128_t)inf * (__int128_t)inf;
    __int128_t x = xx, y = yy;
    if (x == y && y == 0) {
      cout << 0 << endl;
      continue;
    }
    if (x == y)
      ans = min(ans, (x < 0 ? c[3] * (-x) : c[0] * 1ll * x));
    else if (y == 0)
      ans = min(ans, (x < 0 ? c[2] * 1ll * (-x) : c[5] * 1ll * x));
    else if (x == 0)
      ans = min(ans, (y < 0 ? c[4] * 1ll * (-y) : c[1] * 1ll * y));
    if (x <= y && x >= 0) ans = min(ans, x * 1ll * c[0] + (y - x) * 1ll * c[1]);
    if (x <= y && y >= 0) ans = min(ans, y * 1ll * c[0] + (y - x) * 1ll * c[2]);
    if (x >= 0 && x >= y) ans = min(ans, x * 1ll * c[0] + (x - y) * 1ll * c[4]);
    if (y >= 0 && x >= y) ans = min(ans, y * 1ll * c[0] + (x - y) * 1ll * c[5]);
    if (x <= 0 && y >= 0) ans = min(ans, (-x) * 1ll * c[2] + y * 1ll * c[1]);
    if (x <= 0 && y >= x)
      ans = min(ans, (-x) * 1ll * c[3] + (y - x) * 1ll * c[1]);
    if (x >= 0 && y >= 0) ans = min(ans, x * 1ll * c[5] + y * 1ll * c[1]);
    if (y <= 0 && x <= y)
      ans = min(ans, (-y) * 1ll * c[3] + (y - x) * 1ll * c[2]);
    if (x <= 0 && y <= 0) ans = min(ans, (-y) * 1ll * c[4] + (-x) * 1ll * c[2]);
    if (x <= 0 && x >= y)
      ans = min(ans, (-x) * 1ll * c[3] + (x - y) * 1ll * c[4]);
    if (y <= 0 && x >= y)
      ans = min(ans, (-y) * 1ll * c[3] + (x - y) * 1ll * c[5]);
    if (x >= 0 && y <= 0) ans = min(ans, x * 1ll * c[5] + (-y) * 1ll * c[4]);
    long long get = ans;
    cout << get << endl;
  }
  return 0;
}
