#P1609H. Pushing Robots

Pushing Robots

No submission language available for this problem.

Description

There're nn robots placed on a number line. Initially, ii-th of them occupies unit segment [xi,xi+1][x_i, x_i + 1]. Each robot has a program, consisting of kk instructions numbered from 11 to kk. The robot performs instructions in a cycle. Each instruction is described by an integer number. Let's denote the number corresponding to the jj-th instruction of the ii-th robot as fi,jf_{i, j}.

Initial placement of robots corresponds to the moment of time 00. During one second from moment of time tt (0t0 \le t) until t+1t + 1 the following process occurs:

  1. Each robot performs (tmodk+1)(t \bmod k + 1)-th instruction from its list of instructions. Robot number ii takes number F=fi,(tmodk+1)F = f_{i, (t \bmod k + 1)}. If this number is negative (less than zero), the robot is trying to move to the left with force F|F|. If the number is positive (more than zero), the robot is trying to move to the right with force FF. Otherwise, the robot does nothing.
  2. Let's imaginary divide robots into groups of consecutive, using the following algorithm:
    • Initially, each robot belongs to its own group.
    • Let's sum up numbers corresponding to the instructions of the robots from one group. Note that we are summing numbers without taking them by absolute value. Denote this sum as SS. We say that the whole group moves together, and does it with force SS by the same rules as a single robot. That is if SS is negative, the group is trying to move to the left with force S|S|. If SS is positive, the group is trying to move to the right with force SS. Otherwise, the group does nothing.
    • If one group is trying to move, and in the direction of movement touches another group, let's unite them. One group is touching another if their outermost robots occupy adjacent unit segments.
    • Continue this process until groups stop uniting.
  3. Each robot moves by 11 in the direction of movement of its group or stays in place if its group isn't moving. But there's one exception.
  4. The exception is if there're two groups of robots, divided by exactly one unit segment, such that the left group is trying to move to the right and the right group is trying to move to the left. Let's denote sum in the left group as SlS_l and sum in the right group as SrS_r. If SlSr|S_l| \le |S_r| only the right group will move. Otherwise, only the left group will move.
  5. Note that robots from one group don't glue together. They may separate in the future. The division into groups is imaginary and is needed only to understand how robots will move during one second [t,t+1][t, t + 1].

An illustration of the process happening during one second:

Rectangles represent robots. Numbers inside rectangles correspond to instructions of robots. The final division into groups is marked with arcs. Below are the positions of the robots after moving. Only the left of the two rightmost groups moved. That's because these two groups tried to move towards each other, and were separated by exactly one unit segment.

Look at the examples for a better understanding of the process.

You need to answer several questions. What is the position of aia_i-th robot at the moment of time tit_i?

The first line contains two integer numbers nn and kk — the number of robots and the number of instructions in the program of one robot (1n1001 \le n \le 100, 1k501 \le k \le 50).

The next line contains nn integer numbers xix_i — positions of robots at moment of time 00 (109x1<x2<<xn<109-10^9 \le x_1 < x_2 < \dots < x_n < 10^9).

The next nn lines contain descriptions of robots' programs. The ii-th of these lines contains kk integer numbers fi,jf_{i, j} (fi,j106|f_{i, j}| \le 10^6).

The next line contains a single integer number qq — the number of questions you to answer (1q10001 \le q \le 1000).

The next qq lines contain two integer number aia_i and tit_i each, meaning that you should find a position of aia_i-th robot at moment of time tit_i (1ain1 \le a_i \le n, 0ti10180 \le t_i \le 10^{18}).

For every question output a single integer on the new line. Coordinate of the left border of unit segment occupied by the aia_i-th robot at the moment of time tit_i.

Input

The first line contains two integer numbers nn and kk — the number of robots and the number of instructions in the program of one robot (1n1001 \le n \le 100, 1k501 \le k \le 50).

The next line contains nn integer numbers xix_i — positions of robots at moment of time 00 (109x1<x2<<xn<109-10^9 \le x_1 < x_2 < \dots < x_n < 10^9).

The next nn lines contain descriptions of robots' programs. The ii-th of these lines contains kk integer numbers fi,jf_{i, j} (fi,j106|f_{i, j}| \le 10^6).

The next line contains a single integer number qq — the number of questions you to answer (1q10001 \le q \le 1000).

The next qq lines contain two integer number aia_i and tit_i each, meaning that you should find a position of aia_i-th robot at moment of time tit_i (1ain1 \le a_i \le n, 0ti10180 \le t_i \le 10^{18}).

Output

For every question output a single integer on the new line. Coordinate of the left border of unit segment occupied by the aia_i-th robot at the moment of time tit_i.

Samples

Sample Input 1

2 1
0 4
1
-1
8
1 0
2 0
1 1
2 1
1 2
2 2
1 3
2 3

Sample Output 1

0
4
1
3
1
2
1
2

Sample Input 2

2 1
0 4
2
-1
8
1 0
2 0
1 1
2 1
1 2
2 2
1 3
2 3

Sample Output 2

0
4
1
3
2
3
3
4

Sample Input 3

2 2
0 1
1 -1
-1 1
4
1 0
1 1
1 2
1 3

Sample Output 3

0
0
-1
0

Sample Input 4

1 3
0
3 -2 1
3
1 5
1 10
1 15

Sample Output 4

1
4
5

Sample Input 5

4 3
-8 -4 2 5
-1 3 0
1 -3 -4
2 -5 2
-1 -4 2
5
3 12
4 18
4 11
1 6
1 10

Sample Output 5

6
9
6
-8
-9