1499_C. Minimum Grid Path_28909
Let's say you are standing on the XY-plane at point (0, 0) and you want to reach point (n, n).

You can move only in two directions: 

  * to the right, i. e. horizontally and in the direction that increase your x coordinate, 
  * or up, i. e. vertically and in the direction that increase your y coordinate. 



In other words, your path will have the following structure: 

  * initially, you choose to go to the right or up; 
  * then you go some positive integer distance in the chosen direction (distances can be chosen independently); 
  * after that you change your direction (from right to up, or from up to right) and repeat the process. 



You don't like to change your direction too much, so you will make no more than n - 1 direction changes.

As a result, your path will be a polygonal chain from (0, 0) to (n, n), consisting of at most n line segments where each segment has positive integer length and vertical and horizontal segments alternate.

Not all paths are equal. You have n integers c_1, c_2, ..., c_n where c_i is the cost of the i-th segment.

Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of k segments (k ≤ n), then the cost of the path is equal to ∑_{i=1}^{k}{c_i ⋅ length_i} (segments are numbered from 1 to k in the order they are in the path).

Find the path of the minimum cost and print its cost.

Input

The first line contains the single integer t (1 ≤ t ≤ 1000) — the number of test cases.

The first line of each test case contains the single integer n (2 ≤ n ≤ 10^5).

The second line of each test case contains n integers c_1, c_2, ..., c_n (1 ≤ c_i ≤ 10^9) — the costs of each segment.

It's guaranteed that the total sum of n doesn't exceed 10^5.

Output

For each test case, print the minimum possible cost of the path from (0, 0) to (n, n) consisting of at most n alternating segments.

Example

Input


3
2
13 88
3
2 3 1
5
4 3 2 1 4


Output


202
13
19

Note

In the first test case, to reach (2, 2) you need to make at least one turn, so your path will consist of exactly 2 segments: one horizontal of length 2 and one vertical of length 2. The cost of the path will be equal to 2 ⋅ c_1 + 2 ⋅ c_2 = 26 + 176 = 202.

In the second test case, one of the optimal paths consists of 3 segments: the first segment of length 1, the second segment of length 3 and the third segment of length 2.

The cost of the path is 1 ⋅ 2 + 3 ⋅ 3 + 2 ⋅ 1 = 13.

In the third test case, one of the optimal paths consists of 4 segments: the first segment of length 1, the second one — 1, the third one — 4, the fourth one — 4. The cost of the path is 1 ⋅ 4 + 1 ⋅ 3 + 4 ⋅ 2 + 4 ⋅ 1 = 19.
import java.io.*;
import java.util.*;

public class C {

	public static void main(String[] args) {
        FastScanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
        while(t-->0) { 
        	int n = in.nextInt();
        	int a[] = in.readArray(n);
        	int a1[] = new int[(n+1)/2];
        	int a2[] = new int[n/2];
        	int ind1 = 0, ind2 = 0;
        	for(int i=0;i<n;i++){
        		if(i%2==0) a1[ind1++] = a[i];
        		else a2[ind2++] = a[i];
        	}
        	int n1 = a1.length, n2 = a2.length;                  
        	long p1[] = new long[n1], p2[] = new long[n2];
        	p1[0] = a1[0]; p2[0] = a2[0];
        	for(int i=1;i<n1;i++) p1[i] = p1[i-1] + a1[i];
        	for(int i=1;i<n2;i++) p2[i] = p2[i-1] + a2[i];
        	long ans1[] = new long[n1];
        	long ans2[] = new long[n2];
        	ans1[0] = p1[0]*n;
        	ans2[0] = p2[0]*n;
        	
        	long mn = a1[0];
        	for(int i=1;i<n1;i++){
        		ans1[i] = p1[i];
        		mn = Math.min(mn,a1[i]);
        		ans1[i] -= mn;
        		ans1[i] += mn*(n-i);	
        	}
        	mn = a2[0];
        	for(int i=1;i<n2;i++){
        		ans2[i] = p2[i];
        		mn = Math.min(mn,a2[i]);
        		ans2[i] -= mn;
        		ans2[i] += mn*(n-i);	
        	}                    
        	long cost = Long.MAX_VALUE;
        	for(int i=0;i<n1;i++){
        		if(i==n2){
        			cost = Math.min(cost,ans1[i]+ans2[i-1]);
        		}
        		else{
        			cost = Math.min(cost,ans1[i]+ans2[i]);
        			if(i-1>-1) cost = Math.min(cost,ans1[i]+ans2[i-1]);
        		}
        	}
        	out.println(cost);
       	}
        out.flush();
	}
	
	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		
		String next() {
			while(!st.hasMoreTokens())
				try { st = new StringTokenizer(br.readLine()); }
				catch(IOException e) {}
			return st.nextToken();
		}
		
