#include <bits/stdc++.h>
using namespace std;
#define ll long long
// FUCK BITWISE
vector<int> pro(int n) {
int x = log2(n);
int y = (1 << x);
vector<int> ans;
ans.push_back(y);
ans.push_back(y - 1);
ans.push_back(y - 2);
ans.push_back(3);
ans.push_back(1);
ans.push_back(2);
for (int i = 4; i < y - 2; ++i) ans.push_back(i);
for (int i = y + 1; i <= n; ++i) ans.push_back(i);
reverse(ans.begin(), ans.end());
return ans;
}
void solve() {
int n; cin >> n;
if (n % 2 == 0) {
vector<int> ans = pro(n - 1);
ans.push_back(n);
cout << n << '\n';
for (auto it : ans) cout << it << ' ';
cout << '\n';
} else {
vector<int> ans = pro(n);
int k = 1 << ((int)log2(n) + 1);
k--;
cout << k << '\n';
for (auto it : ans) cout << it << ' ';
cout << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while (t--) solve();
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGxsIGxvbmcgbG9uZwovLyBGVUNLIEJJVFdJU0UKCnZlY3RvcjxpbnQ+IHBybyhpbnQgbikgewogICAgaW50IHggPSBsb2cyKG4pOwogICAgaW50IHkgPSAoMSA8PCB4KTsKICAgIHZlY3RvcjxpbnQ+IGFuczsKICAgIGFucy5wdXNoX2JhY2soeSk7CiAgICBhbnMucHVzaF9iYWNrKHkgLSAxKTsKICAgIGFucy5wdXNoX2JhY2soeSAtIDIpOwogICAgYW5zLnB1c2hfYmFjaygzKTsKICAgIGFucy5wdXNoX2JhY2soMSk7CiAgICBhbnMucHVzaF9iYWNrKDIpOwogICAgZm9yIChpbnQgaSA9IDQ7IGkgPCB5IC0gMjsgKytpKSBhbnMucHVzaF9iYWNrKGkpOwogICAgZm9yIChpbnQgaSA9IHkgKyAxOyBpIDw9IG47ICsraSkgYW5zLnB1c2hfYmFjayhpKTsKICAgIHJldmVyc2UoYW5zLmJlZ2luKCksIGFucy5lbmQoK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