CF1998B.Minimize Equal Sum Subarrays

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

Minimize Equal Sum Subarrays

It is known that Farmer John likes Permutations, but I like them too!— Sun Tzu, The Art of Constructing Permutations

You are given a permutation^{\text{∗}} pp of length nn.

Find a permutation qq of length nn that minimizes the number of pairs (i,ji, j) (1ijn1 \leq i \leq j \leq n) such that $p_i + p_{i+1} + \ldots + p_j = q_i + q_{i+1} + \ldots + q_j$.

^{\text{∗}}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] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Input

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

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

The following line contains nn space-separated integers p1,p2,,pnp_1, p_2, \ldots, p_n (1pin1 \leq p_i \leq n) — denoting the permutation pp of length nn.

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

Output

For each test case, output one line containing any permutation of length nn (the permutation qq) such that qq minimizes the number of pairs.

Note

For the first test, there exists only one pair (i,ji, j) (1ijn1 \leq i \leq j \leq n) such that $p_i + p_{i+1} + \ldots + p_j = q_i + q_{i+1} + \ldots + q_j$, which is (1,21, 2). It can be proven that no such qq exists for which there are no pairs.

Samples

3
2
1 2
5
1 2 3 4 5
7
4 7 5 1 2 6 3
2 1
3 5 4 2 1
6 2 1 4 7 3 5

在线编程 IDE

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