#P7777. Diamond Rush
Diamond Rush
Diamond Rush
Problem Description
There are cells on a grid, the top-left cell is at while the bottom-right cell is at . In the cell at , there are diamonds. Initially, you are at , every time you can move to or from without moving out of the grid. Your destination is at , so you will take exactly moves. When you are at a cell, you can take all the diamonds inside this cell, including the starting point and the destination . However, some cells are blocked but you don't know which cells are blocked. Please write a program to answer queries. In each query, you will be given four integers , you need to report the maximum number of diamonds that you can take without passing the cells such that and .
Input
The first line of the input contains a single integer (), the number of test cases. For each case, the first line of the input contains two integers and (), denoting the size of the grid and the number of queries. Each of the following lines contains integers, the -th line contains (), denoting the number of diamonds in each cell. Each of the following lines contains four integers and (), denoting each query. It is guaranteed that you can find at least one valid path in each query.
Output
For each query, print a single line containing an integer, denoting the maximum number of diamonds that you can take. Note that the answer may be extremely large, so please print it modulo instead.
Sample Input
1
2 2
2 3
1 4
1 1 2 2
2 2 1 1
Sample Output
276
336
Source
2020 Multi-University Training Contest 2