This is the easy version of the problem. The difference between the versions is that in this version, $$$t \le 1000$$$, $$$n \le 5000$$$ and the sum of $$$n$$$ does not exceed $$$5000$$$. You can hack only if you solved all versions of this problem.
For a non-empty sequence $$$c$$$ of length $$$k$$$, define $$$f(c)$$$ as follows:
For a permutation $$$p$$$ of length $$$n$$$$$$^{\text{∗}}$$$, Turtle defines the beauty of the permutation as $$$\sum\limits_{i = 1}^n \sum\limits_{j = i}^n f([p_i, p_{i + 1}, \ldots, p_j])$$$ (i.e., the sum of $$$f(c)$$$ where $$$c$$$ is a non-empty subsegment$$$^{\text{†}}$$$ of $$$p$$$).
Piggy gives Turtle a permutation $$$a$$$ of length $$$n$$$ where some elements are missing and represented by $$$0$$$.
Turtle asks you to determine a permutation $$$b$$$ of length $$$n$$$ such that:
For convenience, you only need to find the maximum beauty of such permutation $$$b$$$.
$$$^{\text{∗}}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
$$$^{\text{†}}$$$A sequence $$$a$$$ is a subsegment of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 5000$$$).
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le n$$$). It is guaranteed that the elements of $$$a$$$ that are not $$$0$$$ are distinct.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$.
For each test case, output a single integer — the maximum beauty of the permutation $$$b$$$.
821 030 0 030 1 053 2 4 5 170 3 2 5 0 0 0101 2 6 5 8 9 0 0 0 050 4 1 0 050 1 5 2 3
4 12 11 44 110 300 45 40
In the first test case, the permutation $$$b$$$ with the maximum beauty is $$$[1, 2]$$$. The beauty of $$$[1, 2]$$$ is $$$4$$$ since $$$f([1]) + f([2]) + f([1, 2]) = 1 + 2 + 1 = 4$$$. If $$$c = [1, 2]$$$, then $$$f(c) = 1$$$ since Turtle can only choose $$$i = 1$$$ and he will set $$$c_1$$$ to $$$\min(c_1, c_2) = 1$$$.
In the second test case, one of the permutations $$$b$$$ with the maximum beauty is $$$[3, 2, 1]$$$. The beauty of $$$[3, 2, 1]$$$ is $$$12$$$ since $$$f([3]) + f([2]) + f([1]) + f([3, 2]) + f([2, 1]) + f([3, 2, 1]) = 3 + 2 + 1 + 2 + 1 + 3 = 12$$$.
In the third test case, one of the permutations $$$b$$$ with the maximum beauty is $$$[2, 1, 3]$$$.
In the fourth test case, if $$$c = [3, 2, 4, 5, 1]$$$, then $$$f(c) = 3$$$. One of the possible game processes is as follows:
In the fifth test case, one of the permutations $$$b$$$ with the maximum beauty is $$$[1, 3, 2, 5, 6, 4, 7]$$$.