8641: Darnassus

内存限制:512 MB 时间限制:3 S
题面:传统 评测方式:文本比较 上传者:
提交:3 通过:1

题目描述

  Even the World Tree must bow to the cycle of life. Everything born will die.

Archimonde has hurt it once, Sylvanas burnt it again.

Now the World Tree is slowly recovering.

The World Tree is burnt apart into n parts. Now it tries to rebuild itself.

Each part of the World Tree has an attribute pi, and all pi (1in) forms a permutation of 1,2,3...n.

For all 1i<jn, if the World Tree wants to grow an edge connecting part ii and part j directly, it needs to spend ijpipj energy. x means the absolute value of x.

The World Tree is very smart, so it will grow some edges such that all its n parts become connected (in other words, you can go from any part to any other part using only the edges that have been grown), spending the minimum energy.

Please calculate the minimum energy the World Tree needs to spend.

输入格式

The input consists of multiple test cases.

The first line contains an integer T (1T5) denoting the number of test cases.

For each test case, the first line contains a single integer n(1n50000).

The second line contains n integers pi (1pin), it's guaranteed that all pi forms a permutation.

输出格式

For each test case, output one line containing one integer indicating the answer.

输入样例 复制

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

输出样例 复制

7
24