Codeforces Round 1075 (Div. 2)
C2
XOR-convenience (Hard Version)
Codeforces
1800
原题
← C1D1 →
翻译中…

This is the hard version of the problem. The difference between the versions is that in this version, $1 \le i \le n-1$. Note that a correct solution for the hard version is not necessarily a correct solution for the easy version.

翻译中…

Given a positive integer $n$. Find some permutation$^{\text{∗}}$ $p$ of length $n$ such that for every $i$ ($\style{color:red}{1 \le i \le n-1}$) there exists $j$ ($\style{color:red}{i \le j \le n}$) such that $p_i = p_j \oplus i$ $^{\text{†}}$, or determine that it does not exist.

翻译中…

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array). $^{\text{†}}$$\oplus$ denotes the bitwise XOR operation.

翻译中…

Input

翻译中…

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

翻译中…

The only line of each test case contains one positive integer $n$ ($3 \leq n \leq 2 \cdot 10^5$) — the length of the permutation.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, if there is a suitable permutation, output $n$ integers $p_1,p_2,\ldots,p_n$ — the permutation $p$. Otherwise, output $-1$.

翻译中…

If multiple solutions exist, output any of them.

翻译中…

Note

翻译中…

In the first test case, the permutation $p = [2,1,3]$ fulfills the condition, as $p_2 = 1$ and $p_3 \oplus 2 = 1$.

翻译中…

In the second test case, there is no suitable permutation.

翻译中…

样例

Input #1
2
3
4
Output #1
2 1 3
-1
InputCopy OutputCopy
题目信息
题号
2189C2
时间限制
2 s
内存限制
256 MB
标签
位运算
构造
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。