Cooperation Game

아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

Athletic contests in schools sometimes get too heated. To promote cooperation, the teachers have devised a game where cooperation leads to better scores.

This cooperation game starts with NN students standing in a line. To get a score, two students from the same class will get out of the line. If they were ii-th and jj-th students along the line, just before they went out, they add ij|i - j| to the total score. The empty places of the two students that just came out will be compacted. The game stops when there are no more pairs of students from the same class.

For example, consider six students initially standing in a line:

1,2,3,3,2,11, 2, 3, 3, 2, 1

The numbers are class numbers of the students. If the students come out from the line in the order of class number 11, then class number 22, then class number 33, the total score is 5+3+1=95 + 3 + 1 = 9. However, in the same initial situation, if the students come out from the line in the order of 33, 22, 11, then the total score is 1+1+1=31 + 1 + 1 = 3.

Given the class numbers in the initial line, write a program to calculate the maximum possible score.

입력

The first line contains an integer TT, the number of test cases (1T481 \le T \le 48). The test cases follow.

The first line of each test case contains a single integer NN, the number of students (1N300,0001 \le N \le 300\\,000).

In the next line, the sequence of class numbers are given. The class numbers are integers from 11 to NN.

The sum of NN over all test cases does not exceed 7,000,0007\\,000\\,000.

출력

For each test case, print a line with a single integer: the maximum possible score.