#CF2239C. 复兴 / C. Revival

复兴 / C. Revival

C. Revival

Source: Codeforces 2239C
Contest: Codeforces Round 1105 (Div. 1)
Time limit: 2.5 seconds
Memory limit: 256 megabytes

Statement

Consider a permutation pp of 1,2,,n1, 2, \ldots, n. Let sis_i denote the number of inversions in the entire prefix p1,p2,,pip_1, p_2, \ldots, p_i, defined as:

si=1x<yi[px>py],s_i = \sum_{1 \le x \lt y \le i} [p_x \gt p_y],

where the square parantheses denote the Iverson bracket notation.

For each position ii (1in1 \le i \le n), you are given a condition in the form of either pi=xp_i = x or si=xs_i = x. Your task is to reconstruct the original permutation pp.

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.

For each test case: The first line contains a single integer nn (1n21051 \le n \le 2\cdot 10^5). Each of the following nn lines contains a character cc (c{’p’,’s’}c \in \{\text{'p'}, \text{'s'}\}) and an integer xx:

  • If c=’p’c = \text{'p'}, it denotes that pi=xp_i = x, where 1xn1 \le x \le n.
  • If c=’s’c = \text{'s'}, it denotes that si=xs_i = x, where 0xi(i1)20 \le x \le \frac{i(i-1)}{2}.

The sum of nn over all test cases does not exceed 21052\cdot 10^5.

It is guaranteed that a valid permutation always exists.

Output

For each test case, output nn integers representing the permutation pp.

If there are multiple valid permutations, you can output any of them.

Note

In the first test case, the values of all elements are explicitly given, so the unique valid permutation is {1,2,3}\{1, 2, 3\}.

In the second test case, we need to reconstruct the permutation from the prefix inversion counts:

  • For position 22, the number of inversions increases by s2s1=10=1s_2 - s_1 = 1 - 0 = 1. This means p2p_2 must be smaller than exactly one element before it, so p1>p2p_1 \gt p_2.
  • For position 33, the number of inversions increases by s3s2=21=1s_3 - s_2 = 2 - 1 = 1. This means p3p_3 is smaller than exactly one element before it.

The only permutation satisfying these conditions is {3,1,2}\{3, 1, 2\}. In the third test case, we are given p1=1p_1 = 1 and p3=2p_3 = 2. The only remaining available value for p2p_2 is 33. We can verify that the prefix p1,p2p_1, p_2 (which is 1,31, 3) has 00 inversions, perfectly satisfying the condition s2=0s_2 = 0. Thus, the answer is {1,3,2}\{1, 3, 2\}.

In the fifth test case, the given sis_i values perfectly match i(i1)2\frac{i(i-1)}{2}, which is the maximum possible number of inversions for a prefix of length ii. This implies that every element must be smaller than all elements before it, meaning the permutation is strictly decreasing. Hence, the answer is {6,5,4,3,2,1}\{6, 5, 4, 3, 2, 1\}.

Examples

5
3
p 1
p 2
p 3
3
s 0
s 1
s 2
3
p 1
s 0
p 2
5
p 1
p 4
s 0
p 2
s 4
6
s 0
s 1
s 3
s 6
s 10
s 15
1 2 3
3 1 2
1 3 2
1 4 5 2 3
6 5 4 3 2 1