G. Summmon
time limit per test: 4 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Note that the answer for this problem might not fit in int64 or long long. It is recommended to use int128.
For any array b of length m, define f(b) as the minimum possible value of \max(b) - \min(b) that can be achieved by performing the following operation any number of times:
You are given an array a of length n. Your task is to compute the sum of f over all the subarrays^{\text{∗}} of a. More formally, determine the value of
l,a{l+1},\dots,a_r]). $$
^{\text{∗}}An array b is a subarray of an array a if b can be obtained from a by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.
Input
The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. Description of each test case follows.
The first line of each test case contains a single integer n (1 \le n \le 2\cdot10^5) — the length of the array a.
The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^9) — the elements of the array a.
It is guaranteed that the sum of n over all test cases does not exceed 2\cdot10^5.
Output
For each test case, print a single integer — the value of $\sum_{1 \le l \le r \le n} f([al,a{l+1},\dots,a_r])$.
Example
Input
5
3
6 4 8
4
1 2 3 4
9
9 9 8 2 4 4 3 5 3
6
18 12 24 9 6 36
6
36 24 18 12 9 6
Output
4
3
39
72
111
Note
For the first test case, let us look at all subarrays.
Therefore, the answer for the first test case is 0+0+0+2+0+2=4.
For the second test case, every subarray of length 1 contributes 0. Among the remaining subarrays, only [2,3], [3,4], and [2,3,4] have a non-zero contribution, each contributing 1. Therefore, the answer is 1+1+1=3.