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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t; cin >> t;
    while (t--) {
        int n; cin >> n;
        long long res = 0;
        vector<int> a(n);
        vector<pair<int, int>> op;
        for (int i = 0; i < n; i++) {
            a[i] = i;
        }
        op.push_back({ 0,n - 1 });
        swap(a[0], a[n - 1]);
        res += 1LL * (n - 1) * (n - 1);
        int l = 1, r = n - 2;
        for (int i = 0; l < n && r >= 0; i++) {
            if (a[l] != l && a[r] != r) {
                break;
            }
            if ((i & 1) && a[r] == r) {
                swap(a[r], a[0]);
                res += 1LL * (r) * (r);
                op.push_back({ r--,0 });
            } else if (a[l] == l) {
                swap(a[l], a[n - 1]);
                res += 1LL * (n - l - 1) * (n - l - 1);
                op.push_back({ l++,n - 1 });
            }
        }
        cout << res << '\n';
        for (int i = 0; i < n; i++) {
            cout << a[i] + 1 << " \n"[i == n - 1];
        }
        cout << op.size() << '\n';
        reverse(op.begin(), op.end());
        for (auto i : op) {
            cout << i.first + 1 << ' ' << i.second + 1 << '\n';
        }
    }
	return 0;
}