/*
Author: Wa-Automaton
Time: 202101203 15:20:00 UTC+8       

Website: codeforces 
Contest: Codeforces Round #696 (Div. 2)
Problem: E. What Is It?

Result: 
Solutions: 
    
*/

#include<bits/stdc++.h>
using namespace std;

#define lson (root << 1)
#define rson (root << 1 | 1)

typedef long long LL;
typedef pair<LL, LL> PLL;
typedef vector<LL> VL;
typedef vector<PLL> VLL;
typedef vector<VL> VVL;

const LL MOD = 1e9 + 7;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
const LL Inf = 0x3f3f3f3f;
const LL N = 2e5 + 7;

LL n, a[N], ans;
VLL g;
// 
void solve() {
    cin >> n;
    for (int i = 1; i <= n; ++i) a[i] = i;
    ans = 0; g.clear();
    for (LL l, r, i = 1; i < n; ++i) {
        l = i - 1;
        r = n - i;
        if (l >= r) {
            swap(a[i], a[1]);
            g.push_back({i, 1});
            ans += l * l;
        } else {
            swap(a[i], a[n]);
            g.push_back({i, n});
            ans += r * r;
        }
    }
    cout << ans << endl;
    for (int i = 1; i <= n; ++i) cout << a[i] << " "; cout << endl;
    cout << n - 1 << endl;
    for (int i = n - 2; i >= 0; --i) {
        cout << g[i].first << " " << g[i].second << endl;
    }
}

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