#include #include #include using namespace std; #define DEBUG 0 typedef pair pdd; //typedef pair pid; /*double dist(const pid& a, const pid& b) { double ans = 0; while (a.first != b.first) { ans += 1; } ans += b.second - a.second; if (ans < 0) { ans = -ans; } return min(ans, 4 - ans); }*/ double h(const pdd& a) { double sum = a.first + a.second; return a.second > a.first ? 4 - sum : sum; } double dist(double a, double b) { double ans = a - b; if (ans < 0) { ans = -ans; } return min(ans, 4 - ans); } double Fit(double ps[4], double to[4]) { int order[4] = {0, 1, 2, 3}; double ans = numeric_limits::max(); do { double now = 0; for (int i = 0; i < 4; ++i) { now += dist(ps[order[i]], to[i]); } ans = min(ans, now); } while (next_permutation(order, order + 4)); #if DEBUG printf("ps: "); for (int i = 0; i < 4; ++i) { printf("%f ", ps[i]); } printf("\nto: "); for (int i = 0; i < 4; ++i) { printf("%f ", to[i]); } printf("\nans = %f\n", ans); #endif return ans; } /*double SolveX(double ps[4], double x1, double x2) { static double to[4]; to[0] = h(make_pair(x1, 0)); to[1] = h(make_pair(x2, 0)); to[2] = h(make_pair(x1, 1)); to[3] = h(make_pair(x2, 1)); return Fit(ps, to); } double SolveX(double ha[4], double xs[4]) { double ans = SolveX(ha, xs[0], xs[1]); ans = min(ans, SolveX(ha, xs[0], xs[2])); ans = min(ans, SolveX(ha, xs[0], xs[3])); ans = min(ans, SolveX(ha, xs[1], xs[2])); ans = min(ans, SolveX(ha, xs[1], xs[3])); ans = min(ans, SolveX(ha, xs[2], xs[3])); return ans; } double Solve1(double xs[4], double ys[4]) { static double ha[4], rev[4]; for (int i = 0; i < 4; ++i) { ha[i] = h(make_pair(xs[i], ys[i])); rev[i] = h(make_pair(ys[i], xs[i])); } return min(SolveX(ha, xs), SolveX(rev, ys)); }*/ double Solve2(double ps[4], int fix) { static double to[4]; double gap = fmod(ps[fix], 1); to[0] = ps[fix]; to[1] = fmod(ps[fix] + 2, 4); to[2] = fmod(ps[fix] - 2 * gap + 4, 4); to[3] = fmod(to[2] + 2, 4); return Fit(ps, to); } double Solve3(double ps[4], int fix) { static double to[4]; to[0] = ps[fix]; for (int i = 1; i < 4; ++i) { to[i] = fmod(to[i-1] + 1, 4); } return Fit(ps, to); } double Solve23(double ps[4]) { double ans = numeric_limits::max(); for (int i = 0; i < 4; ++i) { ans = min(ans, Solve2(ps, i)); ans = min(ans, Solve3(ps, i)); } return ans; } double Solve(pdd ps[4]) { static double xs[4], ys[4], ha[4]; for (int i = 0; i < 4; ++i) { xs[i] = ps[i].first; ys[i] = ps[i].second; ha[i] = h(ps[i]); } //return min(Solve1(xs, ys), Solve23(ha)); return Solve23(ha); } int main() { int T; scanf("%d", &T); while (T--) { static pdd ps[4]; for (int i = 0; i < 4; ++i) { scanf("%lf%lf", &ps[i].first, &ps[i].second); } printf("%.12f\n", Solve(ps)); } return 0; }