sub f{ ($b,$y,$a,$x,$c,$z,$t)=@a[map{$_,$_+3}@_]; $a<$b or $t+=$x*($a-$b+1),$a=$b-1; $c<$a or $t+=$z*($c-$a+1),$c=$a-1; $c>0?$t+0:(); } <>; for(<>){ @a=split; print+(sort{$a<=>$b} f(1,0,2), f(1,2,0), f(0,2,1), f(2,0,1), )[0]//-1,$/; }