#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
#include <sstream>
#include <list>
#include <map>
#include <set>
#include <limits>
#include <random>
#include <functional>
#include <unordered_set>
#include <unordered_map>

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int ui;

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>

using namespace std;

const int N = 1000010;
const int MOD = 1000000007;
const int INF = 1009000999;
const ll LINF = (1ll << 60) + 1337;
const ld EPS = 0.0000000001;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef _DEBUG
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        ll cost = 0;
        vector<pii> ans;
        vector<int> p(n + 1);
        for (int i = 0; i <= n; i++) p[i] = i;
        for (int i = 1; i <= n; i++) {
            if (i == p[i]) {
                if (i - 1 > n - i) {
                    ans.push_back({ i,1 });
                    swap(p[i], p[1]);
                    cost += (i - 1) * 1ll * (i - 1);
                }
                else {
                    ans.push_back({ i, n });
                    swap(p[n], p[i]);
                    cost += (n - i) * 1ll * (n - i);
                }
            }
        }
        cout << cost << "\n";
        for (int i = 1; i <= n; i++) {
            cout << p[i] << " ";
        }
        cout << "\n" << ans.size() << "\n";
        reverse(ans.begin(), ans.end());
        for (auto& i : ans) cout << i.first << " " << i.second << "\n";
    }
}

/*
    Important stuff
    - int overflow
    - array bounds
    - special cases (n=1, max n) maybe adhoc problem
    - doubles are read for a long time (4 * 10 ^ 5 danger GNU vs MS ICPC 2020 1/4)
    - don't get stuck on one approach
    - don't get stuck on one problem
    - recheck submissions if near the end of the tournament and there is nothing to do
    - do something instead of nothing and stay organized
*/