#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long x, y;
long long a[10];
vector<long long> dx = {1, 0, -1, -1, 0, 1};
vector<long long> dy = {1, 1, 0, -1, -1, 0};
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    cin >> x >> y;
    for (long long i = 0; i < 6; i++) cin >> a[i];
    for (long long i = 1; i <= 100; i++)
      for (long long j = 0; j < 6; j++)
        a[j] = min(a[j], a[(j - 1 + 6) % 6] + a[(j + 1) % 6]);
    long long ans = 4e18;
    for (long long i = 0; i < 6; i++) {
      long long rx = x, ry = y;
      long long take = 2e9;
      if (dx[i] != 0) take = min(take, rx / dx[i]);
      if (dy[i] != 0) take = min(take, ry / dy[i]);
      if (take < 0) take = take * (-1);
      rx -= take * dx[i];
      ry -= take * dy[i];
      long long curCost = take * a[i];
      long long cx = rx, cy = ry;
      for (long long j = 0; j < 6; j++) {
        long long rx = cx, ry = cy;
        long long take = 2e9;
        if (dx[j] != 0) take = min(take, rx / dx[j]);
        if (dy[j] != 0) take = min(take, ry / dy[j]);
        if (take < 0) take = take * (-1);
        rx -= take * dx[j];
        ry -= take * dy[j];
        if (rx == 0 && ry == 0) ans = min(ans, curCost + take * a[j]);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
