Rope Intranet

Count the cable pairs that cross by sorting on the left height and counting opposite-ordered right heights.

Easy3SortingBrute forceInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

A 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.

Side view of the two buildings and the cables

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?

Input

The first line contains the number of test cases TT. TT test cases follow. The first line of each test case contains the number of cables you see, NN.

Each of the next NN lines describes one cable with two integers AiA_i and BiB_i, the windows that this cable joins. AiA_i is the height of the window on the left building, and BiB_i is the height of the window on the right building.

Limits

  • 1T151 \le T \le 15
  • 1N10001 \le N \le 1\,000
  • 1Ai1041 \le A_i \le 10^4
  • 1Bi1041 \le B_i \le 10^4
  • Within one test case, all AiA_i are different.
  • Within one test case, all BiB_i are different.
  • No three cables meet at the same point.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the number of intersection points you see.