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

double dis(double x1, double y1, double x2, double y2) {
	return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}

double ldis(double x1, double y1, double x2, double y2, double x3, double y3) {

	if ((x1 - x2) * (x3 - x2) + (y1 - y2) * (y3 - y2) > 0 &&
		(x1 - x3) * (x2 - x3) + (y1 - y3) * (y2 - y3) > 0)
		return abs((y3 - y1) * (x2 - x1) - (x3 - x1) * (y2 - y1)) / dis(x2, y2, x3, y3);
	else
		return min(dis(x1, y1, x2, y2), dis(x1, y1, x3, y3));
}

int main()
{
	int q = 0;

	cin >> q;

	for (int i = 0; i < q; i++) {
		double p0x = 0, p0y = 0, p1x = 0, p1y = 0, p2x = 0, p2y = 0, p3x = 0, p3y = 0,
			u = 0, s = 0, t = 0, d = 0;

		cin >> p0x >> p0y >> p1x >> p1y >> p2x >> p2y >> p3x >> p3y;

		u = (p3y - p0y) * (p2x - p0x) - (p3x - p0x) * (p2y - p0y);
		d = min({ ldis(p0x, p0y, p2x, p2y, p3x, p3y), ldis(p1x, p1y, p2x, p2y, p3x, p3y),
			ldis(p2x, p2y, p0x, p0y, p1x, p1y), ldis(p3x, p3y, p0x, p0y, p1x, p1y) });

		if (u != 0) {
			s = ((p3y - p0y) * (p1x - p0x) - (p3x - p0x) * (p1y - p0y)) / u;
			t = -((p2y - p0y) * (p1x - p0x) - (p2x - p0x) * (p1y - p0y)) / u;

			if (s >= 0 && t >= 0) {
				if (s + t >= 1)
					d = 0;
			}
		}
		else {
			double t0 = (p2x - p0x) * (p3x - p0x) + (p2y - p0y) * (p3y - p0y),
				t1 = (p2x - p1x) * (p3x - p1x) + (p2y - p1y) * (p3y - p1y),
				t2 = (p0x - p2x) * (p1x - p2x) + (p0y - p2y) * (p1y - p2y);

			if ((p1y - p0y) * (p2x - p0x) - (p1x - p0x) * (p2y - p0y) != 0) {
				if (t0 <= 0)
					d = 0;
			}
			else if ((t0 <= 0) || (t1 <= 0) || (t2 <= 0))
				d = 0;
		}

		cout << setiosflags(ios::showpoint | ios::fixed) << setprecision(10)
			<< d << endl;
	}

	return 0;
}