Block Breaker

Time limit2sMemory limit512 MB

Summary
Blocks drop in a grid when a knocked block has a dropped left/right neighbor and a dropped front/back neighbor; after each of q moves, report how many blocks fall.
Level

Hard8 of 10

Topics
Union-find, Simulation, Matrix, Implementation
Solved
No attempts yet

Problem

Consider a rectangular frame of size n×mn \times m hanging horizontally in the air. Initially, the frame is filled tightly with n×mn \times m square blocks of size 1×11 \times 1. Because of the friction with the frame and with each other, the blocks are stable and will not drop.

However, the blocks can be knocked down. When a block is knocked down, other remaining blocks may also drop, since the friction from the remaining blocks may no longer hold them. Formally, a block drops if it is knocked or unstable. A block is unstable when at least one of its left and right neighbors has dropped and at least one of its front and back neighbors has also dropped. In this definition, the frame can be regarded as a huge block that is always stable.

Now you, the block breaker, want to knock down blocks. Formally, you will make qq moves. On the ii-th move, you choose position (xi,yi)(x_i, y_i). If a block is still at the chosen position, you knock it down; otherwise, nothing happens. After each move, you wait until no unstable blocks are going to drop before making the next move.

For example, look at the following illustration. The frame has size 2×22 \times 2, and blocks (1,1)(1, 1) and (1,2)(1, 2) have already dropped. If we knock down block (2,2)(2, 2), it drops, and then the last remaining block (2,1)(2, 1) also drops because it becomes unstable.

You are given a sequence of moves to make. For each move, find how many blocks drop as a result. In particular, if nothing happens during a move, the answer for that move is 0.

Input

The first line contains one positive integer TT, the number of test cases. (1≤T≤101 \le T \le 10) For each test case:

The first line contains three positive integers nn, mm, and qq, the dimensions of the frame and the number of moves. (1≤n,m≤20001 \le n, m \le 2000, 1≤q≤100 0001 \le q \le 100\,000)

Each of the following qq lines contains two positive integers xix_i and yiy_i, describing the next move to make. (1≤xi≤n1 \le x_i \le n, 1≤yi≤m1 \le y_i \le m)

Output

For each test case, output qq lines. Each line must contain a non-negative integer: the number of blocks that drop as a result of the corresponding move.

Examples1

  1. Example 1

    Input
    2
    2 2 3
    1 1
    1 2
    2 2
    4 4 6
    1 1
    1 2
    2 1
    2 2
    4 4
    3 3
    
    Expected output
    1
    1
    2
    1
    1
    2
    0
    1
    11