#P1896E. Permutation Sorting

    ID: 8866 Type: RemoteJudge 4000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 7 Uploaded By: Tags>data structuressortings*2100

Permutation Sorting

No submission language available for this problem.

Description

You are given a permutation^\dagger aa of size nn. We call an index ii good if ai=ia_i=i is satisfied. After each second, we rotate all indices that are not good to the right by one position. Formally,

  • Let s1,s2,,sks_1,s_2,\ldots,s_k be the indices of aa that are not good in increasing order. That is, sj<sj+1s_j < s_{j+1} and if index ii is not good, then there exists jj such that sj=is_j=i.
  • For each ii from 11 to kk, we assign as(i%k+1):=asia_{s_{(i \% k+1)}} := a_{s_i} all at once.

For each ii from 11 to nn, find the first time that index ii becomes good.

^\dagger A permutation 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).

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n1061 \le n \le 10^6) — the size of permutation aa.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ain1 \le a_i \le n) — the elements of permutation aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

For each test case, output a single line containing nn integers where the ii-th integer represents the first time that index ii becomes good.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n1061 \le n \le 10^6) — the size of permutation aa.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ain1 \le a_i \le n) — the elements of permutation aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

Output

For each test case, output a single line containing nn integers where the ii-th integer represents the first time that index ii becomes good.

Sample Input 1

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

Sample Output 1

1 0 1 1 0 
2 1 2 1 0 1

Note

In the first test case, 22 and 55 are already in the correct position so indices 22 and 55 become good at 00 second. After 11 second, a cyclic shift will be done with s=[1,3,4]s=[1, 3, 4], resulting in array a=[1,2,3,4,5]a=[1, 2, 3, 4, 5]. Notice that indices 11, 33 and 44 become good at 11 second.

In the second test case, 55 is already in the correct position, so index 55 becomes good at 00 second. After 11 second, a cyclic shift will be done with s=[1,2,3,4,6]s=[1, 2, 3, 4, 6], resulting in array a=[3,2,1,4,5,6]a=[3, 2, 1, 4, 5, 6]. Notice that indices 22, 44 and 66 become good at 11 second. After 22 seconds, a cyclic shift will be done with s=[1,3]s=[1, 3], resulting in array a=[1,2,3,4,5,6]a=[1, 2, 3, 4, 5, 6]. Notice that indices 11 and 33 become good at 22 second.