#Lutece0929. Post office

Post office

Migrated from Lutece 929 Post office

All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.

Description

There are N(N1000)N(N \leq 1000) villages along a straight road, numbered from 11 to NN for simplicity. We know exactly the position of every one (noted pos[i][i],pos[i][i] is positive integer and pos[i]108[i] \leq 10^8). The local authority wants to build a post office for the people living in the range ii to j(inclusive). He wants to make the sum of |pos[k][k]-position_of_postoffice| (ikj)(i \leq k \leq j) is minimum.

Input

For each test case, the first line is nn. Then nn integer, representing the position of every village and in ascending order. Then a integer q(q200000)q (q \leq 200000), representing the queries. Following qq lines, every line consists of two integers ii and jj. the input file is end with EOF. Total number of test case is no more than 1010.

Be careful, the position of two villages may be the same.

Output

For every query of each test case, you tell the minimum sum.

Samples

3
1 2 3
2
1 3
2 3
2
1

Note

Huge input,scanf is recommend.

Resources

2011 Heilongjiang collegiate programming contest