Cairo City is a metropolis with public transport stations, where . The stations are arranged in concentric circles, and there are stations over each circle. The circles are numbered from to , and for each circle, the stations are numbered from to , inclusive. We refer to station on circle as station (where , ).
In the city, there are tram lines. Tram lines are bidirectional and periodic, each connecting consecutive stations of some circle. Let tram line (where ) connect the stations on circle , i.e., stations . Station is connected with station (.
Additionally, there are metro lines in the city. Metro lines are bidirectional, each connecting consecutive stations at the same position over different circles. Let metro line (where ) connect stations at position on each circle, i.e., stations . Stations and are not connected.
The figure shown above is the transport network for and , having and .
Traveling between consecutive stations via tram line takes minutes (where ). For each metro line (where ), traveling between stations and takes minutes (where ).
Your task is to determine shortest travel times in this public transport network. You are given queries, each query specifies two public transport stations and (where ). For each query, find the minimum amount of time it takes to get from the first station to the second one, assuming that changing lines doesn't take extra time.
Input data
The first line contains integers , and . The second line contains integers . The third line contains integers .
Each of the next lines contains four integers and .
Output data
For each query, output a single number on a separate line, the minimum travel time (in minutes) between the specified stations.
Constraints and Clarifications
- .
- .
- .
- for each .
- for each .
- and for each .
- for each .
# | Score | Restrictions |
---|---|---|
1 | 0 | Examples. |
2 | 16 | . |
3 | 13 | and . |
4 | 15 | , all -s are equal and for each . |
5 | 23 | All -s are equal and for each . |
6 | 10 | for each . |
7 | 23 | No additional limitations. |
Example
stdin
3 6 2
1 2 3
1 5
1 2 1 5
2 1 2 4
stdout
5
9
Explanation
The public transport network in the sample case is displayed in the figure above.
In the first query, the minimum time to reach station from is minutes, e.g., by taking the path
.
In the second query, the minimum time to reach station from is minutes, e.g., by taking the path
.