Advanced1800

Not Alone

Codeforces
View on Codeforces
Time: 2 secondsMemory: 256 megabytes

A circular array b of length m is nice if every element has at least one adjacent element^∗ that is equal to it. Formally, for every 1≤ i≤ m, at least one of the following holds: b_i = b_(i+m-2) m\;+1, or b_i = b_i m\;+1, where x y denotes the remainder from dividing x by y.

You are given a circular array a of length n. In one operation, you may increase or decrease any element of a by 1. Your task is to determine the minimum number of operations required to make array a nice. More formally, find the minimum value of Σ_i=1^n |b_i - a_i| among all nice circular arrays b of length n.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10^4). The description of the test cases follows. The first line of each test case contains a single integer n (3≤ n≤ 2· 10^5) — the length of the circular array a.The second line of each test case contains n integers a_1, a_2, …, a_n (1≤ a_i≤ 10^9) — the elements of the circular array a.It is guaranteed that the sum of n over all test cases does not exceed 2· 10^5.

Output

For each test case, output a single integer representing the minimum number of operations required to make array a nice.

Examples

Input
4 5 1 1 1 1 1 4 2 100 99 3 5 2 2 5 9 5 6 1 1 1 2 1 2
Output
0 2 4 1

Note

In the first test case, all elements of a are equal. Therefore, the circular array a is already nice and no operations are required.In the second test case, we can perform the following sequence of operations: Increase a_1 by 1. Now, a=[3, 100, 99, 3]. Decrease a_2 by 1. Now, a=[3, 99, 99, 3]. After these operations, every element has at least one adjacent element with the same value: a_1 is equal to a_4. a_2 is equal to a_3. a_3 is equal to a_2. a_4 is equal to a_1. In the third test case, the circular array a can become nice by decreasing a_4 four times. This results in a = [2, 2, 5, 5, 5] which is nice as every element has at least one adjacent element with the same value.

dpgreedy

Discussion

(0)
Sign in to join the discussion.
Loading editor...
0 online