#include #include #include #include const double EPS = 1e-8; int Sgn(double t) { return t < -EPS ? -1 : t > EPS; } typedef std::pair Point; double DistanceToOrigin(Point a) { if (Sgn(a.second) == 0) { return a.first; } else if(Sgn(a.first - 1) == 0) { return a.second + 1; } else if (Sgn(a.second - 1) == 0) { return 2 + 1 - a.first; } else if (Sgn(a.first == 0)) { return 3 + 1 - a.second; } return -1e9; } double CounterClockDistance(Point a, Point b) { double da = DistanceToOrigin(a); double db = DistanceToOrigin(b); if (Sgn(db - da) > 0) { return db - da; } else { return db - da + 4; } } double ClockDistance(Point a, Point b) { return 4 - CounterClockDistance(a, b); } double MinDistance(Point a, Point b) { return std::min(CounterClockDistance(a, b), ClockDistance(a, b)); } double Match(Point* lhs, Point* rhs) { std::vector id {0, 1, 2, 3}; double res = 1e9; do { double sum = 0; for (int i = 0; i < 4; ++ i) { sum += MinDistance(lhs[i], rhs[id[i]]); } res = std::min(res, sum); } while(std::next_permutation(id.begin(), id.end())); return res; } int main () { int T; std::cin >> T; for (int ca = 1; ca <= T; ++ ca) { Point ps[4]; for (int i = 0; i < 4; ++ i) { std::cin >> ps[i].first >> ps[i].second; } std::vector len; for (auto p : ps) { if (Sgn(p.second) == 0) { len.push_back(p.first); } if (Sgn(p.first - 1) == 0) { len.push_back(p.second); len.push_back(1 - p.second); } if (Sgn(p.second - 1) == 0) { len.push_back(1 - p.first); } if (Sgn(p.first) == 0) { len.push_back(p.second); len.push_back(1 - p.second); } } double res = 1e9; for (double l: len) { Point rect[4]; rect[0] = std::make_pair<>(l, 0); rect[1] = std::make_pair<>(1, l); rect[2] = std::make_pair<>(1 - l, 1); rect[3] = std::make_pair<>(0, 1 - l); res = std::min(res, Match(ps, rect)); rect[1] = std::make_pair<>(1, 1 - l); rect[3] = std::make_pair<>(0, l); res = std::min(res, Match(ps, rect)); } std::cout << std::fixed; std::cout << std::setprecision(12) << res << std::endl; } return 0; }