CF2030A.A Gift From Orangutan

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

A Gift From Orangutan

While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return...

The orangutan has gifted you an array aa of length nn. Using aa, you will construct two arrays bb and cc, both containing nn elements, in the following manner:

  • bi=min(a1,a2,,ai)b_i = \min(a_1, a_2, \ldots, a_i) for each 1in1 \leq i \leq n.
  • ci=max(a1,a2,,ai)c_i = \max(a_1, a_2, \ldots, a_i) for each 1in1 \leq i \leq n.

Define the score of aa as umi=1ncibium_{i=1}^n c_i - b_i (i.e. the sum of cibic_i - b_i over all 1in1 \leq i \leq n). Before you calculate the score, you can shuffle the elements of aa however you want.

Find the maximum score that you can get if you shuffle the elements of aa optimally.

Input

The first line contains tt (1t1001 \leq t \leq 100) — the number of test cases.

The first line of each test case contains an integer nn (1n10001 \leq n \leq 1000) — the number of elements in aa.

The following line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai10001 \leq a_i \leq 1000) — the elements of the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10001000.

Output

For each test case, output the maximum score that you can get.

Note

In the first test case, there is no other way to rearrange aa. So, b=[69]b = [69] and c=[69]c = [69]. The only possible score is 6969=069 - 69 = 0.

In the second test case, you can rearrange aa as [7,5,6][7, 5, 6]. Here, b=[7,5,5]b = [7, 5, 5] and c=[7,7,7]c = [7, 7, 7]. The score in this case is (77)+(75)+(75)=4(7 - 7) + (7 - 5) + (7 - 5) = 4. It can be shown this is the maximum possible score.

Samples

3
1
69
3
7 6 5
5
1 1 1 2 2
0
4
4

在线编程 IDE

建议全屏模式获得最佳体验