#PS0000113. Bài kiểm tra dịch chuyển (Practical Skill Test)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bài kiểm tra dịch chuyển (Practical Skill Test)
Practical Skill Test
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
A magical girl practices teleporting a piece on an by grid. The cells contain each integer from 1 through exactly once. Moving between cells and costs magic points.
For a test , start on the cell numbered . Repeatedly move from label to label until reaching . The difference is divisible by . If , no movement is needed. Find the total cost of each test.
Input
The first line contains integers .
The next lines each contain the integers of one grid row.
The following line contains integer .
Each of the last lines contains integers .
Output
Print lines, each containing the total cost of the corresponding test.
Subtasks
- Subtask 1 (20 points): ; ; ; if ; ; ; .
- Subtask 2 (30 points): ; ; ; if ; ; ; ; .
- Subtask 3 (50 points): ; ; ; if ; ; ; .
Examples 1
Input
3 3 2
1 4 3
2 5 7
8 9 6
1
4 8
Output
5
Explanation
With , the required labels are . Their cells are . The two costs are and , totaling 5.
Examples 2
Input
4 2 3
3 7
1 4
5 2
6 8
2
2 2
2 2
Output
0
0
Explanation
Both queries have . The piece is already at its destination, so neither query requires a move.
Examples 3
Input
5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13
Output
0
5
0
Explanation
The first and last queries require no move. The middle path is , through cells . The costs are 4 and 1, totaling 5.