#Lutece2834. Rapport Test

Rapport Test

Migrated from Lutece 2834 Rapport Test

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

Kanade, Yuzuru, and Yuri are playing Overcooked! 2, a multi-player game that needs much rapport between players. But they seem to have no rapport at all. So they decide to have a test.

There is an array aa consisting of nn integers. The test will be performed on this array for mm times. Yuri, Kanade, and Yuzuru will take an integer away from one subarray of aa by turns. After an integer is taken away, it will disappear from the array. The only requirement is that the largest integer taken out is strictly less than the sum of the other two. The score for this time is the sum of the three integers they take away. After calculating the score, they will put back these integers to their original positions. If the requirement can't be satisfied, the score is 1-1.

Kanade would like to know the maximum scores they can reach each time.

Input

The first line contains one integer n (3n105)n\ (3\le n\le 10^5) --- indicating the size of array aa.

The second line contains nn integers, indicating the array aa. The ii-th integer indicates ai (0ai109)a_i\ (0\le a_i\le 10^9).

The third line contains one integer m (1m105)m\ (1\le m\le 10^5) --- indicating the number of tests.

For the next mm lines, each line contains two integers l,r (1lrn)l,r\ (1\le l\le r\le n), indicating the subarray they take integers away from is al,al+1,,ara_l,a_{l+1},\ldots ,a_r.

Output

Output mm lines, the ii-th line contains the maximum score they can reach in the ii-th test.

Samples

6
1 9 1 9 8 1
2
4 6
1 6
-1
26

Resources

The 18th UESTC Programming Contest Preliminary