spoj#NPC2015B. Eefun the Accountant
Eefun the Accountant
Eefun is an accountant. As an accountant, he loves to work with spreadsheet. In spreadsheet, a data must be saved into a single cell which has row and column. Spreadsheet has an interesting feature. It can move the cursor from a cell to another cell by clicking a button based on this rules :
- If current cell has a data and it's right neighbour also contain data, then clicking the right button will make the cursor to move to the first cell in the right of current cell which it's right neighbour doesnt contain a data. If there is no such cell, then the cursor will move to the rightmost cell in the current row.
- Else, then clicking the right button will make the cursor to move to the first cell in the right of current cell which has a data. If there is no such cell, then the cursor will move to the rightmost cell in the current row.
Same rules apply when clicking the left, up, and down button.
Eefun realizes this feature which makes him curious. He currently has a lot of data in his spreadsheet. He wants to edit a data on cell (R,C), but first he must move his cursor to the desired cell. Currently his cursor is at cell (1,1), the top-leftmost cell in the spreadsheet. Eefun wants to know the minimal number of button clicked to reach the cell.
Input
First line of input consists of 2 integers, N and M, the number of rows and number of columns
Second line consists of a number X, the number of data that Eefun currently has.
Next N lines each consists of 2 integers, A and B which denotes the position of the data
Line (N+3) contains a number Q, the number of query.
Next Q lines each consists of 2 integers, R and C, the position of cells that Eefun wants to edit
Output
For each query, output a single integer, the minimum number of buttons that Eefun should click to reach cell (R,C). If the cell cannot be reached, then output a string "Eefun gagal mengedit data"
"Eefun gagal mengedit data" means "Eefun fails to edit his data"
Note that each query is independent, so Eefun's initial cursor will be at (1,1) for each query
Example
Input: 3 3 8 1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3 2 3 2 1 3</p>Output: Eefun gagal mengedit data
1
Note : You can try this in Microsoft Excel using ctrl + arrow button
Constraints:
- 1 ≤ X ≤ min(100.000 , N*M)
- 1 ≤ N,M ≤ 10.000
- 1 ≤ Q ≤ 100.000