题目描述
We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).
The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is Ai,j.
You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x−i|+|y−j| magic points.
You now have to take Q practical tests of your ability as a magical girl.
The i-th test will be conducted as follows:
Initially, a piece is placed on the square where the integer Li is written.
Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not Ri. The test ends when x=Ri.
Here, it is guaranteed that Ri−Li is a multiple of D.
For each test, find the sum of magic points consumed during that test.
Constraints
1≤H,W≤300
1≤D≤H×W
1≤Ai,j≤H×W
Ai,j≠Ax,y((i,j)≠(x,y))
1≤Q≤105
1≤Li≤Ri≤H×W
(Ri−Li) is a multiple of D.
输入
Input is given from Standard Input in the following format:
H W D
A1,1 A1,2 … A1,W
:
AH,1 AH,2 … AH,W
Q
L1 R1
:
LQ RQ
输出
For each test, print the sum of magic points consumed during that test.
Output should be in the order the tests are conducted.
样例输入
3 3 2
1 4 3
2 5 7
8 9 6
1
4 8
样例输出
5
提示
4 is written in Square (1,2).
6 is written in Square (3,3).
8 is written in Square (3,1).
Thus, the sum of magic points consumed during the first test is (|3−1|+|3−2|)+(|3−3|+|1−3|)=5.
刚开始疯狂T了两次,原来是这个题目卡cout