Count the cable pairs that cross by sorting on the left height and counting opposite-ordered right heights.
Easy3SortingBrute forceInterviewNo attempts yetTime limit5sMemory limit512 MBA company occupies two very tall buildings. The intranet that connects the buildings is made of many cables, and each cable joins one window of the first building to one window of the second building.
You look at the buildings from the side, so one building is on the left and the other is on the right. The windows of the left building appear as points on its right wall, and the windows of the right building appear as points on its left wall. A cable is a straight segment from a window of the left building to a window of the right building.

No two cables share a window, so at most one cable leaves any window. From where you stand, however, some cables cross each other partway. Exactly two cables meet at every intersection point.
In the picture above, the black circles are the intersection points and the white circles are the windows.
How many intersection points do you see?
The first line contains the number of test cases T. T test cases follow. The first line of each test case contains the number of cables you see, N.
Each of the next N lines describes one cable with two integers Ai and Bi, the windows that this cable joins. Ai is the height of the window on the left building, and Bi is the height of the window on the right building.
For each test case, print one line in the form Case #x: y, where x is the test case number starting from 1 and y is the number of intersection points you see.