PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000005. Thu gọn bảng thẻ (Reorder Cards)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Thu gọn bảng thẻ (Reorder Cards)

Reorder Cards

Source: AtCoder Version: Phuoc Hung OJ Extended

Problem Statement

An HH by WW grid contains NN numbered cards at distinct cells (Ai,Bi)(A_i,B_i). Remove every row and column containing no numbered card, shifting the remaining rows upward and columns leftward. Determine the final position of every numbered card while keeping their relative order.

Input

The first line contains HH, WW, NN. Each of the next NN lines gives row AiA_i and column BiB_i of card ii.

Output

Print NN lines. Line ii contains the new row CiC_i and column DiD_i of card ii.

Subtasks

  • Subtask 1 (20 points): 1≤H,W≤301\le H,W\le 30; 1≤N≤min⁡(100,HW)1\le N\le \min(100,HW); 1≤Ai≤H1\le A_i\le H, 1≤Bi≤W1\le B_i\le W; all (Ai,Bi)(A_i,B_i) distinct.
  • Subtask 2 (30 points): 1≤H,W≤20001\le H,W\le 2000; 1≤N≤min⁡(3000,HW)1\le N\le \min(3000,HW); 1≤Ai≤H1\le A_i\le H, 1≤Bi≤W1\le B_i\le W; all (Ai,Bi)(A_i,B_i) distinct.
  • Subtask 3 (50 points): 1≤H,W≤1091\le H,W\le 10^9; 1≤N≤min⁡(105,HW)1\le N\le \min(10^5,HW); 1≤Ai≤H1\le A_i\le H, 1≤Bi≤W1\le B_i\le W; all (Ai,Bi)(A_i,B_i) distinct.

Examples

Input

5 7 3
2 4
5 1
2 7

Output

1 2
2 1
1 3

Explanation

Keep only rows 2,52,5 and columns 1,4,71,4,7. Card one moves from (2,4)(2,4) to (1,2)(1,2).