Certain Scientific Railgun
Time limit1sMemory limit512 MB
Find the least axis-parallel travel from (0, 0) so that every robot shares a row or column with some visited point.
- Level
Medium7 of 10
- Topics
- Graph, Union-find, Greedy
- Solved
- No attempts yet
Problem
Misaka Mikoto is the third-ranked Level 5 esper in Academy City and is nicknamed Railgun after her signature move. One day, several evil robots invade Academy City, and Misaka plans to terminate all of them.
Consider Academy City as a 2-dimensional plane. There are robots in total, and the position of the -th robot is . Misaka starts moving from . Her railgun ability terminates all robots that share the same - or -coordinate with her. If Misaka is located at , every robot with or is terminated.
Misaka hates decimals and Euclidean geometry. She moves only from one integer point to another integer point, and each move can only be horizontal (parallel to the -axis) or vertical (parallel to the -axis). Moving around the city is tiring, so calculate the minimum distance she must move to terminate all robots.
Input
There are multiple test cases. The first line contains an integer , the number of test cases. Each test case is given as follows.
The first line contains an integer (), the number of robots.
The following lines each contain two integers and (), the position of the -th robot.
The sum of over all test cases does not exceed .
Output
For each test case, output one line containing one integer, the minimum distance Misaka needs to move to terminate all robots.
Hint
In the second sample test case, Misaka should first go to , then to , then to , then to .
In the third sample test case, Misaka should first go to , then to , then to .