#include <bits/stdc++.h>
 
using namespace std;
 
#define mod(a, b) ((((a)%(b))+(b))%(b))
#define pb push_back
#define sz(c) int((c).size())
 
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); 
	ll T, n; cin >> T;
 	
 	for(int t = 0; t < T; t++) {
 		cin >> n;

 		vector<ll> f(n-1);
 		vector<ll> s(n-1);

 		f[0] = 1;
 		s[0] = n;
 		int r = n-1;
 		int l = 2;
 		for(int i = 1; i < n-1; i++) {
 			if(i % 2) {
 				s[i] = 1;
 				f[i] = r;
 				r--;
 			}
 			else {
 				s[i] = n;
 				f[i] = l;
 				l++;
 			}
 		}

 		vector<int> perm(n);
 		for(int i = 0; i < n; i++) perm[i] = i+1;
 		ll sec = 0;
 		for(int i = 0; i < n-1; i++){
 			swap(perm[f[i] - 1], perm[s[i] - 1]);
 			sec += (f[i] - s[i]) * (f[i] - s[i]);
 		} 
 		cout << sec << endl;
 		for(int i = 0; i < n; i++) cout << perm[i] << " ";
 		cout << endl;

 		cout << (n-1) << endl;

 		for(int i = n-2; i >= 0; i--) {
 			cout << f[i] << " " << s[i] << endl;
 		}
 	}
 
	return 0;
}