#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pp pair<int,int>
#define endl '\n'
const int N=1e5+10;
int p[N];
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		ll ans=(ll)(n-1)*(n-1);
		for(int i=1;i<n-1;i++){
			int m=max(i,n-1-i);
			ans=ans+(ll)m*m;
		}
		cout<<ans<<endl;
		int id=n/2;
		if(n==2){
			cout<<"2 1\n1\n2 1\n";
			continue;
		}
		if(n==3){
			cout<<"2 3 1\n2\n1 3\n2 3\n";
			continue;
		}
		p[0]=n-2;
		p[n-1]=1;
		for(int i=n-2;i>id;i--){
			p[i]=i-1;
		}
		for(int i=1;i<id-1;i++){
			p[i]=i+1;
		}
		p[id]=n-1;
		p[id-1]=0;
		for(int i=0;i<n;i++){
			cout<<p[i]+1<<" ";
		}
		cout<<endl;
		cout<<n-1<<endl;
		for(int i=n-2;i>=id;i--){
			cout<<i+1<<" "<<1<<endl;
		}
		for(int i=1;i<id;i++){
			cout<<i+1<<" "<<n<<endl;
		}
		cout<<1<<" "<<n<<endl;
	}
}