#include <bits/stdc++.h>
using namespace std;
long long v[7];
int wx[7] = {0, 1, 0, -1, -1, 0, 1}, wy[7] = {0, 1, 1, 0, -1, -1, 0};
int main() {
  int t;
  long long x, y, t1, t2, t3;
  cin >> t;
  while (t--) {
    cin >> x >> y;
    for (int i = 1; i <= 6; i++) {
      cin >> v[i];
    }
    long long ans = 1e19;
    for (int i = 1; i <= 6; i++) {
      for (int j = i + 1; j <= 6; j++) {
        if (j - i == 3) continue;
        t1 = (wy[j] * x - wx[j] * y) / (wx[i] * wy[j] - wx[j] * wy[i]);
        t2 = (wy[i] * x - wx[i] * y) / (wx[j] * wy[i] - wx[i] * wy[j]);
        if (t1 < 0 || t2 < 0) continue;
        ans = min(ans, t1 * v[i] + t2 * v[j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
