#include <bits/stdc++.h>
using namespace std;
int dx[6] = {1, 0, -1, -1, 0, 1};
int a[6], b[6];
int dy[6] = {1, 1, 0, -1, -1, 0};
long long dc[6];
long long x, y;
int main() {
  int t;
  int i, j, k;
  long long ans, s1, s2, s3, s4, s5, s6, x1, x2, x3, y1, y2, y3;
  cin >> t;
  while (t--) {
    cin >> x >> y;
    for (i = 0; i < 6; i++) cin >> dc[i];
    ans = 8E18;
    for (i = 0; i < 6; i++)
      for (j = 0; j < 6; j++) {
        a[i] = dx[i];
        a[j] = dy[i];
        b[i] = dx[j];
        b[j] = dy[j];
        if (abs(i - j) == 3 || i == j) continue;
        s1 = a[i] * b[j] - a[j] * b[i];
        if (s1 == 0) continue;
        x1 = x * b[j] - y * b[i];
        y1 = y * a[i] - x * a[j];
        if (s1 == 0 || x1 % s1 != 0 || y1 % s1 != 0) continue;
        x2 = x1 / s1;
        y2 = y1 / s1;
        if (x2 < 0 || y2 < 0) continue;
        s2 = x2 * dc[i] + y2 * dc[j];
        ans = min(ans, s2);
      }
    cout << ans << endl;
  }
  return 0;
}
