/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

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

#define forn(i,n) for(int i=0;i<(int)(n);i++)
#define si(c) ((int)(c).size())
#define forsn(i,s,n) for(int i = (int)(s); i<((int)n); i++)
#define dforsn(i,s,n) for(int i = (int)(n)-1; i>=((int)s); i--)
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << (a) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using ll = long long int;
using vi = vector<int>;
using pii = pair<int,int>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template<class T>
istream& operator>>(istream &in, vector<T> &a) {
    for (auto &x : a) in >> x;
    return in;
}

// use any_of, all_of, none_of, count_if, find, accumulate, binary_search, max_element, min_element, partial_sum, for_each
// in general params are: (iter from, iter to, lambda function)

struct EWhatIsIt {

    ll sqr(ll x) {
        return x*x;
    }

    void go(int pos, ll &cost, vector<pii> &ans, vi &p) {
        auto j = pos, i = p[j];
        cost += sqr(i-j);
        ans.eb(i,j);
        swap(p[i], p[j]);
    }

    void solve(std::istream& cin, std::ostream& cout) {
        int T; cin >> T;
        while (T--) {
            int n; cin >> n;
            vi left(1,0), right(1,n-1);
            int mid = n/2;
            for (int i = n-2; i >= mid; i--) left.pb(i);
            for (int i = 1; i < mid; i++) right.pb(i);
            auto cycle = left;
            cycle.insert(cycle.end(), all(right));
            vi p(n);
            forn(i,n) p[cycle[i]] = cycle[(i+1)%n];

            ll cost = 0;
            vector<pii> ans;
            auto np = p;
            forn(_, si(left)-1) go(0, cost, ans, np);
            forn(_, si(right)) go(n-1, cost, ans, np);
            cout << cost << '\n';
            for (auto x : p) cout << x+1 << ' '; cout << '\n';
            cout << si(ans) << '\n';
            for (auto [i,j] : ans) cout << i+1 << ' ' << j+1 << '\n';
        }
    }
};


int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    EWhatIsIt solver;
    std::istream& in(std::cin);
    std::ostream& out(std::cout);
    solver.solve(in, out);
    return 0;
}
