CF2144B.Maximum Cost Permutation

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Maximum Cost Permutation

Recall that a permutation of length nn is a sequence of length nn such that each integer from 11 to nn appears in it exactly once.

Let's define the cost of a permutation as the minimum length of its contiguous subsegment (possibly empty) that needs to be sorted so that the entire permutation becomes sorted in ascending order.

You are given an integer array pp consisting of integers from 00 to nn, where no positive (strictly greater than zero) integer appears more than once. You should replace zeros with integers so that the array pp becomes a permutation.

Your task is to calculate the maximum possible cost of the resulting permutation.

Input

The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5).

The second line contains nn integers p1,p2,,pnp_1, p_2, \dots, p_n (0pin0 \le p_i \le n). No positive integer appears more than once in this sequence.

Additional constraint on the input: the sum of nn over all test cases doesn't exceed 21052 \cdot 10^5.

Output

For each test case, print a single integer — the maximum possible cost of the resulting permutation.

Note

In the first example, you can make a permutation [1,3,4,2,5][1, 3, 4, 2, 5] with the cost 33, because you have to sort segment [2,4][2, 4].

In the second example, you can make a permutation [2,3,1][2, 3, 1] with the cost 33, because you have to sort segment [1,3][1, 3].

In the third example, there is only one possible permutation — [1,2,3,4][1, 2, 3, 4], with the cost 00, because the permutation is already sorted.

In the fourth example, there is only one possible permutation — [1,3,2][1, 3, 2], with the cost 22, because you have to sort segment [2,3][2, 3].

Samples

4
5
1 0 4 0 5
3
0 0 0
4
1 2 3 0
3
0 3 2
3
3
0
2

在线编程 IDE

建议全屏模式获得最佳体验