Rooks
Time limit1sMemory limit128 MB
Move N rooks with orthogonal steps onto cells with distinct rows and columns using the fewest total moves.
Problem
A chessboard has rows and columns, and rooks stand on it. Two rooks attack each other when they share a cell, share a row, or share a column, so several rooks may start stacked on one cell.
One move shifts a single rook one cell up, down, left, or right. Diagonal moves are not allowed.
Move the rooks onto different cells so that no two of them attack each other. Report the smallest number of moves that is enough.
Input
The first line contains the number of test cases .
Each test case begins with a line containing . Each of the next lines contains two integers, the row and the column of one rook, and both values are between and inclusive. is at most .
Output
For each test case, print one line in the format Case #x: M, where is the test case number starting from and is the minimum number of moves needed to relocate the rooks.