COMFUNC - Commuting Functions

no tags 

Two functions f and g (f, g: X → X) are commuting if and only if f(g(x)) = g(f(x)) for each x in X. For example, functions f(x) = x + 1 and g(x) = x − 2 are commuting, whereas functions f(x) = x + 1 and g(x) = 2x are not commuting.

Each function h (h: Nn → Nn, where Nn = {1, 2, ... n} and n is positive integer) can be represented as a value list — a list in which the i-th element is equal to h(i). For example, a function h(x) = ceil(x/2) from N5 to N5 has the value list [1, 1, 2, 2, 3].

The value lists are ordered lexicographically: list [a1 ... an] is smaller than list [b1 ... bn] if and only if there exists such an index k that ak < bk, and al = bl for any index l < k.

The function f (f: X → X) is bijective if for every y in X, there is exactly one x in X such that f(x) = y.

Given a bijective function f (f: Nn → Nn, n is positive integer), find the function g that is commuting with f and has the lexicographically smallest possible value list.

Input

The first line of the input file contains the number of test cases. Each test case is described by a line containing a single integer number n — the number of the elements in the value list of a bijective function f (1 ≤ n ≤ 200000), followed by another line which contains the value list of the function f.

Output

For each test case, output a single line containing n integer numbers — the value list of function g that commutes with the function f and has the lexicographically smallest value list.

Example

Input:
2
10
1 2 3 4 5 6 7 8 9 10
10
2 3 4 5 6 7 8 1 9 10

Output:
1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 9

hide comments
Danny Sleator: 2010-11-11 15:35:21

That was my problem. Thanks for clearing it up.

Fidel Schaposnik: 2010-11-11 12:32:40

I don't really understand Ocaml, but from what I gather looking at your code it seems you are not reading multiple test cases in your solution. This is the only difference between the dataset used here and the one used in the original contest...

Danny Sleator: 2010-11-11 03:20:14

I'm getting an error "runtime error (SIGSEGV)" on my submission to this problem. However, my solution works perfectly, and in a small fraction of a second on the data used in the actual contest. My solution is on Ocaml. Can somebody give me a little more info on what is happening? Thanks.


Added by:Fidel Schaposnik
Date:2010-11-08
Time limit:0.307s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:ACM ICPC 2010, NEERC, Northern Subregional Contest