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 <cmath>
#include <iomanip>

using namespace std;

const double EPS = 1e-8;

int dcmp(double d) {
    if (fabs(d) < EPS) {
        return 0;
    }
    return d < 0? -1 : 1;
}

struct Vector {
    double x, y;
    Vector(double x=0, double y=0): x(x), y(y){
    }
};

typedef Vector Point;

double dot(const Vector& a, const Vector& b) {
    return a.x * b.x + a.y * b.y;
}

double cross(const Vector& a, const Vector& b) {
    return a.x * b.y - a.y * b.x;
}

Vector operator-(const Vector& a, const Vector& b) {
    return Vector(a.x - b.x, a.y - b.y);
}

Vector operator+(const Vector& a, const Vector& b) {
    return Vector(a.x + b.x, a.y + b.y);
}

Vector operator*(const Vector& a, const double p) {
    return Vector(a.x * p, a.y * p);
}

double length(const Vector& v) {
    return sqrt(dot(v, v));
}

bool intersectProp(const Point& a1, const Point& a2, const Point& b1, const Point& b2) {
    double c1 = cross(a2 - a1, b1 - a1);
    double c2 = cross(a2 - a1, b2 - a1);
    double c3 = cross(b2 - b1, a1 - b1);
    double c4 = cross(b2 - b1, a2 - b1);
    
    return dcmp(c1 * c2) == -1 && dcmp(c3 * c4) == -1;
}

bool onSegment(const Point& a, const Point& b, const Point& p) {
    Vector pa = a - p;
    Vector pb = b - p;
    return dcmp(cross(pa, pb)) == 0 && dcmp(dot(pa, pb)) <= 0;
}

bool intersect(const Point& a1, const Point& a2, const Point& b1, const Point& b2) {
    return intersectProp(a1, a2, b1, b2) || onSegment(a1, a2, b1) 
            || onSegment(a1, a2, b2) || onSegment(b1, b2, a1) || onSegment(b1, b2, a2);
}

Point lineIntesection(const Point& p, const Vector& v, const Point& q, const Vector& w) {
    Vector u = p - q;
    return p + v * (cross(w, u) / cross(v, w)); 
}

double distanceToSegment(const Point& a, const Point& b, Point& p) {
    Vector u = b - a;
    Vector v = p - a;
    Vector w = p - b;
    
    if (dot(u, v) < 0) {
        return length(v);
    }
    else if (dot(u, w) > 0) {
        return length(w);
    }
    else {
        return fabs(cross(u, v) / length(u));
    }
}

int main() {
    Point points[4];
    int q;
    cin >> q;
    for (int kase = 0; kase < q; kase++) {
        for (int i = 0; i < 4; i++) {
            cin >> points[i].x >> points[i].y;
        }
        Vector v1 = points[1] - points[0];
        Vector v2 = points[3] - points[2];

        double ans = 0;
        
        if (intersect(points[0], points[1], points[2], points[3])) {
            ans = 0;
        }
        else {
            ans = min(min(distanceToSegment(points[2], points[3], points[0]),
                    distanceToSegment(points[2], points[3], points[1])),
                    min(distanceToSegment(points[0], points[1], points[2]),
                    distanceToSegment(points[0], points[1], points[3])));
        }
        cout << fixed << setprecision(8) << ans << endl;
    }
   
    return 0;
}