100 atcoder#ABC213C. [ABC213C] Reorder Cards

[ABC213C] Reorder Cards

Score : 300300 points

Problem Statement

We have HWHW cards arranged in a matrix with HH rows and WW columns. For each i=1,,Ni=1, \ldots, N, the card at the AiA_i-th row from the top and BiB_i-th column from the left has a number ii written on it. The other HWNHW-N cards have nothing written on them.

We will repeat the following two kinds of operations on these cards as long as we can.

  • If there is a row without a numbered card, remove all the cards in that row and fill the gap by shifting the remaining cards upward.
  • If there is a column without a numbered card, remove all the cards in that column and fill the gap by shifting the remaining cards to the left.

Find the position of each numbered card after the end of the process above. It can be proved that these positions are uniquely determined without depending on the order in which the operations are done.

Constraints

  • 1H,W1091 \leq H,W \leq 10^9
  • 1Nmin(105,HW)1 \leq N \leq \min(10^5,HW)
  • 1AiH1 \leq A_i \leq H
  • 1BiW1 \leq B_i \leq W
  • All pairs (Ai,Bi)(A_i,B_i) are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

HH WW NN

A1A_1 B1B_1

\vdots

ANA_N BNB_N

Output

Print NN lines.

If, after the end of the process, the card with the number ii is at the CiC_i-th row from the top and DiD_i-th column from the left, the ii-th line should contain CiC_i and DiD_i in this order with a space between them.

4 5 2
3 2
2 5
2 1
1 2

Let * represent a card with nothing written on it. The initial arrangement of cards is:

*****
****2
*1***
*****

After the end of the process, they will be arranged as follows:

*2
1*

Here, the card with 11 is at the 22-nd row from the top and 11-st column from the left, and the card with 22 is at the 11-st row from the top and 22-nd column from the left.

1000000000 1000000000 10
1 1
10 10
100 100
1000 1000
10000 10000
100000 100000
1000000 1000000
10000000 10000000
100000000 100000000
1000000000 1000000000
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10