Problem: For given two segments s1 and s2, print the distance between them.

s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.

Constraints

* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.

Input

The entire input looks like:


q (the number of queries)
1st query
2nd query
...
qth query


Each query consists of integer coordinates of end points of s1 and s2 in the following format:


xp0 yp0 xp1 yp1 xp2 yp2 xp3 yp3


Output

For each query, print the distance. The output values should be in a decimal fraction with an error less than 0.00000001.

Example

Input

3
0 0 1 0 0 1 1 1
0 0 1 0 2 1 1 2
-1 0 1 0 0 1 0 -1


Output

1.0000000000
1.4142135624
0.0000000000

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
double x,y,xx,yy,xxx,yyy,xxxx,yyyy;
int q;
int main(){
	cin>>q;
	while(q--){
		cin>>x>>y>>xx>>yy>>xxx>>yyy>>xxxx>>yyyy;
		int pd1=0,pd2=0,pd3=0,pd4=0,pd5=0;
		xx-=x,yy-=y,xxx-=x,yyy-=y,xxxx-=x,yyyy-=y;
		if((xx*yyy-xxx*yy)*(xx*yyyy-xxxx*yy)<=0) pd1=1;
		xx+=x,yy+=y,xxx+=x,yyy+=y,xxxx+=x,yyyy+=y;
		
		x-=xx,y-=yy,xxx-=xx,yyy-=yy,xxxx-=xx,yyyy-=yy;
		if((x*yyy-xxx*y)*(x*yyyy-xxxx*y)<=0) pd2=1;
		x+=xx,y+=yy,xxx+=xx,yyy+=yy,xxxx+=xx,yyyy+=yy;
		
		x-=xxx,y-=yyy,xx-=xxx,yy-=yyy,xxxx-=xxx,yyyy-=yyy;
		if((xxxx*y-x*yyyy)*(xxxx*yy-xx*yyyy)<=0) pd3=1;
		x+=xxx,y+=yyy,xx+=xxx,yy+=yyy,xxxx+=xxx,yyyy+=yyy;
		
		x-=xxxx,y-=yyyy,xx-=xxxx,yy-=yyyy,xxx-=xxxx,yyy-=yyyy;
		if((xxx*y-x*yyy)*(xxx*yy-xx*yyy)<=0) pd4=1;
		x+=xxxx,y+=yyyy,xx+=xxxx,yy+=yyyy,xxx+=xxxx,yyy+=yyyy;
		
		if(max(x,xx)>=min(xxx,xxxx)
		&& max(xxx,xxxx)>=min(x,xx)
		&& max(y,yy)>=min(yyy,yyyy)
		&& max(yyy,yyyy)>=min(y,yy)){
			pd5=1;
		}
		double ANS=100000000;
		if(pd1==1 && pd2==1 && pd3==1 && pd4==1 && pd5==1){
			ANS=0;
		}
		double ans,ansx,ansy;
		
		ANS=min(ANS,sqrt((xxx-x)*(xxx-x)+(yyy-y)*(yyy-y)));
		ANS=min(ANS,sqrt((xxxx-x)*(xxxx-x)+(yyyy-y)*(yyyy-y)));
		ANS=min(ANS,sqrt((xxx-xx)*(xxx-xx)+(yyy-yy)*(yyy-yy)));
		ANS=min(ANS,sqrt((xxxx-xx)*(xxxx-xx)+(yyyy-yy)*(yyyy-yy)));
		
		xx-=x,yy-=y,xxx-=x,yyy-=y,xxxx-=x,yyyy-=y;
		ans=xx*xxx+yy*yyy;
		ans/=(xx*xx+yy*yy);
		ansx=ans*xx,ansy=ans*yy;
		if((ansx>=0 && ansx<=xx || ansx<=0 && ansx>=xx) && (ansy>=0 && ansy<=yy || ansy<=0 && ansy>=yy)){
			ANS=min(ANS,sqrt((xxx-ansx)*(xxx-ansx)+(yyy-ansy)*(yyy-ansy)));
		}
//		printf("%.10lf\n",ANS);
		ans=xx*xxxx+yy*yyyy;
		ans/=(xx*xx+yy*yy);
		ansx=ans*xx,ansy=ans*yy;
		if((ansx>=0 && ansx<=xx || ansx<=0 && ansx>=xx) && (ansy>=0 && ansy<=yy || ansy<=0 && ansy>=yy)){
			ANS=min(ANS,sqrt((xxxx-ansx)*(xxxx-ansx)+(yyyy-ansy)*(yyyy-ansy)));
		}
//		printf("%.10lf\n",ANS);
		xx+=x,yy+=y,xxx+=x,yyy+=y,xxxx+=x,yyyy+=y;
		
		x-=xxx,y-=yyy,xx-=xxx,yy-=yyy,xxxx-=xxx,yyyy-=yyy;
		ans=xxxx*x+yyyy*y;
		ans/=(xxxx*xxxx+yyyy*yyyy);
		ansx=ans*xxxx,ansy=ans*yyyy;
		if((ansx>=0 && ansx<=xxxx || ansx<=0 && ansx>=xxxx) && (ansy>=0 && ansy<=yyyy || ansy<=0 && ansy>=yyyy)){
//			cout<<"1 "<<xxxx<<" "<<ansx<<endl;
			ANS=min(ANS,sqrt((x-ansx)*(x-ansx)+(y-ansy)*(y-ansy)));
		}
//		printf("%.10lf\n",ANS);
		ans=xxxx*xx+yyyy*yy;
		ans/=(xxxx*xxxx+yyyy*yyyy);
		ansx=ans*xxxx,ansy=ans*yyyy;
		if((ansx>=0 && ansx<=xxxx || ansx<=0 && ansx>=xxxx) && (ansy>=0 && ansy<=yyyy || ansy<=0 && ansy>=yyyy)){
//			cout<<"2 "<<xxxx<<" "<<ansx<<endl;
			ANS=min(ANS,sqrt((xx-ansx)*(xx-ansx)+(yy-ansy)*(yy-ansy)));
		}
		printf("%.10lf\n",ANS);
	}
	return 0;
}