#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

int main()
{
	int t; scanf("%d", &t);
	while (t--)
	{
		int n; scanf("%d", &n);
		
		vector<int> v;
		for (int i = 1; i <= n; i++)
			v.push_back(i);
		
		ll cost = 0;
		vector<PII> path;
		for (int i = n / 2 + 1; i <= n; i++)
		{
			swap(v[0], v[i - 1]);
			if (1 == v[i - 1]) path.push_back({1, i});
			else path.push_back({i, 1});
			cost += 1LL * (1 - i) * (1 - i);
		}
		for (int i = 2; i <= n / 2; i++)
		{
			swap(v[i - 1], v[n - 1]);
			if (i == v[n - 1]) path.push_back({i, n});
			else path.push_back({n, i});
			cost += 1LL * (i - n) * (i - n);
		}
		reverse(path.begin(), path.end());
		
		printf("%lld\n", cost);
		for (auto x: v) printf("%d ", x); printf("\n");
		printf("%d\n", n - 1);
		for (auto x: path) printf("%d %d\n", x.first, x.second);
	}
	
	return 0;
}