#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
int n,a[N],T;
void gao(){
	ll sum=0;
	scanf("%d",&n);
	for (int i=1;i<=n;i++)a[i]=i;
	for (int i=1;i<n;i++){
		int t=n-i>i-1?n:1;
		swap(a[i],a[t]);
		sum+=1ll*(t-i)*(t-i);
	}
	printf("%lld\n",sum);
	for (int i=1;i<=n;i++)printf("%d ",a[i]);
	puts("");
	printf("%d\n",n-1);
	for (int i=n-1;i>=1;i--){
		int t=n-i>i-1?n:1;
		printf("%d %d\n",i,t);
	}
}
int main(){
	scanf("%d",&T);
	while (T--)gao();
	return 0;
}  
