Journey through the kingdom

No attempts yetTime limit3sMemory limit256 MB

Problem

The kingdom of Quadradonia is divided into provinces arranged in a grid of RR rows and CC columns. The roads between them are dangerous, so nobody travels alone. Every trip goes by escorted carriage, and the carriages belong to the Interprovincial Communication and Peregrination Company (ICPC).

The company charges like this. In the province in row ii and column jj you rent a carriage for a cost of VijV_{ij}. That carriage takes you to any province at most RijR_{ij} rows away from row ii and at most CijC_{ij} columns away from column jj, that is, to the province in row ii' and column jj' whenever iiRij|i - i'| \le R_{ij} and jjCij|j - j'| \le C_{ij}. The price is flat: it depends on the province where you rent, not on where you get off.

You want to visit NN provinces p1,p2,,pNp_1, p_2, \dots, p_N in that order. Your budget is small, so for every leg of the trip you want the cheapest way to make it. A leg may pass through any number of intermediate provinces, and you pay the rental cost of every province where you board a carriage.

Input

The first line contains three integers RR, CC and NN (1R,C5001 \le R, C \le 500, 2N52 \le N \le 5), the number of rows, the number of columns and the number of provinces you want to visit. Rows are numbered from 1 to RR and columns from 1 to CC.

The next 3×R3 \times R lines form three groups of RR lines, each line holding CC integers. The jj-th number on the ii-th line of the first group is VijV_{ij} (1Vij10001 \le V_{ij} \le 1000). The second group gives RijR_{ij} (0RijR0 \le R_{ij} \le R) in the same layout, and the third group gives CijC_{ij} (0CijC0 \le C_{ij} \le C).

The last NN lines describe p1,p2,,pNp_1, p_2, \dots, p_N in visiting order. The kk-th of them contains two integers IkI_k and JkJ_k (1IkR1 \le I_k \le R, 1JkC1 \le J_k \le C), meaning that pkp_k is the province in row IkI_k and column JkJ_k.

Output

Print one line with N1N - 1 integers separated by single spaces. For k=1,2,,N1k = 1, 2, \dots, N - 1, the kk-th number is the minimum total rental cost of going from pkp_k to pk+1p_{k+1} with the escorted carriage system, or 1-1 if that leg is impossible. If pkp_k and pk+1p_{k+1} are the same province the cost is 0.