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 double ll;

struct Point
{
    ll x,y;
    Point(){ x = y = 0.0; }
    Point(ll _x, ll _y) : x(_x), y(_y){}

    Point operator+ (Point p)
    {
        return Point(p.x + x, p.y + y);
    }

    Point operator- (Point p)
    {
        return Point(x - p.x, y - p.y);
    }

    ll operator* (Point p)
    {
        return p.x * x + p.y * y;
    }

    Point operator* (ll t)
    {
        return Point(x*t, y*t);
    }

    ll operator~ ()
    {
        return sqrt(x * x + y * y);
    }

    ll operator% (Point p)
    {
        return x * p.y - y * p.x;
    }

};

bool areIntersectSegment(Point a, Point b, Point p, Point q)
{
    Point v1=(b-a);
    Point v2=(q-p);
    double t,t1;
    if( v1 % v2 != 0 )
    {
        t = (double)(( p - a ) % v2) / (double)(v1 % v2);
        t1 = (double)(( a - p ) % v1) / (double)(v2 % v1);
        if( t >= 0.0 && t <= 1.0 && t1 >= 0.0 && t1 <= 1.0) return true;
        else return false;
    }
    else
    {
        // Segment collinear
        if( v1 % v2 == 0 && (p - a) % v1 == 0 )
        {
            t = (double)((p - a)*v1) /  (double)(v1*v1);
            t1 = (double)((p+v2-a)*v1) / (double)(v1*v1);
            //If are in opposite direction
            if( v1 * v2 < 0 )
            {
                if( t1 <= 1.0 && 0.0 <= t ) return true;
            }
            else
            {
                if( t <= 1.0 && 0.0 <= t1 ) return true;
            }
        }
    }
    return false;
}

Point projection(Point p1, Point p2, Point o)
{
    Point a = (p2 - p1);
    Point b = (o - p1);
    double t =( (a * b) / (a * a) );
    if( t < 0.0 ) return p1;
    if( t > 1.0 ) return p2;
    return p1 + a * t;
}

double distanceSegment(Point a, Point b, Point p, Point q)
{
    double minimo = numeric_limits<double>::max();
    Point aux;
    if( !areIntersectSegment(a,b,p,q) )
    {
        minimo = min( minimo, ~(projection(a,b,p)-p) );
        minimo = min( minimo, ~(projection(a,b,q)-q) );
        minimo = min( minimo, ~(projection(p,q,a)-a) );
        minimo = min( minimo, ~(projection(p,q,b)-b) );
        return  minimo;
    }
    return 0.0;
}

int main()
{
    cout << fixed;
    cout.precision(10);
    ios::sync_with_stdio(0); cin.tie(0);
    Point p0,p1,p2,p3,ans;
    Point vec, vec1;
    int q;
    cin >> q;
    while(q--)
    {
        cin >> p0.x >> p0.y >> p1.x >> p1.y >> p2.x >> p2.y >> p3.x >> p3.y;
        cout << distanceSegment(p0, p1, p2, p3) << '\n';
    }
    return 0;
}