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


using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define FORR(i,a,b) for (int i=(a);i>=(b);i--)

typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const double EPS = 1e-9;


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

bool equal(double x, double y) {
    return abs(y - x) < EPS;
}

double cross(double x1, double y1, double x2, double y2) {
    return x1 * y2 - x2 * y1;
}
double cross(Vector& a, Vector& b) {
    return a.x * b.y - b.x * a.y;
}

double dot(double x1, double y1, double x2, double y2) {
    return x1 * x2 + y1 * y2;
}
double dot(Vector& a, Vector& b) {
    return a.x * b.x + a.y * b.y;
}

double dist(double x1, double y1, double x2, double y2) {
    return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
double dist(Vector& a, Vector& b) {
    return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));
}
double dist2(Vector& a, Vector& b) {
    return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y);
}


double distance_between_segment_and_point(Vector& p1, Vector& p2, Vector& q) {
    Vector pp, pq;
    pp.x = p2.x - p1.x; pp.y = p2.y - p1.y;
    pq.x = q.x - p1.x; pq.y = q.y - p1.y;
    double dotpq = dot(pp, pq);
    double lenp = dist(p1, p2);
    double lenpq = dist(p1, q);
    if (dotpq <= 0.0) {
        return lenpq;
    }
    else if (dotpq < dot(pp, pp)) {
        double dpr = dotpq / lenp;
        return sqrt(lenpq * lenpq - dpr * dpr);
    }
    return dist(p2, q);
}

double distance_between_2_line_segments(Vector& p1, Vector& p2, Vector& q1, Vector& q2) {
    double lenp, lenq;
    lenp = dist(p1, p2);
    lenq = dist(q1, q2);

    // some segments have 0 length
    if (equal(lenp, 0) && equal(lenq, 0)) {
        return dist(p1, q1);
    }
    else if (equal(lenp, 0)) {
        return distance_between_segment_and_point(q1, q2, p1);
    }
    else if (equal(lenq, 0)) {
        return distance_between_segment_and_point(p1, p2, q1);
    }

    // if two segments is crossing..
    Vector pp(p2.x - p1.x, p2.y - p1.y), qq(q2.x - q1.x, q2.y - q1.y);
    double crossq, crossp;
    crossq = cross(pp.x, pp.y, q1.x - p1.x, q1.y - p1.y) * cross(pp.x, pp.y, q2.x - p1.x, q2.y - p1.y);
    crossp = cross(qq.x, qq.y, p1.x - q1.x, p1.y - q1.y) * cross(qq.x, qq.y, p2.x - q1.x, p2.y - q1.y);
    if (crossq < 0 && crossp < 0) {
        return 0.0;
    }

    double d1, d2, d3, d4;
    d1 = distance_between_segment_and_point(p1, p2, q1);
    d2 = distance_between_segment_and_point(p1, p2, q2);
    d3 = distance_between_segment_and_point(q1, q2, p1);
    d4 = distance_between_segment_and_point(q1, q2, p2);

    return min(min(d1, d2), min(d3, d4));
}


int main() {
    int N;
    Vector p1, p2, q1, q2;
    cin >> N;
    cout << fixed << setprecision(10);

    for (int i = 0; i < N; i++) {
        cin >> p1.x >> p1.y >> p2.x >> p2.y >> q1.x >> q1.y >> q2.x >> q2.y;
        cout << distance_between_2_line_segments(p1, p2, q1, q2) << endl;
    }

    return 0;
}