#include<bits/stdc++.h>
using namespace std;
int T,n,p[100010],ls;
long long ans;
int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n),ls=n,ans=0;
		for(int i=2;i<=(n>>1);++i)p[ls]=i,ans+=1ll*(n-i)*(n-i),ls=i;
		p[ls]=1,ls=1;
		for(int i=(n>>1)+1;i<=n;++i)p[ls]=i,ans+=1ll*(i-1)*(i-1),ls=i;
		printf("%lld\n",ans);
		for(int i=1;i<=n;++i)printf("%d%c",p[i]," \n"[i==n]);
		printf("%d\n",n-1);
		for(int i=2;i<=(n>>1);++i)printf("%d %d\n",i,n);
		for(int i=(n>>1)+1;i<=n;++i)printf("%d 1\n",i);
	}
	return 0;
}