#include using namespace std; typedef long long ll; typedef pair pp; const ll INF = 1e18; const int MOD = 1000000007; #define rep(i,n) for(int i=0;i=2) res = min(res,y); if(a>=3) res = min(res,x+z*(c-a+2)); if(a>=3) res = min(res,2*x+z*(c-b+1)); if(res == INF) return -1; return res; }if(c==b && b < a){ ll res = INF; if(b>=2) res = min(res,y); if(c>=3) res = min(res,z+x*(a-c+2)); if(c>=3) res = min(res,2*z+x*(a-b+1)); if(res == INF) return -1; return res; }if(a < b && b == c){ ll res = INF; if(a+1 == b){ if(b >= 3) res = min(res,2*y); if(a >= 2) res = min(res,x+z*(c-b+1)); if(a >= 2) res = min(res,z*(c-a+1)); if(res == INF) return -1; return res; }else{ if(a >= 2) res = min(res,y*(b-a+1)); res = min(res,z*(c-b+1)); return res; } }if(c < b && b == a){ ll res = INF; if(c+1 == b){ if(b >= 3) res = min(res,2*y); if(c >= 2) res = min(res,z+x*(a-b+1)); if(c >= 2) res = min(res,x*(a-c+1)); if(res == INF) return -1; return res; }else{ if(c >= 2) res = min(res,y*(b-c+1)); res = min(res,x*(a-b+1)); return res; } }if(a < b && b < c){ ll res = INF; if(a+1 == b){ if(a >= 2) res = min(res,z*(c-a+1)); if(a >= 2) res = min(res,x+z*(c-a)); if(a >= 2) res = min(res,y*(b-a+1)); if(res == INF) return -1; return res; }else{ res = min(res,z*(c-b+1)); if(a >= 2) res = min(res,y*(b-a+1)); return res; } }if(c < b && b < a){ ll res = INF; if(c+1 == b){ if(c >= 2) res = min(res,x*(a-c+1)); if(c >= 2) res = min(res,z+x*(a-c)); if(c >= 2) res = min(res,y*(b-c+1)); if(res == INF) return -1; return res; }else{ res = min(res,z*(a-b+1)); if(c >= 2) res = min(res,y*(b-c+1)); return res; } }if(a==c && c < b){ if(a >= 2) return min(x,z); return -1; }if(a==c && b < c){ if(b+1 == a){ ll res = INF; if(b >= 3) res = min(res,x*(a-b+1)+z*(c-b+2)); if(b >= 3) res = min(res,x*(a-b+2)+z*(c-b+1)); if(b >= 2) res = min(res,min(y+x,y+z)); if(res == INF) return -1; return res; }else{ ll res = INF; if(b >= 3) res = min(res,x*(a-b+1)+z*(c-b+2)); if(b >= 3) res = min(res,x*(a-b+2)+z*(c-b+1)); if(b >= 2) res = min(res,min(x,z)); if(res == INF) return -1; return res; } } } bool ok(ll a,ll b,ll c){ if(a < c && c < b) return 1; if(c < a && a < b) return 1; if(b < a && a < c) return 1; if(b < c && c < a) return 1; else return 0; } int main(){ int t; cin >> t; while(t--){ ll a,b,c,x,y,z; cin >> a >> b >> c >> x >> y >> z; if(ok(a,b,c)) cout << 0 << endl; else cout << solve(a,b,c,x,y,z) << endl; } return 0; }