var x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6, c1, c2, c3, c4, c5, c6:longint;
t, m, n, i, j, ii, drum:longint;
avantaj1, avantaj2, avantaj3:longint;
f, g:text;
buf1, buf2:array [1.. 1 shl 17] of char;
begin
assign (f, 'portal3.in'); settextbuf (f, buf1); reset (f);
assign (g, 'portal3.out'); settextbuf (g, buf2); rewrite (g);
read (f, t);
for ii := 1 to t do
begin
drum :=0;
read (f, n, m, x1, y1, x2, y2, c1, x3, y3, x4, y4, c3, x5, y5, x6, y6, c5);
if abs(x1-x2)>abs(y1-y2) then
begin
if x1> x2 then avantaj1:= x1-x2+y1-y2-c1
else avantaj1:= x2-x1+y2-y1-c1;
end
else
begin
if y1>y2 then avantaj1:= x1-x2+y1-y2-c1
else avantaj1:= x2-x1+y2-y1-c1;
end;
if abs(x3-x4)>abs(y3-y4) then
begin
if x3> x4 then avantaj2:= x3-x4+y3-y4-c3
else avantaj2:= x4-x3+y4-y3-c3;
end
else
begin
if y3>y4 then avantaj2:= x3-x4+y3-y4-c3
else avantaj2:= x4-x3+y4-y3-c3;
end;
if abs(x5-x6)>abs(y5-y6) then
begin
if x5> x6 then avantaj3:= x5-x6+y5-y6-c5
else avantaj3:= x6-x5+y6-y5-c5;
end
else
begin
if y5>y6 then avantaj3:= x5-x6+y5-y6-c5
else avantaj3:= x6-x5+y6-y5-c5;
end;
drum := n+m;
if avantaj1>0 then drum := drum-avantaj1;
if avantaj2>0 then drum := drum-avantaj2;
if avantaj3>0 then drum := drum-avantaj3;
writeln (g, drum);
end;
close (f); close (g);
end.