		String nextLine(){
			try{ return br.readLine(); } 
			catch(IOException e) { } return "";
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		int[] readArray(int n) {
			int a[] = new int[n];
			for(int i=0;i<n;i++) a[i] = nextInt();
			return a;
		}
	}

	static final Random random = new Random();

	static void ruffleSort(int[] a){
		int n = a.length;
		for(int i=0;i<n;i++){
			int j = random.nextInt(n), temp = a[j];
			a[j] = a[i]; a[i] = temp;
		}
		Arrays.sort(a); 	
	}
}

3
2
13 88
3
2 3 1
5
4 3 2 1 4
 3
2
13 88
3
2 3 0
5
4 3 2 1 4
 3
2
13 88
3
2 3 1
5
4 3 3 1 4
 3
2
13 100
3
2 3 0
5
4 3 2 1 5
 3
2
13 88
3
2 3 1
5
4 3 3 2 4
 3
2
13 100
3
2 3 0
5
4 3 1 1 5
 3
2
13 88
3
2 3 2
5
4 3 3 2 4
 3
2
13 100
3
2 3 0
5
4 3 0 1 5
 3
2
13 88
3
0 3 2
5
4 3 3 2 4
 3
2
13 100
3
2 3 0
5
4 2 0 1 5
 3
2
13 71
3
0 3 2
5
4 3 3 2 4
 3
2
13 100
3
2 2 0
5
4 2 0 1 5
 3
2
13 100
3
1 2 0
5
4 2 0 1 1
 3
2
13 100
3
1 0 0
5
4 2 0 1 1
 3
2
13 100
3
0 0 0
5
4 2 0 1 1
 3
2
13 100
3
0 0 0
5
4 2 1 1 1
 3
2
13 100
3
1 0 0
5
4 2 1 1 1
 3
2
10 88
3
2 3 1
5
4 3 2 1 4
 3
2
13 88
3
2 3 0
5
4 3 2 1 1
 3
2
13 88
3
2 3 1
5
4 3 3 2 2
 3
2
13 100
3
2 3 0
5
4 4 1 1 5
 3
2
13 88
3
2 3 2
5
4 3 5 2 4
 3
2
13 101
3
2 3 0
5
4 3 0 1 5
 3
2
13 110
3
2 3 0
5
4 2 0 1 5
 3
2
13 71
3
0 3 2
5
4 1 3 2 4
 3
2
13 100
3
2 2 0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0 0 0
 3
2
13 88
3
2 3 0
5
7 4 2 1 4
 3
2
13 100
3
2 4 0
5
2 3 2 1 5
 3
2
13 148
3
1 3 1
5
4 3 3 2 4
 3
2
13 88
3
2 0 2
5
4 3 1 2 4
 3
2
13 100
3
2 0 1
5
4 3 0 1 5
 3
2
22 100
3
2 3 0
5
6 2 0 1 5
 3
2
13 67
3
0 3 2
5
6 3 3 2 4
 3
2
13 110
3
3 2 0
5
4 2 0 1 1
 3
2
13 100
3
0 1 0
5
4 3 0 1 1
 3
2
13 110
3
0 0 0
5
7 2 1 1 1
 3
2
10 88
3
2 2 1
5
4 1 2 1 4
 3
2
13 95
3
0 3 2
5
4 1 4 2 4
 3
2
21 100
3
2 2 0
5
2 1 0 1 0
 3
2
13 88
3
2 3 2
5
4 3 4 2 2
 3
2
13 100
3
4 2 0
5
8 4 1 1 5
 3
2
13 88
3
2 10 2
5
4 3 5 3 4
 3
2
19 101
3
2 3 1
5
4 5 0 1 5
 3
2
13 167
3
2 0 0
5
4 4 2 1 4
 3
2
13 88
3
2 7 0
5
4 5 2 1 5
 3
2
20 88
3
1 4 1
5
4 3 3 2 4
 3
2
8 88
3
2 3 2
5
4 3 2 2 4
 3
2
6 000
3
2 0 0
5
4 3 0 1 5
 3
