def lscan; gets.split.map(&:to_i); end def bye(msg); puts msg.to_s; exit; end def 🎍(a,b,c) a != c && ((a < b && b> c) || (a > b && b < c)) end def solve1(a,b,c,x,y,z) # bmax cost = 0 if b <= c cost += (c - b + 1)*z c = b - 1 end if b <= a cost += (a - b + 1)*x a = b - 1 end if a == c cost += [x,z].min a -= 1 end return a > 0 && b > 0 && c > 0 && cost end def solve2(a,b,c,x,y,z) # bmin cost = 0 if b >= c cost += (b - c + 1)*y b = c - 1 end if b >= a cost += (b - a + 1)*y b = a - 1 end if a == c if a-b > 1 a -= 1 cost += [x,z].min else a -= 1 t = a < b ? 2 : 1 b -= t cost += [x,z].min + t*y end end return a > 0 && b > 0 && c > 0 && cost end def query(a,b,c,x,y,z) return 0 if 🎍(a,b,c) a,c,x,z = c,a,z,x if a > c s1 = solve1(a,b,c,x,y,z) s2 = solve2(a,b,c,x,y,z) return s1 && s2 ? [s1, s2].min : s1 || s2 ? s1 || s2 : -1 end gets.to_i.times do p query(*lscan) end