#include <cstdio>
#include <algorithm>
using namespace std;
int n, m, t;
int ax[4], ay[4], bx[4], by[4], c[4], f[4];
int ab(int a)
{
if(a<0)
return -a;
return a;
}
int dist(int a, int b, int c, int d)
{
return ab(a-c)+ab(b-d);
}
int back(int tc, int xc, int yc)
{
int sol = tc + dist(xc, yc, n, m);
int newt, newx, newy;
for(int i=0; i<3; ++i)
{
if(f[i])
continue;
f[i]=1;
newt = (tc + dist(xc, yc, ax[i], ay[i]) + c[i]);
sol=min(sol, back(newt, bx[i], by[i]));
newt = (tc + dist(xc, yc, bx[i], by[i]) + c[i]);
sol=min(sol, back(newt, ax[i], ay[i]));
f[i]=0;
}
return sol;
}
int main()
{
freopen("portal3.in", "r", stdin);
freopen("portal3.out", "w", stdout);
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
for(int i=0; i<3; ++i)
scanf("%d%d%d%d%d", &ax[i], &ay[i], &bx[i], &by[i], &c[i]);
printf("%d\n", back(0, 0, 0));
}
return 0;
}