Under construction

Still being built. Things may change

Colored Tiles 4

Time limit1sMemory limit512 MB

Summary
Place N tiles of given colors and sizes (1x1 or 1x2) on an HxW board without overlap to maximize the sum of edge scores between adjacent tiles.
Level

Hard9 of 10

Topics
Graph, Matrix, Implementation
Solved
No attempts yet

Problem

In 2018, the International Olympiad in Informatics will be held in Japan. To celebrate it, the IOI committee plans to make an artwork of "slightly odd design" and decorate the competition venue with it. The committee asked JOI (Just Odd Inventions) Co. Ltd. to design it. JOI proposed the following.

  • The artwork is made on a rectangular board of H×WH \times W cells. The board is paved with NN tiles, and the tiles do not overlap.
  • Tile ii (1≤i≤N1 \le i \le N) is 1×11 \times 1 or 1×21 \times 2, and its color is CiC_i.
  • A 1×21 \times 2 tile may be rotated and used as a 2×12 \times 1 tile.

JOI fixed the kinds of tiles but did not say how to lay them out. The committee must keep JOI's tile kinds and find a layout that maximizes the beauty of the design.

When two tiles share an edge of cells and their colors are jj and kk, that edge scores Aj,kA_{j,k}. The beauty is the sum of the scores of all such edges. If two 1×21 \times 2 tiles share the edges of two cells, both edges count separately. Find a layout whose beauty is as large as possible.

Input

The first line contains four space-separated integers HH, WW, KK, and NN. The board has H×WH \times W cells, there are KK colors, and NN tiles are used.

The ii-th line (1≤i≤N1 \le i \le N) contains two integers SiS_i and CiC_i. Tile ii has size 1×Si1 \times S_i and color CiC_i.

The jj-th line (1≤j≤K1 \le j \le K) contains KK integers Aj,1,Aj,2,…,Aj,KA_{j,1}, A_{j,2}, \ldots, A_{j,K}. When a tile of color jj and a tile of color kk share an edge, the score is Aj,kA_{j,k}.

Output

Print NN lines. The ii-th line describes how to place tile ii.

  • If Si=1S_i = 1, print two integers AiA_i and BiB_i. The tile covers the cell in row AiA_i (counted from the top) and column BiB_i (counted from the left).
  • If Si=2S_i = 2, print four integers AiA_i, BiB_i, CiC_i, and DiD_i. The tile covers the cell in row AiA_i and column BiB_i, and the cell in row CiC_i and column DiD_i (rows counted from the top, columns from the left).

Constraints

All input data satisfy the following conditions.

  • 1≤H≤1001 \le H \le 100
  • 1≤W≤1001 \le W \le 100
  • 1≤K≤1001 \le K \le 100
  • 1≤N≤100001 \le N \le 10000
  • 1≤Si≤21 \le S_i \le 2 (1≤i≤N1 \le i \le N)
  • 1≤Ci≤K1 \le C_i \le K (1≤i≤N1 \le i \le N)
  • H×W=S1+S2+⋯+SNH \times W = S_1 + S_2 + \cdots + S_N
  • 0≤Aj,k≤10000 \le A_{j,k} \le 1000 (1≤j≤K1 \le j \le K, 1≤k≤K1 \le k \le K)
  • Aj,k=Ak,jA_{j,k} = A_{k,j} (1≤j≤K1 \le j \le K, 1≤k≤K1 \le k \le K)

Hint

In this sample, the board is 3×23 \times 2 and 4 tiles are used. The tiles are as follows.

NumberColorSize
111×11 \times 1
221×21 \times 2
331×11 \times 1
411×21 \times 2

The figure below shows the layout. The numbers in the figure are tile numbers.

The beauty of this design is 26.

  • Tile 2 (color 2) and tile 4 (color 1) share an edge, so they score 7.
  • Tile 2 (color 2) and tile 1 (color 1) share an edge, so they score 7.
  • Tile 4 (color 1) and tile 1 (color 1) share an edge, so they score 2.
  • Tile 4 (color 1) and tile 3 (color 3) share an edge, so they score 5.
  • Tile 1 (color 1) and tile 3 (color 3) share an edge, so they score 5.

Examples1

  1. Example 1

    Input
    3 2 3 4
    1 1
    2 2
    1 3
    2 1
    2 7 5
    7 4 3
    5 3 1
    
    Expected output
    2 2
    1 1 1 2
    3 2
    3 1 2 1