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

typedef long long ll;
typedef long double ld;

#define FOR(i,n) for (int i=0;i<(n);++i)
#define FORT(i,a,b) for (int i=(a);i<=(b);++i)
#define FORTD(i,a,b) for (int i=(a);i>=(b);--i)
#define FORD(i,n) for (int i=(n)-1;i>=0;--i)
#define trav(to,v) for (auto to : v)
#define fr first
#define sc second
#define mpr make_pair
#define pir pair<int,int>
#define ad push_back
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
#define all(v) v.begin(),v.end()
#define endl '\n'

const int N=1e6+6;
const ll MOD=1e9+7;

int p[N];
vector<pir> v;

void solve(int n){
	v.clear();
	v.ad({1,n});
	FORT(i,2,n-1){
		if (i-1>n-i)v.ad({i,1});
		else v.ad({i,n});
	}

}

int main(){
	fastio;
	int ttt;
	cin>>ttt;
	while(ttt--){
		int n;
		cin>>n;
		solve(n);
		ll ans=0;
		FORT(i,1,n)p[i]=i;
		trav(to,v){
			ans+=(ll)(abs(to.fr-to.sc))*(ll)(abs(to.fr-to.sc));
			swap(p[to.fr],p[to.sc]);
		}
		cout<<ans<<endl;
		FORT(i,1,n)cout<<p[i]<<" ";
		cout<<endl<<v.size()<<endl;
		reverse(all(v));
		trav(to,v)cout<<to.fr<<" "<<to.sc<<endl;
	}
	return 0;
}