아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Nice Shape

시간 제한4초메모리 제한512 MB

요약
n개의 룩이 주어질 때, 어떤 네 개의 룩이 축에 평행한 직사각형의 네 꼭짓점을 이루도록 만드는 최소 이동 횟수를 구한다.
난이도

보통10점 중 6점

유형
해시맵, 수학, 그리디
정답자
아직 제출이 없습니다

문제

You are given nn rooks on the different cells of the infinite chessboard.

The ii-th of them is in the cell (r_i,c_i)(r\_i, c\_i).

In one move you can move any rook to any cell in the same row/column. In other words, in one move you can choose any ii and then either replace r_ir\_i to any other integer or replace c_ic\_i to any other integer. You can't move a rook to the cell with some other rook.

Four different rooks a,b,c,da, b, c, d form a nice shape if you can find a rectangle such that a,b,c,da,b,c,d are its corners. In other words, if the set of cells (r_a,c_a),(r_b,c_b),(r_c,c_c),(r_d,c_d)\\{(r\_a, c\_a), (r\_b, c\_b), (r\_c, c\_c), (r\_d, c\_d)\\} is equal to the set of cells (x_1,y_1),(x_1,y_2),(x_2,y_1),(x_2,y_2)\\{(x\_1, y\_1), (x\_1, y\_2), (x\_2, y\_1), (x\_2, y\_2)\\} for some integers x_1,x_2,y_1,y_2x\_1, x\_2, y\_1, y\_2 with x_1≠x_2x\_1 \neq x\_2 and y_1≠y_2y\_1 \neq y\_2.

For example, the white rooks in the following picture form a nice shape.

Your goal is to find the minimum number of moves that you can perform to get a nice shape.

In other words, you need to find the minimum number of moves that you can perform, such that after them it will be possible to find a rectangle with four rooks in its corners.

입력

The first line of input contains one integer tt (1≤t≤25,0001 \leq t \leq 25\\,000): the number of test cases.

The description of tt test cases follows.

The first line contains one integer nn (4≤n≤100,0004 \leq n \leq 100\\,000).

The ii-th of the next nn lines contains two integers r_i,c_ir\_i, c\_i (1≤r_i,c_i≤1091 \leq r\_i, c\_i \leq 10^9)

For each pair i,ji, j with i≠ji \neq j, r_i≠r_jr\_i \neq r\_j or c_i≠c_jc\_i \neq c\_j.

The total sum of nn is at most 100,000100\\,000.

출력

For each test case, print one integer: the minimum number of moves you need to perform to obtain at least one nice shape among given rooks.

힌트

One of the possible optimal solutions for the first test case of the example:

    

One of the possible optimal solutions for the second test case of the example:

  

예제1

  1. 예제 1

    입력
    5
    4
    4 4
    1 1
    2 2
    3 3
    4
    4 4
    4 1
    1 4
    2 2
    6
    3 2
    2 1
    1 2
    3 3
    3 4
    3 1
    5
    1 1
    1 2
    1 3
    1 4
    5 5
    4
    1000000000 1000000000
    1000000000 1
    2 2
    1000000000 999999999
    
    예상 출력
    4
    2
    1
    3
    3