#include<bits/stdc++.h>
using namespace std;
int main() {
	int t;
	scanf("%d", &t);
	while(--t >= 0 ) {
		int n;
		scanf("%d", &n);


		vector<int> P;
		vector<pair<int, int>> op;
		


			
		long long ans = (n-1) * 1ll * (n-1);
		for (int i = 2; i <= n/2; i++) {
				ans += 2ll * (n-i) * (n-i);	
		}

		if (n & 1) ans += (n - (n/2 + 1)) * 1ll * (n - (n/2 + 1));


		P.push_back(n/2 + 1);

		for (int i=1; i<n/2; ++i) P.push_back(i);
		for (int i=n/2 + 2; i<=n; ++i) P.push_back(i);
		P.push_back(n/2);


		for (int i=1; i<=n/2; i++) {
			op.push_back({i, n});
		}
		for (int i=n-1; i>n/2; --i) op.push_back({i, 1});


		printf("%lld\n", ans);
		for (int i=0; i<n; i++) {
			printf("%d%c", P[i], " \n"[i==n-1]);		
		}

		printf("%d\n", n-1);
		for (int i=(int)(op.size())-1; i>=0; --i) printf("%d %d\n", op[i].first, op[i].second);
	}
	return 0;
}
