#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
#define setPrecision(n) cout << std::setprecision(n) << std::fixed;
#define rep(i,j,n) for(int i = j; i < n; i++)
#define ll long long
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define pli pair<long long,int>
#define vi vector<int>
#define vll vector<ll>
#define sz(x) ((int) (x).size())
#define all(c) (c).begin(),(c).end()
#define F first
#define S second
#define INF 2e9
#define LINF 2e18
const long long MOD = 1e9 + 7;
const double err = 1e-10;
const int N = 1e5 + 5;

//bruh what is pj  = i. Lame Lame Lame Lame

void solve() {
    int n; cin >> n;
    ll ans = 0;

    //maximum n-1 such operations
    // (n-1)^2 -> only one such operation
    // (n-2)^2 ->

    vi V(n);
    iota(all(V), 1);
    vector<pii> ways;
    vector<bool> swapped(n, 0);
    ways.pb({0, n-1});
    ans += 1ll * (n-1) * (n-1);
    swap(V[0], V[n-1]);
    swapped[0] = swapped[n-1] = 1;
    int l = 1, r = n-2;
    while(l <= r){
        if(!swapped[l]){
            swap(V[l], V[n-1]);
            ways.pb({n-1, l});
            ans += 1ll * (n-1 - l) * (n-1 - l);
            swapped[l] = 1;
        }
        if(!swapped[r]){
            swap(V[0], V[r]);
            ways.pb({0, r});
            ans += 1ll * (r) * (r);
            swapped[r] = 1;
        }
        l++, r--;
    }
    cout << ans << endl;
    rep(i,0,n) cout << V[i] << ' '; cout << endl;
    cout << sz(ways) << endl;
    reverse(all(ways));
    for(auto P : ways){
        cout << P.S + 1 << ' ' << P.F + 1 <<endl;
    }
}

int main() {
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    FASTER;
    int T = 1;
    cin >> T;
    while (T--) solve();
    return 0;
}
