You are given an array a consisting of n n n integers.
In one move, you can choose some index i ( 1 ≤ i ≤ n − 2 ) i (1≤i≤n−2) i(1≤i≤n−2) and shift the segment [ a i , a i + 1 , a i + 2 ] [a_i,a_{i+1},a_{i+2}] [ai,ai+1,ai+2] cyclically to the right (i.e. replace the segment [ a i , a i + 1 , a i + 2 ] [a_i,a_{i+1},a_{i+2}] [ai,ai+1,ai+2] with [ a i + 2 , a i , a i + 1 ] [a_{i+2},a_i,a_{i+1}] [ai+2,ai,ai+1]).
Your task is to sort the initial array by no more than n 2 n_2 n2 such operations or say that it is impossible to do that.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t ( 1 ≤ t ≤ 100 ) t (1≤t≤100) t(1≤t≤100) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n ( 3 ≤ n ≤ 500 ) n (3≤n≤500) n(3≤n≤500) — the length of a. The second line of the test case contains n integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 500 ) a_1,a_2,…,a_n (1≤a_i≤500) a1,a2,…,an(1≤ai≤500), where a i a_i ai is the i i i-th element a.
It is guaranteed that the sum of n does not exceed 500 500 500.
Output
For each test case, print the answer: − 1 -1 −1 on the only line if it is impossible to sort the given array using operations described in the problem statement, or the number of operations ans on the first line and a n s ans ans integers i d x 1 , i d x 2 , … , i d x a n s idx_1,idx_2,…,idx_{ans} idx1,idx2,…,idxans ( 1 ≤ i d x i ≤ n − 2 ) (1≤idx_i≤n−2) (1≤idxi≤n−2), where i d x i idx_i idxi is the index of left border of the segment for the i i i-th operation. You should print indices in order of performing operations.
Example
input
5
5
1 2 3 4 5
5
5 4 3 2 1
8
8 4 5 2 3 6 7 3
7
5 2 1 6 4 7 3
6
1 2 3 3 6 4
output
0
6
3 1 3 2 2 3
13
2 1 1 6 4 2 4 3 3 4 4 6 6
-1
4
3 3 4 4
首先,对于前 n − 2 n-2 n−2小的数,一定存在方法将其由小到大依次移动到特定位置,这样就把前 n − 2 n-2 n−2小的数排好了。对于后两个数,如果顺序递增,则已经排好,否则要么两个数中较小的与数在已排好的序列里存在或者已排好的序列里有重复的元素,都存在策略使其排好,如果两种情况都没有,则不能排好。
#include
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector
#define pii pair
#define mii unordered_map
#define msi unordered_map
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 505;
int T, n, m;
vi ans, seq;
int a[N];
int now;
inline void move(int x) {
int pos;
now++;
if (m - now == 1) {
if (a[1] > a[2]) {
if (m == 2) {
puts("-1");
return;
}
if (a[2] == seq[now - 2])ans.pb(now - 1);
else {
int j = -1;
repd(i, now - 2, 1)if (seq[i] == seq[i - 1]) {
j = i + 2;
break;
}
if (j == -1) {
puts("-1");
return;
}
if ((now & 1) == (j & 1)) {
for (int i = now - 2; i >= j; i -= 2)ans.pb(i);
for (int i = now - 1; i >= j + 1; i -= 2)ans.pb(i);
ans.pb(j - 2), ans.pb(j - 2);
ans.pb(j - 1), ans.pb(j - 1);
for (int i = j + 1; i <= now - 1; i += 2)ans.pb(i), ans.pb(i);
for (int i = j; i <= now - 2; i += 2)ans.pb(i), ans.pb(i);
} else {
for (int i = now - 1; i >= j; i -= 2)ans.pb(i);
for (int i = now - 1; i >= j; i -= 2)ans.pb(i);
ans.pb(j - 2), ans.pb(j - 2), ans.pb(j - 1);
for (int i = j; i <= now - 1; i += 2)ans.pb(i), ans.pb(i);
for (int i = j - 1; i <= now - 2; i += 2)ans.pb(i), ans.pb(i);
}
}
}
pi(ans.size());
for (auto it:ans)printf("%d ", it);
puts("");
return;
}
repi(i, 1, n)if (a[i] == x) {
pos = i;
break;
}
n--;
repi(i, pos, n)a[i] = a[i + 1];
for (int i = now + pos - 3; i >= now; i -= 2)ans.pb(i);
if (!(pos & 1)) {
ans.pb(now), ans.pb(now);
swap(a[1], a[2]);
}
}
int main() {
T = qr();
while (T--) {
ans.clear(), seq.clear(), now = 0;
n = qr(), m = n;
repi(i, 1, n)a[i] = qr(), seq.pb(a[i]);
if (n == 1) {
puts("0\n");
continue;
}
sort(seq.begin(), seq.end());
repi(i, 0, m - 2)move(seq[i]);
}
return 0;
}