Under construction

Still being built. Things may change

Electric Needs

Time limit1sMemory limit128 MB

Summary
Number all empty cells of a huge grid by Chebyshev distance to the nearest of up to 20 power plants, breaking ties by row then column, and answer positional queries.
Level

Hard8 of 10

Topics
Binary search, Geometry, Math, Brute force
Solved
No attempts yet

Problem

You are building a new factory in your city. Because the factory has large electric needs, placing it close to a power plant matters, so you want to produce a prioritized list of the possible locations.

The candidate area is a rectangular grid of NN rows and MM columns of cells. Some cells contain a power plant. The factory occupies exactly one cell and may be placed on any empty cell (any cell that does not contain a power plant).

Rows are numbered 11 to NN and columns 11 to MM, so a cell is identified by two integers: cell (i,j)(i, j) is in row ii and column jj. The distance between cells (i0,j0)(i_0, j_0) and (i1,j1)(i_1, j_1) is max⁡(∣i0−i1∣,∣j0−j1∣)\max(|i_0 - i_1|, |j_0 - j_1|), where ∣x∣|x| is the absolute value of xx. The electric priority of a location is its minimum distance to a power plant.

Number every possible location with consecutive integers starting from 11, in ascending order of electric priority. Among locations with equal electric priority, order them by ascending row number; among those with equal priority and row, order them by ascending column number.

For intuition, in a 4×74 \times 7 grid the empty cells that lie at distance 11 from a power plant all receive electric priority 11, the next ring outward receives priority 22, and so on; the empty cells are then numbered following the ordering rule above.

You will answer several queries about this prioritized list. Each query gives a position in the list, and you must report which location was assigned that position.

Input

The input contains several test cases.

Each test case begins with a line of three integers NN, MM and PP — the number of rows, the number of columns (1≤N,M≤1091 \le N, M \le 10^9), and the number of power plants (1≤P≤201 \le P \le 20). Each of the next PP lines contains two integers RR and CC, the row and column of a power plant (1≤R≤N1 \le R \le N, 1≤C≤M1 \le C \le M). Within a test case all power plant locations are distinct. The next line contains one integer QQ, the number of queries (1≤Q≤501 \le Q \le 50). The following line contains QQ integers p1,…,pQp_1, \dots, p_Q, positions in the prioritized list (1≤pi≤N×M−P1 \le p_i \le N \times M - P).

The last test case is followed by a line containing three zeros (0 0 0), which must not be processed.

Output

For each test case, output Q+1Q + 1 lines. For i=1,…,Qi = 1, \dots, Q, line ii contains two integers: the row and the column of the location that was assigned position pip_i. After those QQ lines, print a single line containing the character - (a hyphen).

Examples3

  1. Example 1

    Input
    4 7 2
    2 5
    4 4
    6
    1 6 11 16 21 26
    1000000000 1000000000 1
    1 1
    1
    999999999999999999
    0 0 0
    
    Expected output
    1 4
    3 3
    4 5
    2 7
    4 7
    4 1
    -
    1000000000 1000000000
    -
    
  2. Example 2

    Input
    5 5 1
    3 3
    24
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    0 0 0
    
    Expected output
    2 2
    2 3
    2 4
    3 2
    3 4
    4 2
    4 3
    4 4
    1 1
    1 2
    1 3
    1 4
    1 5
    2 1
    2 5
    3 1
    3 5
    4 1
    4 5
    5 1
    5 2
    5 3
    5 4
    5 5
    -
    
  3. Example 3

    Input
    1 10 2
    1 3
    1 8
    8
    1 2 3 4 5 6 7 8
    0 0 0
    
    Expected output
    1 2
    1 4
    1 7
    1 9
    1 1
    1 5
    1 6
    1 10
    -