#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; } } } bool ok(int a,int b,int 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--){ int a,b,c; ll 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; }