#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf=1e9+7;
const int maxn=2e5+100;

ll ans[maxn];

void init(){
    for(ll i=2;i<100010;i++){
        ans[i]=ans[i-1]+(i-1)*(i-1)+(i-2)*(i-2)-((i-1)/2)*((i-1)/2);
    }
}


void solve(){
    int n;
    scanf("%d",&n);
    printf("%lld\n",ans[n]);
    vector<int> a(n+1),b[2];
    for(int i=1;i<=n;i++)a[i]=i;
    int l=2,r=n-1;
    b[0].push_back(1),b[1].push_back(n);swap(a[1],a[n]);
    while(l<=r){
        if(l<=r){swap(a[l++],a[n]);b[0].push_back(l-1),b[1].push_back(n);}
        if(l<=r){swap(a[r--],a[1]);b[0].push_back(r+1),b[1].push_back(1);}
    }
    for(int i=1;i<n;i++)printf("%d ",a[i]);
    printf("%d\n%d\n",a[n],b[0].size());

    for(int i=b[0].size()-1;i>=0;i--){
        printf("%d %d\n",b[0][i],b[1][i]);
    }
}

int main(){
    int t;
    scanf("%d",&t);
    init();
    while(t--){
        solve();
    }
    //system("pause");
}