G. Minimize Sum

Time limit: 1s Memory limit: 256MB Input: Output:

Sumin likes deques, so his teacher gave him a cool deque game. The game works based on an array aa, and is played as follows:

  1. There will initially be a variable T=0T = 0 and a deque containing just the element 00.
  2. Sumin will go through the array elements, starting with the 1st one and ending at the nth one. At the ithi^{th} element Gigel must choose one of two ways to progress the game:
    • add to T the first element of the deque, then put aia_i at the front of the deque (that is T:=T+deque.front(), deque.push front(ai)T := T + deque.front(), \ deque.push \ front(a_i) )
    • add to T the last element of the deque, then put aia_i at the back of the deque (that is T:=T+deque.back(), deque.push back(ai)T := T + deque.back(), \ deque.push \ back(a_i) )

After the nthn^{th} element is processed, the score of the game will be equal to the current value of T. Grigoutz wants to minimize this final score T but he is unsure if he received the minimum score that is possible. Can you help him calculate it?

Task

Sumin asks you to write a program which given nn and the number of minutes to write each of the essays will print the minimum value of TT after the nn operations.

Input

The first line contains an integer tt (1t1041 \leq t \leq 10^4) the number of test cases. Then follows the description of the test cases.

The first line contains a single integer nn (1n21051 \leq n \leq 2 \cdot 10^5) the length of the array.

The second line of each test case contains nn integers a1,a2,ana_1, a_2, \dots a_n (0ai1090 \leq a_i \leq 10^9) - the elements of the array.

It is guaranteed that the sum of the values of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, you have to print a single integer - the minimum value of TT

Examples

stdin

3
2
1 2
5
9 3 6 5 9
8
5 6 4 8 3 1 0 10

stdout

0
14
19

Example description

For the first test case, Sumin will go for the first choice, resulting in T=0T = 0 and the deque containing 1 01 \ 0. After this, he will go with the second choice, adding 00 to TT, and the deque contains 1 0 21 \ 0 \ 2 in this order.
For the second test case, Sumin will go for the first choice, resulting in T=0T = 0 and the deque containing 9 09 \ 0. After this, he will go with the second choice, adding 00 to TT, and the deque contains 9 0 39 \ 0 \ 3 in this order. Third choice is for the back, T=3T = 3 and deque is 9 0 3 69 \ 0 \ 3 \ 6. Next is the back once again, resulting in T=9T = 9 and the deque is 9 0 3 6 59 \ 0 \ 3 \ 6 \ 5. Final choice is still on the back side of the deque, resulting in T=14T = 14 and the deque being 9 0 3 6 5 99 \ 0 \ 3 \ 6 \ 5 \ 9.

Log in or sign up to be able to send submissions!