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 <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;

#define EPS (1e-10)
#define equals( A, B ) (fabs((A) - (B)) < EPS)

class Point {
public:
    double x, y;
    Point( double x = 0, double y = 0 ): x(x), y(y) {}
    
    Point operator + ( Point p ) { return Point( x + p.x, y + p.y ); }
    Point operator - ( Point p ) { return Point( x - p.x, y - p.y ); }
    Point operator * ( double a ) { return Point( a * x, a * y ); }
    Point operator / ( double a ) { return Point( x / a, y / a ); }

    double abs() { return sqrt( norm() ); }
    double norm() { return x * x + y * y; }

    bool operator < ( const Point &p ) const {
	return ( x != p.x ) ? x < p.x : y < p.y;
    }

    bool operator == ( const Point &p ) const {
	return equals( x, p.x ) && equals( y, p.y ); 
    }

    double dot( Point a ) { return x * a.x + y * a.y; }
    double cross( Point a ) { return x * a.y - y * a.x; }
    double distance( Point a ) {
	return (*this - a).abs();
    }
};

typedef Point Vector;

static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;

class Line {
public:
    Point p1, p2;
    Line( Point p1 = Point(), Point p2 = Point() ): p1(p1), p2(p2){}
    // ?????´????????????( cos = 0 )
    bool isOrthogonal( Line a ) {
	return equals( 0.0, (p1 - p2).dot( a.p1 - a.p2 ) );
    }
    // ??????????????????( sin = 0 )
    bool isParallel( Line a ) {
	return equals( 0.0, (p1 - p2).cross( a.p1 - a.p2 ) );
    }

    Point projection( Point p ) {
	Vector v12 = p2 - p1;
	double r = v12.dot( p - p1 ) / v12.norm();
	return p1 + v12 * r;
    }

    Point reflection( Point p ) {
	return p + ( projection( p ) - p ) * 2.0;
    }

    double distance( Point p ) {
	Vector v12 = this->p2 -this->p1;
	Vector v10 = p - this->p1;
	
	return abs( v12.cross( v10 ) / v12.abs() );
    }

    double distanceSP( Point p ) {
	if ( (p - this->p1).dot( this->p2 - this->p1 ) < 0.0 ) {
	    return this->p1.distance( p );
	} else if ( (p - this->p2).dot( this->p1 - this->p2 ) < 0.0 ) {
	    return this->p2.distance( p );
	}
	else {
	    return distance( p );
	}
    }

    int ccw( Point p ) {
	Vector a = p2 - p1;
	Vector b = p - p1;
	if ( a.cross( b ) > EPS ) return COUNTER_CLOCKWISE;
	else if ( a.cross( b ) < -EPS ) return CLOCKWISE;
	else if ( a.dot( b ) < -EPS ) return ONLINE_BACK;
	else if ( a.norm() < b.norm() ) return ONLINE_FRONT;
	else return ON_SEGMENT;
    }

    bool intersect( Line l ) {
	return ( ( ccw( l.p1 ) * ccw( l.p2 ) <= 0 && l.ccw( p1 ) * l.ccw( p2 ) <= 0 ) );
    }
};

typedef Line Segment;


int main()
{
    int q;
    scanf("%d", &q);

    for ( int i = 0; i < q; i++ ) {
	Point p0, p1, p2, p3;
	scanf("%lf %lf", &p0.x, &p0.y);
	scanf("%lf %lf", &p1.x, &p1.y);
	scanf("%lf %lf", &p2.x, &p2.y);
	scanf("%lf %lf", &p3.x, &p3.y);

	Segment s1 = Segment( p0, p1 );
	Segment s2 = Segment( p2, p3 );
	if ( s1.intersect( s2 ) ) {
	    printf("%.10lf\n", 0.0);
	}
	else {
	    double dist1 = s1.distanceSP( s2.p1 );
	    double dist2 = s1.distanceSP( s2.p2 );	
	    double dist3 = s2.distanceSP( s1.p1 );
	    double dist4 = s2.distanceSP( s1.p2 );	
	    double min_dist = min( min( dist1, dist2 ), min( dist3, dist4 ) );
	    printf("%.10lf\n", min_dist);
	}
    }

    return 0;
}