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


void solve(){
    int n; scanf("%d", &n);
    if (n == 2){
        printf("1\n2 1\n1\n2 1\n");
    }
    else if (n == 3){
        printf("5\n2 3 1\n2\n1 3\n2 3\n");
    }
    else {
        long long t = 0;
        vector<int> ans;
        vector<pair<int, int>> vc;
        ans.push_back(n / 2 + 1);
        for (int i = 3; i <= n / 2; ++i) ans.push_back(i);
        ans.push_back(1);
        for (int i = n / 2 + 2; i <= n; ++i) ans.push_back(i);
        ans.push_back(2);
        for (int i = 2; i <= n / 2; ++i){
            vc.push_back({i, n});
            t += (long long)(n - i) * (n - i);
        }
        for (int i = n / 2 + 1; i <= n; ++i){
            vc.push_back({i, 1});
            t += (long long)(i - 1) * (i - 1);
        }
        printf("%lld\n", t);
        for (int it : ans) printf("%d ", it);
        printf("\n");
        printf("%d\n", n - 1);
        for (auto it : vc){
            printf("%d %d\n", it.first, it.second);
        }
    }
}

int main(){
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}