This page is still under construction.

Parts of this page are still being built. What you see may change.

Rooks

Time limit1sMemory limit128 MB

Summary
Move N rooks with orthogonal steps onto cells with distinct rows and columns using the fewest total moves.
Level

Medium6 of 10

Topics
Sorting, Greedy, Math
Solved
No attempts yet

Problem

A chessboard has NN rows and NN columns, and NN 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 NN rooks onto NN 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 TT.

Each test case begins with a line containing NN. Each of the next NN lines contains two integers, the row and the column of one rook, and both values are between 11 and NN inclusive. NN is at most 2000020000.

Output

For each test case, print one line in the format Case #x: M, where xx is the test case number starting from 11 and MM is the minimum number of moves needed to relocate the rooks.

Examples1

  1. Example 1

    Input
    2
    3
    1 1
    1 1
    1 1
    4
    1 1
    1 1
    1 3
    3 1
    
    Expected output
    Case #1: 6
    Case #2: 8