Under construction

Still being built. Things may change

Republic of Korea

Time limit1sMemory limit128 MB

Summary
Count crossing pairs among K straight highways linking numbered east and west coast cities, using inversion counting.
Level

Medium4 of 10

Topics
Sorting, Divide and conquer, Array
Solved
No attempts yet

Problem

The Republic of Korea lies on the Korean Peninsula in East Asia. It is bordered by sea on three sides: the West Sea to the west, the East Sea to the east, and the South Sea to the south.

There are NN cities on the east coast and MM cities on the west coast. (N≤1000N \le 1000, M≤1000M \le 1000) On each coast the cities are numbered from 11, in order from north to south.

The newly inaugurated president wants to build KK highways connecting the east coast to the west coast. Each highway is a straight road that connects exactly one east-coast city to exactly one west-coast city. (In reality a perfectly straight road bores and tires drivers and can cause accidents, so roads are deliberately given slopes and curves to prevent this.)

A rest stop is built at every point where two highways cross. At most 22 highways cross at any single point. Given the highways, write a program that counts the number of points at which highways cross.

Input

The first line contains the number of test cases TT.

For each test case, the first line contains three integers NN, MM, and KK, where KK is the number of highways.

Each of the next KK lines describes one highway with two integers: the first is the number of the east-coast city it connects, and the second is the number of the west-coast city.

The number of highways is at most 400,000400{,}000, and only inputs whose answer is at most 263−12^{63}-1 are given.

Output

For each test case, print one line in the form Test case X: Y, where XX is the test case number (starting from 11) and YY is the number of points at which the highways cross.

Examples3

  1. Example 1

    Input
    1
    3 4 4
    1 4
    2 3
    3 2
    3 1
    
    Expected output
    Test case 1: 5
    
  2. Example 2

    Input
    1
    3 3 3
    1 1
    2 2
    3 3
    
    Expected output
    Test case 1: 0
    
  3. Example 3

    Input
    1
    4 4 4
    1 4
    2 3
    3 2
    4 1
    
    Expected output
    Test case 1: 6