#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#define ll long long
#define chushi(a, b) memset(a, b, sizeof(a))
#define endl "\n"
const double eps = 1e-8;
const ll INF=0x3f3f3f3f;
const int mod = 20000311;
const int maxn = 1e6 + 5;
const int N=1005;
using namespace std;

int v[maxn];
vector<pair<ll, ll> > res;

int main(){
	
	int ncase;
	cin >> ncase;
	while(ncase--){
		ll n;
		cin >> n;
		ll sum = 0;
		
		ll x = n/2;
		v[1] = n; v[n] = 1;
		res.push_back({n, 1});
		sum += (n-1) * (n-1);
		for(ll i = 1; i < n/2; i++){
			ll a = 1 + i;
			ll b = n - i;
			res.push_back({a, n});
			res.push_back({b, 1});
			v[a] = a; swap(v[a], v[n]);
			v[b] = b; swap(v[b], v[1]);
			sum += (n-a) * (n-a);
			sum += (n-a) * (n-a);
		}
		
		if(n & 1){
			v[x+1] = x+1;
			swap(v[1], v[x+1]);
			res.push_back({x+1, 1});
			sum += x * x;
		}
		
		cout << sum << endl;
		for(int i = 1; i <= n; i++) cout << v[i] << " ";
		cout << endl;
		n = res.size();
		cout << n << endl;
		for(int i = n-1; i >= 0; i--) cout << res[i].first << " " << res[i].second << endl; 
		res.clear();
	}
	

	return 0;
}
