#include<iostream>
#include<math.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long t,T,x1,x2,x3,x4,y1,y2,y3,y4;
cin>>T;
for(t=0;t<T;t++){
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
if(x3<=x2&&x3>=x1){
if(y1>=y3&&y1<=y4){
cout<<0<<endl;
}
else if(y1<y3){
cout<<llabs(y3-y1)<<endl;
}
else if(y1>y4){
cout<<llabs(y1-y4)<<endl;
}
}
else if(x3>x2){
if(y1>=y3&&y1<=y4){
cout<<llabs(x3-x2)<<endl;
}
else if(y1<y3){
cout<<llabs(y3-y1)+llabs(x3-x2)<<endl;
}
else if(y1>y4){
cout<<llabs(y1-y4)+llabs(x3-x2)<<endl;
}
}
else if(x3<x1){
if(y1>=y3&&y1<=y4){
cout<<llabs(x1-x3)<<endl;
}
else if(y1<y3){
cout<<llabs(y3-y1)+llabs(x1-x3)<<endl;
break;
}
else if(y1>y4){
cout<<llabs(y1-y4)+llabs(x1-x3)<<endl;
}
}
}
return 0;
}