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 <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
typedef long long int ll;
using namespace std;
const int MAX=5000+10;
const double eps=1e-9;
const double PI=acos(-1.0);

int sgn(double x)
{
    if (fabs(x)<eps) {
        return 0;
    }
    if (x<0) {
        return -1;
    }
    else return 1;
}
struct point
{
    double x,y;
    point(){}
    point(double _x,double _y)
    {
        x=_x;y=_y;
    }
    point operator +(const point &b)const
    {
        return point(x+b.x,y+b.y);
    }
    point operator -(const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    double operator ^(const point &b)const
    {
        return x*b.y-y*b.x;
    }
    double operator *(const point &b)const
    {
        return x*b.x+y*b.y;
    }
    point operator *(const double k)const
    {
        point kk;kk.x=k*x;kk.y=k*y;return kk;
    }
    double len(void)
    {
        return sqrt(x*x+y*y);
    }
};

struct line
{
    point s,e;
    line(){}
    line(point _s,point _e)
    {
        s=_s;e=_e;
    }
    pair<int, point> operator &(const line &b)const
    {
        point res=s;
        if(sgn((s-e)^(b.s-b.e))==0){
            if(sgn((s-b.e)^(b.s-b.e))==0){
                return make_pair(0, res);
            }
            else{
                return make_pair(1,res);
            }
        }
        double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x+=(e.x-s.x)*t;
        res.y+=(e.y-s.y)*t;
        return make_pair(2, res);
    }
};
double dist(point a,point b) {
    return sqrt((a-b)*(a-b));
}
bool inter(line l1,line l2)
{
    return max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
    max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
    max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
    max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
    sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&
    sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
}
point PointToLine(point P,line L) {
    point result;
    double t = ((P-L.s)*(L.e-L.s))/((L.e-L.s)*(L.e-L.s)); result.x = L.s.x + (L.e.x-L.s.x)*t;
    result.y = L.s.y + (L.e.y-L.s.y)*t;
    return result;
}
point NearestPointToLineSeg(point P,line L) {
    point result;
    double t = ((P-L.s)*(L.e-L.s))/((L.e-L.s)*(L.e-L.s)); if(t >= 0 && t <= 1)
    {
        result.x = L.s.x + (L.e.x - L.s.x)*t;
        result.y = L.s.y + (L.e.y - L.s.y)*t;
    }
    else
    {
        if(dist(P,L.s) < dist(P,L.e))
            result = L.s; else result = L.e;
    }
    return result;
}
double distpointtoseg(point p,line l)
{
    point result=NearestPointToLineSeg(p, l);
    return dist(p, result);
}
double distsegtoseg(line l1,line l2)
{
    if (inter(l1, l2)) {
        return 0;
    }
    return min(min(distpointtoseg(l1.s, l2), distpointtoseg(l1.e, l2)),min(distpointtoseg(l2.s, l1), distpointtoseg(l2.e, l1)));
}
int main(){
    //freopen("/Users/mac/Desktop/C++?¨????/test/test.txt", "r", stdin);
    int q;
    scanf("%d",&q);
    while(q--){
        line l1,l2;
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&l1.s.x,&l1.s.y,&l1.e.x,&l1.e.y,&l2.s.x,&l2.s.y,&l2.e.x,&l2.e.y);
        printf("%.8lf\n",distsegtoseg(l1, l2));
    }
    return 0;
}