The Happy Worm

Time limit1sMemory limit128 MB

Summary
Count maximal horizontal and vertical runs of empty cells that are at least 2 long in a field with stones.
Level

Medium4 of 10

Topics
Sorting, Implementation, Array, Hash map
Solved
No attempts yet

Problem

A happy worm lives in an m×nm \times n rectangular field. Some cells of the field contain a stone; every other cell is empty (each cell is either empty or holds exactly one stone).

When the worm sleeps it lies in a straight line: either horizontally, along a single row, or vertically, along a single column. It then stretches so that its length is as large as possible, extending from where it lies in both directions until it is stopped by a stone or by the edge of the field. The worm may never occupy a cell that contains a stone or a cell outside the field, and while sleeping it must be at least 22 cells long.

A sleeping position is therefore a maximal straight run of empty cells (in one row or one column) whose length is at least 22. Two positions are different when they cover different sets of cells; a horizontal run and a vertical run are always different positions.

Count how many different positions the worm can be in while sleeping.

Input

The first line contains an integer tt (1≤t≤111 \le t \le 11), the number of test cases. Each test case is given as follows.

The first line of a test case contains three integers mm, nn, and kk (1≤m,n,k≤1000001 \le m, n, k \le 100000): the number of rows, the number of columns, and the number of stones. Each of the next kk lines contains two integers rr and cc, the row and column of one stone (1≤r≤m1 \le r \le m, 1≤c≤n1 \le c \le n). No stone is listed more than once.

Output

For each test case, print a single line containing the number of different positions in which the happy worm can sleep.

Examples1

  1. Example 1

    Input
    1
    5 5 6
    1 5
    2 3
    2 4
    4 2 
    4 3
    5 1
    
    Expected output
    9