2
15 100
3
2 5 0
5
4 2 0 1 5
 3
2
13 000
3
3 0 0
5
4 2 0 1 1
 3
2
13 110
3
0 0 0
5
4 5 0 1 1
 3
2
6 100
3
1 0 1
5
4 1 1 1 1
 3
2
10 88
3
3 3 1
5
4 3 2 2 4
 3
2
13 1
3
2 1 2
5
4 3 3 2 2
 3
2
0 101
3
2 3 0
5
4 4 0 0 5
 3
2
21 100
3
2 2 1
5
4 4 0 1 0
 3
2
10 57
3
2 4 1
5
4 3 1 2 4
 3
2
13 88
3
2 3 2
5
4 3 3 1 2
 3
2
13 100
3
4 3 1
5
13 4 1 1 5
 3
2
13 101
3
2 3 1
5
4 2 0 1 2


202
13
19
 202
11
19
 202
13
23
 226
11
19
 202
13
27
 226
11
15
 202
15
27
 226
11
11
 202
9
27
 226
11
10
 168
9
27
 226
8
10
 226
7
10
 226
1
10
 226
0
10
 226
0
14
 226
1
14
 196
13
19
 202
11
16
 202
13
24
 226
11
16
 202
15
32
 228
11
11
 246
11
10
 168
9
21
 226
8
9
 226
10
10
 196
13
15
 202
13
26
 226
11
20
 202
24
32
 228
11
13
 168
9
10
 244
8
9
 228
10
10
 226
13
10
 196
11
15
 26
11
20
 244
14
9
 244
2
9
 206
13
15
 226
2
9
 202
13
11
 202
11
20
 202
17
19
 226
14
19
 202
12
27
 202
15
19
 226
2
11
 202
9
31
 244
11
10
 160
9
27
 226
8
4
 226
9
10
 226
0
11
 246
0
14
 212
1
14
 196
10
19
 202
7
24
 228
11
12
 246
11
6
 168
9
25
 226
8
7
 202
15
24
 226
13
20
 202
24
35
 228
13
13
 26
11
24
 206
13
8
 246
2
9
 202
2
20
 202
17
21
 216
12
27
 202
15
23
 26
2
11
 230
11
10
 160
9
28
 226
7
4
 26
9
10
 226
0
13
 246
0
16
 212
3
14
 196
14
19
 28
7
24
 202
11
12
 246
11
7
 204
8
7
 226
10
12
 134
13
19
 202
15
29
 226
15
20
 246
2
11
 108
13
11
 176
2
20
 202
19
21
 216
9
27
 194
15
23
 26
2
15
 202
9
35
 138
9
28
 222
7
4
 46
9
10
 202
10
12
 4
8
7
 84
15
29
 240
15
20
 248
13
13
 246
2
12
 96
13
11
 176
5
20
 186
19
21
 216
9
23
 26
2
16
 130
9
35
 138
9
30
 222
10
12
 2
8
7
 262
15
20
 262
13
13
 246
2
13
 96
13
9
 46
5
20
 216
12
23
 206
15
23
 26
2
12
 138
9
32
 22
10
12
 2
14
7
 262
13
20
 96
11
9
 46
5
18
 216
10
23
 306
15
23
 2
14
2
 262
7
20
 62
13
13
 246
2
7
 250
9
30
 246
5
7
 202
7
19
 272
11
19
 202
11
13
 202
13
15
 240
11
19
 226
13
15
 226
11
4
 168
9
33
 26
8
10
 228
8
10
 26
7
10
 230
1
10
 196
14
21
 202
11
18
 202
9
24
 226
11
12
 228
11
14
 26
8
9
 26
10
10
 246
7
10
 184
13
15
 202
18
32
 244
7
9
 226
13
11
 26
11
21
 196
16
15
 206
15
15
 226
2
5
 202
11
23
 226
14
17
 322
12
27
 202
6
19
 226
4
11
 244
11
12
 160
9
29
 246
9
10
 226
3
11
 246
0
17
 196
10
17
 216
9
25
 242
8
7
 202
15
25
 226
10
20
 202
36
35
 240
13
13
 360
2
20
 202
23
21
 216
15
27
 192
15
23
 12
2
11
 230
17
10
 26
3
10
 246
0
13
 212
3
13
 196
14
23
 28
9
24
 202
11
8
 242
10
12
 134
16
19
 202
15
20
 226
15
25
 228
13
10
