Linearville

For each query, find the length of a shortest path between two grid crossings when the path must alternate directions at every crossing.

Hard8Shortest pathGraphPrefix sumDynamic programmingNo attempts yetTime limit1sMemory limit1024 MB

Problem

An alternating path on a grid with N = 10

Linearville has NN two way streets running west to east and NN two way streets running south to north. Together they form a grid of (N1)×(N1)(N-1) \times (N-1) blocks. The distance between two consecutive parallel streets is 1 or 5.

The Linearville Transit Authority is running an experiment and now requires every car to change direction at every crossing it passes. A car that reaches a crossing must turn left or right, so it can never drive straight through a crossing, and the segments of its path alternate between the west to east direction and the south to north direction. A path that obeys this rule is an alternating path. Each segment of a path joins two crossings that are consecutive on the same street, and the length of the path is the sum of the distances of its segments. The car has no previous move at the start crossing, so the first segment may run in either direction.

The figure shows an alternating path on a grid with N=10N = 10. That path is not a shortest alternating path.

The authority is building a new navigation app. Given QQ pairs of a start crossing and a target crossing, write a program that computes the length of a shortest alternating path for each pair. Linearville may be huge.

Input

The first line contains an integer NN (2N1000002 \le N \le 100000), the number of streets in each direction. In each direction the streets carry distinct numbers from 11 to NN, starting at the south west corner of the city.

The second line contains the integers D1,D2,,DN1D_1, D_2, \dots, D_{N-1} (Di{1,5}D_i \in \{1, 5\}), where DiD_i is the distance between the south to north street ii and the south to north street i+1i+1.

The third line contains the integers E1,E2,,EN1E_1, E_2, \dots, E_{N-1} (Ei{1,5}E_i \in \{1, 5\}), where EiE_i is the distance between the west to east street ii and the west to east street i+1i+1.

The fourth line contains an integer QQ (1Q1000001 \le Q \le 100000), the number of queries.

Each of the next QQ lines contains four integers AXAX, AYAY, BXBX, BYBY (1AX,AY,BX,BYN1 \le AX, AY, BX, BY \le N). The start crossing is (AX,AY)(AX, AY) and the target crossing is (BX,BY)(BX, BY). AXAX and BXBX are south to north streets, while AYAY and BYBY are west to east streets. No query appears twice.

Output

Print QQ lines. Line ii contains one integer, the length of a shortest alternating path for query ii. If the start crossing and the target crossing are the same, print 0.