#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;


const int maxn = 1e6 + 5;



void solve() {
    int n;
    cin>>n;
    vector<int> a(n);
    iota(a.begin(),a.end(),0);
    vector<pair<int,int>> ans;

    ll res = 0;

    auto Swap = [&](int i, int j) {
	res += 1ll*(j-i)*(j-i);
	swap(a[i],a[j]);
	if (a[i]==j) {
	    ans.push_back({j,i});
	} else if (a[j]==i) {
	    ans.push_back({i,j});
	} else assert(false);
    };

    Swap(0,n-1);

    for (int i=1; i<n/2; i++) {
	Swap(0,n-1-i);
	Swap(n-1,i);
    }

    if (n%2) {
	Swap(0,n/2);
    }

    assert((int)ans.size()==n-1);

    cout<<res<<"\n";
    
    for (int i=0; i<n; i++) {
	cout<<a[i]+1<<" ";
    }
    cout<<"\n";

    reverse(ans.begin(), ans.end());
    cout<<ans.size()<<"\n";
    for (auto p: ans) cout<<p.first+1<<" "<<p.second+1<<"\n";
}


int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);
    int t;
    cin>>t;
    while (t--) solve();    
    return 0;
}
