D. Roy Loves Permutation

D. Roy Loves Permutation

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

Time Limit: 1.5 s

Memory Limit: 256.0 MB

Description

You are given a permutation of integers from \(1\) to \(N\). Your task is to rearrange the permutation in such a way that for every consecutive pair of elements \(A_i\) and \(A_{i+1}\) in the array \(A[]\)( \(1 \leq i < N\)) the following two conditions are satisfied:

  1. \(\gcd(A_i, A_{i+1}) = 1\)
  2. \(|A_i - A_{i+1}| > 1\)

You must print any such valid rearrangement of the numbers from 1 to N if it exists.
If no such rearrangement exists, print -1.

Note : gcd means greatest common divisor, for example gcd(2,4)=2, gcd(15,10)=5 etc.
'| |' means absolute value, for example |2-4| = |-2|= 2.

Input

  • The first line contains an integer \(T\)(\(1 \leq T \leq 100\) ) — the number of test cases.
  • Each of the next \(T\) lines contains a single integer \(N\)(\(2 \leq N \leq 2000\)) — the length of the permutation.
  • Sum of \(N\) overall test cases doesn't exceed 2000.

Output

  • For each test case, print:
  • A line containing \(N\) space-separated integers — a valid rearrangement of permutation \(N\). If multiple valid rearrangements exist, you are allowed to print any of them.
  • if no such arrangement exists print -1.

Sample

Input Output
3
4
5
8
-1
2 5 3 1 4 
2 5 8 3 7 4 1 6 

Brain Booster #10

Not Attended
Status
Done
Rule
ACM/ICPC
Problem
6
Start at
2025-06-13 15:30
End at
2025-06-13 18:00
Duration
2.5 hour(s)
Host
Partic.
91