#include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define ll long long
const ll mod = 1000000007;
int lset(int n){
int cnt = 0;
for(int i = 0; i < 32; i++){
if(n&(1<<i)) cnt = i;
}
return cnt+1;
}
int fun(vector<int>&v){
int k = 0;
for(int i = 0; i < v.size(); i++){
if((i+1) & 1) k = k&v[i];
else k = k|v[i];
}
return k;
}
void solve()
{
int n;cin>>n;
vector<int> v;
if(n%2 == 0){
cout << (1<<lset(n))-1 << endl;
int ele = (1<<(lset(n)-1))-1;
for(int i = 1; i <= n; i++) if(i != ele) v.push_back(i);
v.push_back(ele);
} else {
cout << n << endl;
int a = 1,b = 3;
for(int i = 1; i < n-1; i++){
if(i != a && i != b) v.push_back(i);
}
v.push_back(a),v.push_back(b),v.push_back(n-1),v.push_back(n);
}
// 10111
// 10110
cout << fun(v) << endl;
for(auto it:v) cout << it << " ";
cout << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin>>t;
while(t--){
solve();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBhbGwoYSkgYS5iZWdpbigpLGEuZW5kKCkKI2RlZmluZSBhbGxyKGEpIGEucmJlZ2luKCksYS5yZW5kKCkKI2RlZmluZSBsbCBsb25nIGxvbmcKY29uc3QgbGwgbW9kID0gMTAwMDAwMDAwNzsKCmludCBsc2V0KGludCBuKXsKICAgIGludCBjbnQgPSAwOwogICAgZm9yKGludCBpID0gMDsgaSA8IDMyOyBpKyspewogICAgICAgIGlmKG4mKDE8PGkpKSBjbnQgPSBpOwogICAgfQogICAgcmV0dXJuIGNudCsxOwp9CgppbnQgZnVuKHZlY3RvcjxpbnQ+JnYpewogICAgaW50IGsgPSAwOwogICAgZm9yKGludCBpID0gMDsgaSA8IHYuc2l6ZSgpOyBpKyspewogICAgICAgIGlmKChpKzEpICYgMSkgayA9IGsmdltpXTsKICAgICAgICBlbHNlIGsgPSBrfHZbaV07CiAgICB9CiAgICByZXR1cm4gazsgCn0Kdm9pZCBzb2x2ZSgpCnsKICAgIGludCBuO2Npbj4+bjsKICAgIHZlY3RvcjxpbnQ+IHY7CgogICAgaWYobiUyID09IDApewogICAgICAgIGNvdXQgPDwgKDE8PGxzZXQobikpLTEgIDw8IGVuZGw7IAogICAgICAgIGludCBlbGUgPSAoMTw8KGxzZXQobiktMSkpLTE7CiAgICAgICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyBpKyspIGlmKGkgIT0gZWxlKSB2LnB1c2hfYmFjayhpKTsKICAgICAgICB2LnB1c2hfYmFjayhlbGUpOwogICAgfSBlbHNlIHsKICAgICAgICBjb3V0IDw8IG4gPDwgZW5kbDsKICAgICAgICBpbnQgYSA9IDEsYiA9IDM7CiAgICAgICAgZm9yKGludCBpID0gMTsgaSA8IG4tMTsgaSsrKXsKICAgICAgICAgICAgaWYoaSAhPSBhICYmIGkgIT0gYikgdi5wdXNoX2JhY2soaSk7CiAgICAgICAgfQogICAgICAgIHYucHVzaF9iYWNrKGEpLHYucHVzaF9iYWNrKGIpLHYucHVzaF9iYWNrKG4tMSksdi5wdXNoX2JhY2sobik7CiAgICB9CgogICAgLy8gMTAxMTEKICAgIC8vIDEwMTEwCgogICAgY291dCA8PCBmdW4odikgPDwgZW5kbDsKICAgIGZvcihhdXRvIGl0OnYpIGNvdXQgPDwgaXQgPDwgIiAiOwogICAgY291dCA8PCBlbmRsOwp9CgppbnQgbWFpbigpCnsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoMCk7CiAgICAKICAgIGludCB0ID0gMTsKICAgIGNpbj4+dDsKICAgIHdoaWxlKHQtLSl7CiAgICAgICAgc29sdmUoKTsKICAgIH0KICAgIHJldHVybiAwOwp9