Increasing Income

Time limit: 0.5s Memory limit: 256MB Input: Output:

Everything i did, I did it for me.

  • Walter White, Breaking Bad

Walter always says he is doing everything for his family. But in reality, due to his ego, he became obsessed with making as much money as possible.

It turned out that his income would be equal to the maximal possible value of f((q1,q2,…,qn))+f((pq1,pq2,…,pqn))f((q_1, q_2, \ldots, q_n)) + f((p_{q_1}, p_{q_2}, \ldots, p_{q_n})) over all permutations (q1,q2,…,qn)(q_1, q_2, \ldots, q_n) of integers from 11 to nn.

Here p1,p2,…,pnp_1, p_2, \ldots, p_n is Walt's favorite permutation, and f(a)f(a) is defined as the number of positions 1≀i≀n1 \le i \le n, for which ai=max⁑(a1,a2,…,ai)a_i = \max(a_1, a_2, \ldots, a_i): in other words, the number of prefix maximums.

Find any permutation (q1,q2,…,qn)(q_1, q_2, \ldots, q_n) of integers from 11 to nn that maximizes Walt's income. If there are many such permutations, find any of them.

Input data

The first line contains a single integer tt (1≀t≀1051 \le t \le 10^5) - the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (1≀n≀2β‹…1051 \leq n \leq 2\cdot 10^5) - the length of Walt's favorite permutation.

The second line of each test case contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≀pi≀n1 \le p_i \le n, all pip_i are distinct) - elements of the permutation.

It is guaranteed that the sum of nn over all test cases does not exceed 2β‹…1052\cdot 10^5.

Output data

For each test case, output nn integers q1,q2,…,qnq_1, q_2, \ldots, q_n (1≀qi≀n1 \le q_i \le n, all qiq_i are distinct) - any permutation of integers from 11 to nn, which maximizes the value of f((q1,q2,…,qn))+f((pq1,pq2,…,pqn))f((q_1, q_2, \ldots, q_n)) + f((p_{q_1}, p_{q_2}, \ldots, p_{q_n}))

Example 1

stdin

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

stdout

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

Explanation

In the first test case, for q=(1,2,3)q = (1, 2, 3), the value is f(1,2,3)+f(p1,p2,p3)=f(1,2,3)+f(1,2,3)=3+3=6f(1, 2, 3) + f(p_1, p_2, p_3) = f(1, 2, 3) + f(1, 2, 3) = 3 + 3 = 6.

In the second test case, for q=(1,3,4,2)q = (1, 3, 4, 2), the value is f(1,3,4,2)+f(p1,p3,p4,p2)=f(1,3,4,2)+f(2,3,1,4)=3+3=6f(1, 3, 4, 2) + f(p_1, p_3, p_4, p_2) = f(1, 3, 4, 2) + f(2, 3, 1, 4) = 3 + 3 = 6.

In the third test case, for q=(1,3,5,4,2)q = (1, 3, 5, 4, 2), the value is f(1,3,5,4,2)+f(p1,p3,p5,p4,p2)=f(1,3,5,4,2)+f(1,2,3,4,5)=3+5=8f(1, 3, 5, 4, 2) + f(p_1, p_3, p_5, p_4, p_2) = f(1, 3, 5, 4, 2) + f(1, 2, 3, 4, 5) = 3 + 5 = 8.

Log in or sign up to be able to send submissions!