#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define sz(x) (int)(x).size()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
template<class T> bool cmn(T& a, T b) { return b < a ? a = b, true : false; }
template<class T> bool cmx(T& a, T b) { return b > a ? a = b, true : false; }
void solve() {
    int n;
    cin >> n;
    int mid = n / 2 - 1;
    if (n == 2) {
        cout << "1\n2 1\n1\n1 2\n";
        return;
    }
    if (n == 3) {
        cout << "5\n2 3 1\n2\n1 3\n2 3\n";
        return;
    }
    vector<int> p(n);
    for (int i = 1; i < mid; i++) p[i] = i + 2;
    p[mid] = 1;
    p[n - 1] = 2;
    p[0] = mid + 2;
    for (int i = mid + 1; i < n - 1; i++) p[i] = i + 2;
    vector<pii> ans;
    for (int i = 1; i <= mid; i++) ans.pb(mp(i + 1, n));
    for (int i = mid + 1; i < n; i++) ans.pb(mp(i + 1, 1));
    ll S = 0;
    for (auto [x, y] : ans) S += 1ll * (x - y) * (x - y);
    cout << S << '\n';
    for (int i : p) cout << i << ' ';
    cout << '\n';
    cout << sz(ans) << '\n';
    for (auto [x, y] : ans) cout << x << ' ' << y << '\n';
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}

