#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int t;
    cin >> t;

    for(int nr = 0; nr < t; nr++){
        int n;
        cin >> n;

        if(n == 2) {
            cout << 1 << endl;
            cout << 2 << " " << 1 << endl;
            cout << 1 << endl;
            cout << 1 << " " << 2 << endl;
        } else if(n == 3){
            cout << 5 << endl;
            cout << 2 << " " << 3 << " " << 1 << endl;
            cout << 2 << endl;
            cout << 1 << " " << 3 << endl;
            cout << 2 << " " << 3 << endl;
        } else {
            int time = (n - 1) * (n - 1);
            for(int i = 0; i < n / 2 - 1; i++) time += 2 * (n - 2 - i) * (n - 2 - i);
            if(n % 2 == 1) time += (n / 2) * (n / 2);
            cout << time << endl;

            vector<int> p(n, 0);
            for(int i = 3; i <= n; i++) p[i - 2] = i;
            p[0] = n / 2 + 1;
            p[n / 2 - 1] = 1;
            p.back() = 2;

            for(int x : p) cout << x << " ";
            cout << endl;

            cout << n - 1 << endl;
            for(int i = 2; i <= n / 2; i++){
                cout << i << " " << n << endl;
            }
            for(int i = n / 2 + 1; i <= n - 1; i++){
                cout << i << " " << 1 << endl;
            }
            cout << 1 << " " << n << endl;
        }
    }
}
