#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
int a[maxn];
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		for(int i = 1; i <= n; i++) {
			a[i] = i;
		}
		vector<pair<int,int> >vec;
		ll ans = 0;
		int cnt = n - 1;
		for(int i = n - 1;; i--) {
			swap(a[i + 1],a[1]);
			vec.push_back({i + 1,1});
			ans += 1ll * i * i;
			cnt--;
			if(cnt == 0) break;
			if(i == n - 1) continue;

			swap(a[n - i],a[n]);
			vec.push_back({n - i,n});
			ans += 1ll * i * i;
			cnt--;
			if(cnt == 0) break;
		}
		printf("%lld\n",ans);
		for(int i = 1; i <= n; i++) {
			printf("%d ",a[i]);
		}
		printf("\n");
		printf("%d\n",n - 1);
		for(int i = vec.size() - 1; i >= 0; i--) {
			printf("%d %d\n",vec[i].first,vec[i].second);
		}
	}
	return 0;
}
 		 		  	  	  				 				   		 	