#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long x, y;
    cin >> x >> y;
    long long c1, c2, c3, c4, c5, c6;
    cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
    vector<pair<pair<int, int>, long long> > v = {
        {{0, 0}, 0},    {{1, 1}, c1},  {{0, 1}, c2}, {{-1, 0}, c3},
        {{-1, -1}, c4}, {{0, -1}, c5}, {{1, 0}, c6}};
    long long ans = -1;
    for (int i = 1; i < 6; i++) {
      for (int j = i + 1; j <= 6; j++) {
        long long c = v[j].first.first * v[i].first.second -
                      v[i].first.first * v[j].first.second;
        if (c == 0) {
          continue;
        }
        long long a = (v[j].first.first * y - v[j].first.second * x) / c;
        long long b = (v[i].first.second * x - v[i].first.first * y) / c;
        if (a < 0 || b < 0) {
          continue;
        }
        long long tmp = v[i].second * a + v[j].second * b;
        if (ans == -1 || tmp < ans) {
          ans = tmp;
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
