#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll c[6];
int main() {
  ios ::sync_with_stdio(0);
  int T;
  cin >> T;
  while (T--) {
    ll x, y;
    cin >> x >> y;
    for (int i = 0; i < 6; i++) cin >> c[i];
    for (;;) {
      bool f = 0;
      for (int i = 0; i < 6; i++) {
        if (c[i] + c[(i + 2) % 6] < c[(i + 1) % 6]) {
          c[(i + 1) % 6] = c[i] + c[(i + 2) % 6];
          f = 1;
        }
      }
      if (!f) break;
    }
    while (!(x >= 0 && y >= x)) {
      ll nx = y, ny = y - x;
      x = nx;
      y = ny;
      int tc = c[0];
      for (int i = 0; i < 5; i++) c[i] = c[i + 1];
      c[5] = tc;
    }
    ll ans = c[0] * x + c[1] * (y - x);
    cout << ans << '\n';
  }
}
