#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <utility>
#include <queue>
#include <map>
#include <assert.h>
#include <stack>
#include <string>
#define int long long
using namespace std;
/*
author: Legendary Grandmaster mshiladityam
Wowwwww. Loved it. What a beutiful problem. 
And the proof comes naturally with the solution.
Gr8 problem.

It's like: the best you can do for each index, actually exists, by construction.
*/

int sq(int x)
{
	return x*x;
}


void solve()
{
	int n;
	cin>>n;
	vector<int> v(n+1);
	
	v[1]=(n/2)+1;
	for (int j=n/2+1; j<n; j++)
	{
		v[j]=j+1;
	}
	v[n]=n/2;
	for (int j=n/2; j>1; j--) v[j]=j-1;
	
	
	int ans=0;
	for (int j=2; j<=n; j++) 
	{
		ans+=max(sq(j-1), sq(n-j));
	}
	cout<<ans<<"\n";
	for (int j=1; j<=n; j++) cout<<v[j]<<" ";
	cout << "\n";
	
	cout<<n-1<<"\n";
	
	for (int j=n/2+1; j<n; j++)
	{
		cout<<j<<" "<<1<<"\n";
	}
	for (int j=n/2; j>1; j--)
	{
		cout<<j<<" "<<n<<"\n";
	}
	cout<<1<<" "<<n<<"\n";
	return;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
	int t;
	cin >> t;
	//t=1;
	while (t--)
	{
		solve();
	}
	return 0;
}