#include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
// #define endl "\n"
const int MAXN = 2E5 + 7;
// const int MOD = ;
// const int INF = ;
// const double eps = ;
// const double PI = acos(-1);
// const int DIRX[] = {};
// const int DIRY[] = {};

int T;
int n;
int ans;
int a[MAXN];
vector<pair<int, int> > sol;

void init(int n)
{
    sol.clear();
    for (int i = 1; i <= n; ++i)
        a[i] = i;
}

void solve(int n)
{
    ans = 0;
    int dlt = 1;
    int cnt = 0;
    while ((++cnt) <= n - 1)
    {
        if (cnt == 1)
        {
            swap(a[1], a[n]);
            sol.push_back({1, n});
            ans += (n - 1) * (n - 1);
        }
        else
        {
            swap(a[(++dlt)], a[n]);
            sol.push_back({dlt, n});
            ans += (n - dlt) * (n - dlt);
            if ((++cnt) <= n - 1)
            {
                swap(a[1], a[n - dlt + 1]);
                sol.push_back({n - dlt + 1, 1});
                ans += (n - dlt) * (n - dlt);
            }
        }
    }
    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 << sol[i].first << " " << sol[i].second << endl;
    }
}

int32_t main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> T;
    while (T--)
    {
        cin >> n;
        init(n);
        solve(n);
    }
    return 0;
}