#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to calculate the value of k for a given permutation
int calculateK(const vector<int>& p) {
int k = 0;
int n = p.size();
for (int i = 0; i < n; ++i) {
if ((i + 1) % 2 == 1) { // i+1 is odd
k &= p[i];
} else { // i+1 is even
k |= p[i];
}
}
return k;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> perm(n);
for (int i = 0; i < n; ++i) {
perm[i] = i + 1;
}
int maxK = 0;
vector<int> bestPerm = perm;
// Try all permutations
do {
int currentK = calculateK(perm);
if (currentK > maxK) {
maxK = currentK;
bestPerm = perm;
}
} while (next_permutation(perm.begin(), perm.end()));
// Output the maximum value of k
cout << maxK << endl;
// Output the permutation that gives the maximum k
for (int i = 0; i < n; ++i) {
cout << bestPerm[i] << " ";
}
cout << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vIEZ1bmN0aW9uIHRvIGNhbGN1bGF0ZSB0aGUgdmFsdWUgb2YgayBmb3IgYSBnaXZlbiBwZXJtdXRhdGlvbgppbnQgY2FsY3VsYXRlSyhjb25zdCB2ZWN0b3I8aW50PiYgcCkgewogICAgaW50IGsgPSAwOwogICAgaW50IG4gPSBwLnNpemUoKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICAgICAgaWYgKChpICsgMSkgJSAyID09IDEpIHsgIC8vIGkrMSBpcyBvZGQKICAgICAgICAgICAgayAmPSBwW2ldOwogICAgICAgIH0gZWxzZSB7ICAgICAgICAgICAgICAgICAvLyBpKzEgaXMgZXZlbgogICAgICAgICAgICBrIHw9IHBbaV07CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIGs7Cn0KCmludCBtYWluKCkgewogICAgaW50IHQ7CiAgICBjaW4gPj4gdDsKICAgIAogICAgd2hpbGUgKHQtLSkgewogICAgICAgIGludCBuOwogICAgICAgIGNpbiA+PiBuOwoKICAgICAgICB2ZWN0b3I8aW50PiBwZXJtKG4pOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICAgICAgICAgIHBlcm1baV0gPSBpICsgMTsKICAgICAgICB9CgogICAgICAgIGludCBtYXhLID0gMDsKICAgICAgICB2ZWN0b3I8aW50PiBiZXN0UGVybSA9IHBlcm07CgogICAgICAgIC8vIFRyeSBhbGwgcGVybXV0YXRpb25zCiAgICAgICAgZG8gewogICAgICAgICAgICBpbnQgY3VycmVudEsgPSBjYWxjdWxhdGVLKHBlcm0pOwogICAgICAgICAgICBpZiAoY3VycmVudEsgPiBtYXhLKSB7CiAgICAgICAgICAgICAgICBtYXhLID0gY3VycmVudEs7CiAgICAgICAgICAgICAgICBiZXN0UGVybSA9IHBlcm07CiAgICAgICAgICAgIH0KICAgICAgICB9IHdoaWxlIChuZXh0X3Blcm11dGF0aW9uKHBlcm0uYmVnaW4oKSwgcGVybS5lbmQoKSkpOwoKICAgICAgICAvLyBPdXRwdXQgdGhlIG1heGltdW0gdmFsdWUgb2YgawogICAgICAgIGNvdXQgPDwgbWF4SyA8PCBlbmRsOwogICAgICAgIC8vIE91dHB1dCB0aGUgcGVybXV0YXRpb24gdGhhdCBnaXZlcyB0aGUgbWF4aW11bSBrCiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICAgICAgY291dCA8PCBiZXN0UGVybVtpXSA8PCAiICI7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgZW5kbDsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=