the-great-indian-circus

Alex is the most famous ringmaster(circus) of the Great Indian Circus from the movie Dhoom3 who has just come to your city to demonstrate his skills and leave you awestruck by his stage performances. This time he is up with a new show which he named as 'Jungle me Mangal'. 

This show is held in an artificially made jungle(forest) in your city. The artificial jungle looks like a cartesian plane when seen from a helicopter(top view) and has an artificial tree planted at some of the integer coordinates of the cartesian plane looking jungle.

This special show requires a monkey to start from any tree in the jungle and visit all the trees atleast once by jumping from one tree to another, but with a constraint that any monkey can only jump from one tree to another parallel to the axis of the cartesian plane(jungle).

Initially there are some trees planted in the jungle, that may or may not be sufficient for the monkey to visit all the trees.
You are asked to tell the minimum number of trees required to be planted so that the monkey could visit all the trees.

Input:

The first line contains a positive integer t denoting the number of test cases.
Each test case begins with a line containing a positive integer N, denoting the number of trees already planted in the jungle. N lines follow in each test case.

Each of the next N lines contains 2 positive integers x and y, the coordinates of the i'th tree.

Output:

Print the minimal number of trees required to be planted so that the monkey could visit all the trees starting from any tree by jumping from one tree to another.

Constraints:

1 ≤ t ≤ 10

1 ≤ N ≤ 1000

1 ≤ x, y ≤ 1000

SAMPLE INPUT
1
2
1 2
2 1

SAMPLE OUTPUT
1

Source: hackerearth | Difficulty: UNKNOWN_DIFFICULTY


Solutions: