Alice's Secret Santa
Alice is organising a Secret Santa party for n friends, numbered from 1 to n (Alice and Bob
among them). Every friend buys exactly one gift and hands it to exactly one other friend, and every
friend receives exactly one gift. Nobody may give a gift to themselves. Last year Bob gave Alice a
gift and Alice gave Bob one right back, and everybody agreed that was dull, so this year no two
friends may give gifts to each other.
Formally, find a sequence p_1, p_2, …, p_n, where friend i gives a gift to friend p_i, such
that:
- every number from 1 to
nappears in the sequence exactly once; p_i ≠ ifor everyi;- there are no two friends
i ≠ jwithp_i = jandp_j = i.
Help Alice, or tell her that no such plan exists.
Input
The first line contains t (1 ≤ t ≤ 10^4), the number of parties.
Each of the next t lines contains one integer n (1 ≤ n ≤ 2·10^5), the number of friends at that
party. The sum of n over all parties does not exceed 2·10^5.
Output
For each party, print one line. If a plan exists, print n integers p_1, p_2, …, p_n separated
by spaces. Otherwise print -1.
If there are several valid plans, print any of them.
Notes
In the first example, with 4 friends: friend 1 gives to friend 2, friend 2 to friend 3, friend 3 to
friend 4 and friend 4 to friend 1. Nobody gives to themselves and no two friends exchange gifts with
each other. Other plans, such as 3 4 2 1, are also accepted.
With 1 friend there is nobody to give a gift to, and with 2 friends each one has to give to the
other, which is not allowed, so both answers are -1.