CF2193B.Reverse a Permutation

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

Reverse a Permutation

A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] and [1,3,4][1,3,4] are not permutations.

You are given a permutation pp of length nn. You can perform the following operation exactly once:

  • Choose two integers l,l, rr (1lrn1\le l\le r\le n).
  • Reverse the segment [l,r][l, r] in the permutation pp.

Your task is to output the lexicographically maximum permutation that can be obtained by performing this operation. A permutation aa is lexicographically greater than a permutation bb if for the first position ii where they differ, it holds that ai>bia_i\gt b_i.

Input

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

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

The second line of each test case contains nn distinct integers p1,p2,...,pnp_1, p_2,...,p_n (1pin)(1\le p_i\le n).

It is guaranteed that the sum of the values of nn across all test cases does not exceed 21052\cdot 10^5.

Output

For each test case, output the lexicographically maximum permutation that can be obtained with one operation.

Note

For the first test case, the best segment is [1,4][1, 4]. After reversing, a=[4,1,2,3]a = [4, 1, 2, 3]. For the second test case, the best segment is [2,3][2, 3]. After reversing, a=[3,2,1]a = [3, 2, 1].

Samples

4
4
3 2 1 4
3
3 1 2
4
4 3 2 1
2
2 1
4 1 2 3 
3 2 1 
4 3 2 1 
2 1 

在线编程 IDE

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