#include<bits/stdc++.h>
// code by CHOW
using namespace std;
// Speed
#define code ios_base::sync_with_stdio(false);
#define by cin.tie(NULL);
#define RR cout.tie(NULL);
// Aliases
using ll = long long;
using lld = long double;
using ull = unsigned long long;
// Constants
const lld pi = 3.141592653589793238;
const ll INF = LONG_LONG_MAX;
const ll mod = 1e9 + 7;
// Macros
#define pb push_back
#define mp make_pair
// Mathematical functions
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a / gcd(a, b) * b); }
ll moduloMultiplication(ll a, ll b, ll mod)
{
ll res = 0;
a %= mod;
while (b)
{
if (b & 1)
res = (res + a) % mod;
b >>= 1;
}
return res;
}
ll powermod(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
// DP
int longestCommonSubstringLength(string s1, string s2) {
int m = s1.length(), n = s2.length();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
int maxLength = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
maxLength = max(maxLength, dp[i][j]);
}
}
}
return maxLength;
}
int longestCommonSubsequenceLength(string s1, string s2) {
int m = s1.length(), n = s2.length();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
// Sorting
bool sorta(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); }
bool sortd(const pair<int, int> &a, const pair<int, int> &b) { return (a.second > b.second); }
// Bits
string decToBinary(int n)
{
string s = "";
int i = 0;
while (n > 0)
{
s = to_string(n % 2) + s;
n = n / 2;
i++;
}
return s;
}
ll binaryToDecimal(string n)
{
string num = n;
ll dec_value = 0;
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--)
{
if (num[i] == '1')
dec_value += base;
base = base * 2;
}
return dec_value;
}
ll HeightPowerOfTwo(ll n)
{
int k =0;
while(n>=(1LL<<k)){
k++;
}
return k-1;
}
// Check
bool isSubstring(const std::string &s1, const std::string &s2)
{
// Use the find function to search for s1 in s2
return s2.find(s1) != std::string::npos;
}
bool isPrime(ll n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
bool isPowerOfTwo(int n)
{
if (n == 0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
bool isPerfectSquare(ll x)
{
if (x >= 0)
{
ll sr = sqrt(x);
return (sr * sr == x);
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll t;
cin >> t;
while (t--) {
ll n;
cin>>n;
// handle differently for[1,5] cases.
if(n<=5){
if(n==1){
cout<<0<<endl;
cout<<1<<endl;
}
if(n==2){
cout<<2<<endl;
cout<<1<<" "<<2<<endl;
}
if(n==3){
cout<<2<<endl;
cout<<1<<" "<<2<<" "<<3<<endl;
}
if(n==4){
cout<<6<<endl;
cout<<1<<" "<<2<<" "<<3<<" "<<4<<endl;
}
if(n==5){
cout<<5<<endl;
cout<<"2 1 3 4 5"<<endl;
}
continue;
}
if(!((n-1)&n-2)){ // means odd that is just +1 the perfect power of 2.
ll t = HeightPowerOfTwo(n-1);
ll sl= 1LL<<t;
ll last = sl-1;
cout<<n<<endl;
t = HeightPowerOfTwo(n-2);
sl= 1<<t;
last = sl-1;
for(int i =1; i<n-2; i++){
if(i==last) continue;
else{
cout<<i<<" ";
}
}
cout<<last<<" "<<n-2<<" "<<n-1<<" "<<n<<endl;
}
else if(n&1){
// handling for odd cases
cout<<n<<endl;
ll t = HeightPowerOfTwo(n);
ll sl= 1LL<<t;
ll last = sl-1;
for(int i =1; i<n; i++){
if(i==last) continue;
else{
cout<<i<<" ";
}
}
cout<<last<<" "<<n<<endl;
}
else if(!(n&n-1)){
// it is like making the odd wala case
ll t = HeightPowerOfTwo(n);
ll sl= 1LL<<t;
ll last = sl-1;
cout<<last+n<<endl;
t= HeightPowerOfTwo(n-1);
sl = 1<<t;
last = sl-1;
for(int i =1; i<n-1; i++){
if(i==last) continue;
else{
cout<<i<<" ";
}
}
cout<<last<<" "<<n-1<<" "<<n<<endl;
}
// simple even wala case
else{
ll t = HeightPowerOfTwo(n);
ll sl= 1LL<<t;
ll last = sl-1;
cout<<sl+last<<endl;
for(int i=1; i<=n; i++){
if(i==last) continue;
else{
cout<<i<<" ";
}
}
cout<<last<<endl;
}
}
}