CF BUDDY
← Problems·

1455E · Four Points

2400 · brute force, constructive algorithms, flows

Problem: You are given four different integer points p1p_1, p2p_2, p3p_3 and p4p_4 on XY\mathit{XY} grid.

In one step you can choose one of the points pip_i and move it in one of four directions by one. In other words, if you have chosen point pi=(x,y)p_i = (x, y) you can move it to (x,y+1)(x, y + 1), (x,y1)(x, y - 1), (x+1,y)(x + 1, y) or (x1,y)(x - 1, y).

Your goal to move points in such a way that they will form a square with sides parallel to OX\mathit{OX} and OY\mathit{OY} axes (a square with side 00 is allowed).

What is the minimum number of steps you need to make such a square?

Input Format: The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

Each test case consists of four lines. Each line contains two integers xx and yy (0x,y1090 \le x, y \le 10^9) — coordinates of one of the points pi=(x,y)p_i = (x, y).

All points are different in one test case.

Output Format: For each test case, print the single integer — the minimum number of steps to make a square.

Note: In the first test case, one of the optimal solutions is shown below:

In the second test case, one of the optimal solutions is shown below:

In the third test case, one of the optimal solutions is shown below:

Sample Cases

Case 1

Input

3
0 2
4 2
2 0
2 4
1 0
2 0
4 0
6 0
1 6
2 2
2 5
4 1

Output

8
7
5

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback