Permutation Weight (Easy Version)
Contest: Codeforces Round 794 (Div. 1)
Difficulty rating: 2800
Tags: constructive algorithms, dfs and similar, dsu

This is an easy version of the problem. The difference between the easy and hard versions is that in this version, you can output any permutation with the smallest weight.

You are given a permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$.

Let's define the weight of the permutation $$$q_1, q_2, \ldots, q_n$$$ of integers from $$$1$$$ to $$$n$$$ as $$$$$$|q_1 - p_{q_{2}}| + |q_2 - p_{q_{3}}| + \ldots + |q_{n-1} - p_{q_{n}}| + |q_n - p_{q_{1}}|$$$$$$

You want your permutation to be as lightweight as possible. Find any permutation $$$q$$$ with the smallest possible weight.

Input format:
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 200$$$)  — the size of the permutation.

The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct)  — the elements of the permutation.

The sum of $$$n$$$ over all test cases doesn't exceed $$$400$$$.

Output format:
For each test case, output $$$n$$$ integers $$$q_1, q_2, \ldots, q_n$$$ ($$$1 \le q_i \le n$$$, all $$$q_i$$$ are distinct)  — one of the permutations with the smallest weight.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, there are two permutations of length $$$2$$$: $$$(1, 2)$$$ and $$$(2, 1)$$$. Permutation $$$(1, 2)$$$ has weight $$$|1 - p_2| + |2 - p_1| = 0$$$, and permutation $$$(2, 1)$$$ has the same weight: $$$|2 - p_1| + |1 - p_2| = 0$$$. You can output any of these permutations in this version.

In the second test case, the weight of the permutation $$$(1, 3, 4, 2)$$$ is $$$|1 - p_3| + |3 - p_4| + |4 - p_2| + |2 - p_1| = |1 - 1| + |3 - 4| + |4 - 3| + |2 - 2| = 2$$$. There are no permutations with smaller weights.

In the third test case, the weight of the permutation $$$(1, 4, 2, 3, 5)$$$ is $$$|1 - p_4| + |4 - p_2| + |2 - p_3| + |3 - p_5| + |5 - p_1| = |1 - 2| + |4 - 4| + |2 - 3| + |3 - 1| + |5 - 5| = 4$$$. There are no permutations with smaller weights.

Example input:
3
2
2 1
4
2 3 1 4
5
5 4 3 2 1

Example output:
1 2 
1 3 4 2 
1 4 2 3 5

Editorial:
Let's first understand what is the minimum possible weight of $$$q$$$. When can it be $$$0$$$?
Only when $$$q_i = p_{q_{i+1}}$$$ for each $$$i$$$. Clearly, such $$$q$$$ exists only when $$$p$$$ is just one cycle. This gives a hint that we should look at cycles.
Consider splitting of $$$p$$$ into cycles (where a cycle is an array $$$[a_1, a_2, \ldots, a_m]$$$ such that $$$p_{a_i} = a_{i \bmod m +1}$$$ for $$$1 \le i \le m$$$). Let's say there are $$$k$$$ of such cycles. I claim that the answer is $$$2(k-1)$$$. You can see the proof in the tutorial of the hard version of this problem.
Now, let's provide an example. We will construct a permutation $$$p'$$$ as follows:
• Initially, it's equal to $$$p$$$
• Then, for each $$$x$$$ from $$$1$$$ to $$$n-1$$$, if $$$x$$$ and $$$x+1$$$ are in different cycles in $$$p'$$$ currently, swap them.
One such swap reduces the number of cycles by exactly $$$1$$$, so we will do exactly $$$k-1$$$ such swaps and $$$p'$$$ will consist of exactly one cycle.
Next, construct $$$q$$$ by the rule $$$q_i = p'_{q_{i+1}}$$$ (it's possible as $$$p'$$$ is just one cycle). As $$$|q_i - p_{q_{i+1}}| = |p'_{q_{i+1}} - p_{q_{i+1}}|$$$, the weight of $$$q$$$ is just the sum of $$$|p_i - p'_i|$$$. Clearly, one swap increases this value by at most $$$2$$$, so in the end it will be at most $$$2(k-1)$$$, as desired.