CF1858C.Yet Another Permutation Problem

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

Yet Another Permutation Problem

Alex got a new game called "GCD permutations" as a birthday present. Each round of this game proceeds as follows:

  • First, Alex chooses a permutation^{\dagger} a1,a2,,ana_1, a_2, \ldots, a_n of integers from 11 to nn.
  • Then, for each ii from 11 to nn, an integer di=gcd(ai,a(imodn)+1)d_i = \gcd(a_i, a_{(i \bmod n) + 1}) is calculated.
  • The score of the round is the number of distinct numbers among d1,d2,,dnd_1, d_2, \ldots, d_n.

Alex has already played several rounds so he decided to find a permutation a1,a2,,ana_1, a_2, \ldots, a_n such that its score is as large as possible.

Recall that gcd(x,y)\gcd(x, y) denotes the greatest common divisor (GCD) of numbers xx and yy, and xmodyx \bmod y denotes the remainder of dividing xx by yy.

^{\dagger}A permutation of length nn 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).

Input

The first line of the input contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

Each test case consists of one line containing a single integer nn (2n1052 \le n \le 10^5).

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

Output

For each test case print nn distinct integers a1,a2,,ana_{1},a_{2},\ldots,a_{n} (1ain1 \le a_i \le n) — the permutation with the largest possible score.

If there are several permutations with the maximum possible score, you can print any one of them.

Note

In the first test case, Alex wants to find a permutation of integers from 11 to 55. For the permutation a=[1,2,4,3,5]a=[1,2,4,3,5], the array dd is equal to [1,2,1,1,1][1,2,1,1,1]. It contains 22 distinct integers. It can be shown that there is no permutation of length 55 with a higher score.

In the second test case, Alex wants to find a permutation of integers from 11 to 22. There are only two such permutations: a=[1,2]a=[1,2] and a=[2,1]a=[2,1]. In both cases, the array dd is equal to [1,1][1,1], so both permutations are correct.

In the third test case, Alex wants to find a permutation of integers from 11 to 77. For the permutation a=[1,2,3,6,4,5,7]a=[1,2,3,6,4,5,7], the array dd is equal to [1,1,3,2,1,1,1][1,1,3,2,1,1,1]. It contains 33 distinct integers so its score is equal to 33. It can be shown that there is no permutation of integers from 11 to 77 with a score higher than 33.

Samples

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

在线编程 IDE

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