CF2001B.Generate Permutation

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Generate Permutation

There is an integer sequence aa of length nn, where each element is initially 1-1.

Misuki has two typewriters where the first one writes letters from left to right, with a pointer initially pointing to 11, and another writes letters from right to left with a pointer initially pointing to nn.

Misuki would choose one of the typewriters and use it to perform the following operations until aa becomes a permutation of [1,2,,n][1, 2, \ldots, n]

  • write number: write the minimum positive integer that isn't present in the array aa to the element aia_i, ii is the position where the pointer points at. Such operation can be performed only when ai=1a_i = -1.
  • carriage return: return the pointer to its initial position (i.e. 11 for the first typewriter, nn for the second)
  • move pointer: move the pointer to the next position, let ii be the position the pointer points at before this operation, if Misuki is using the first typewriter, i:=i+1i := i + 1 would happen, and i:=i1i := i - 1 otherwise. Such operation can be performed only if after the operation, 1in1 \le i \le n holds.

Your task is to construct any permutation pp of length nn, such that the minimum number of carriage return operations needed to make a=pa = p is the same no matter which typewriter Misuki is using.

Input

Each test contains multiple test cases. The first line of input contains a single integer tt (1t5001 \le t \le 500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5) — the length of the permutation.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output a line of nn integers, representing the permutation pp of length nn such that the minimum number of carriage return operations needed to make a=pa = p is the same no matter which typewriter Misuki is using, or 1-1 if it is impossible to do so.

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

Note

In the first testcase, it's possible to make a=p=[1]a = p = [1] using 00 carriage return operations.

In the second testcase, it is possible to make a=p=[1,2]a = p = [1, 2] with the minimal number of carriage returns as follows:

If Misuki is using the first typewriter:

  • Write number: write 11 to a1a_1, aa becomes [1,1][1, -1]
  • Move pointer: move the pointer to the next position. (i.e. 22)
  • Write number: write 22 to a2a_2, aa becomes [1,2][1, 2]

If Misuki is using the second typewriter:

  • Move pointer: move the pointer to the next position. (i.e. 11)
  • Write number: write 11 to a1a_1, aa becomes [1,1][1, -1]
  • Carriage return: return the pointer to 22.
  • Write number: write 22 to a2a_2, aa becomes [1,2][1, 2]

It can be proven that the minimum number of carriage returns needed to transform aa into pp when using the first typewriter is 00 and it is 11 when using the second one, so this permutation is not valid.

Similarly, p=[2,1]p = [2, 1] is also not valid, so there is no solution for n=2n = 2.

In the third testcase, it is possibile to make a=p=[3,1,2]a = p = [3, 1, 2] with 11 carriage return with both the first and the second typewriter. It can be proven that, for both typewriters, it is impossible to write pp with 00 carriage returns.

With the first typewriter it is possible to:

  • Move pointer: move the pointer to the next position. (i.e. 22)
  • Write number: write 11 to a2a_2, aa becomes [1,1,1][-1, 1, -1]
  • Move pointer: move the pointer to the next position. (i.e. 33)
  • Write number: write 22 to a3a_3, aa becomes [1,1,2][-1, 1, 2]
  • Carriage return: return the pointer to 11.
  • Write number: write 33 to a1a_1, aa becomes [3,1,2][3,1,2]

With the second typewriter it is possible to:

  • Move pointer: move the pointer to the next position. (i.e. 22)
  • Write number: write 11 to a2a_2, aa becomes [1,1,1][-1, 1, -1]
  • Carriage return: return the pointer to 33.
  • Write number: write 22 to a3a_3, aa becomes [1,1,2][-1, 1, 2]
  • Move pointer: move the pointer to the next position. (i.e. 22)
  • Move pointer: move the pointer to the next position. (i.e. 11)
  • Write number: write 33 to a1a_1, aa becomes [3,1,2][3, 1, 2]

Samples

3
1
2
3
1
-1
3 1 2

在线编程 IDE

建议全屏模式获得最佳体验