CF2205A.Simons and Making It Beautiful

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

Simons and Making It Beautiful

Those cling-on troubles that ride my stride — cough up the fee, or step aside.— SHUN, TAKE IT AWAY

For a permutation^{\text{∗}} rr of length mm, we call an index ii (1im1\le i\le m) ugly if and only if i=max({r1,r2,,ri})i=\max(\{r_1,r_2,\ldots,r_i\}).

Simons has a permutation pp of length nn, and he can perform the following operation on pp at most once:

  • Choose two indices ii and jj (1ijn1\le i\ne j\le n), then swap pip_i and pjp_j.

Find a permutation qq that can be obtained from pp by performing the above operation at most once, such that the number of ugly indices in qq is minimized.

^{\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

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1001 \le t \le 100). The description of the test cases follows.

The first line contains an integer nn (1n5001\le n\le 500) — the length of pp.

The second line contains nn integers p1,p2,,pnp_1,p_2,\ldots,p_n (1pin1\le p_i\le n, all pip_i-s are distinct) — the elements of pp.

Output

For each test case, print nn integers q1,q2,,qnq_1,q_2,\ldots,q_n — the permutation you found.

If there are multiple possible permutations, you may output any.

Note

In the first test case, Simons can obtain only two possible permutations: [1,2][1,2] and [2,1][2,1].

  • For permutation [1,2][1,2], we can see 1=max({1})1=\max(\{1\}) and 2=max({1,2})2=\max(\{1,2\}). So there are two ugly indices.
  • For permutation [2,1][2,1], we can see 1max({2})1\ne\max(\{2\}) and 2=max({2,1})2=\max(\{2,1\}). So there is only one ugly index.

Thus, permutation [2,1][2,1] has the minimum count of ugly indices.

In the second test case, Simons can obtain permutation [2,4,1,3][2,4,1,3] by choosing indices 22 and 44 to swap. There is only one ugly index in the permutation:

  • 1max({2})1\ne \max(\{2\}), so index 11 is not ugly;
  • 2max({2,4})2\ne \max(\{2,4\}), so index 22 is not ugly;
  • 3max({2,4,1})3\ne \max(\{2,4,1\}), so index 33 is not ugly;
  • 4=max({2,4,1,3})4=\max(\{2,4,1,3\}), so index 44 is ugly.

In the third test case, note that Simons does not perform the operation.

Samples

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

在线编程 IDE

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