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

    #define pb(x) push_back(x)
    #define FOR(i,a,b) for(int i=a;i<b;i++)
    typedef long long ll;

    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);

        int t;
        cin>>t;
        while(t--){
            int n;
            cin>>n;
            vector<int> v1;
            stack<pair<int,int> > s;
            FOR(i,0,n) v1.pb(i+1);
            int left=0;
            int right=n-1;
            ll tc=0;
            ll nc=0;
            swap(v1[left],v1[right]);
            tc+=pow(right-left,2);
            nc+=1;
            s.push(make_pair(left+1,right+1));
            while(left<right-1){
                right-=1;
                swap(v1[0],v1[right]);
                tc+=pow(right,2);
                nc++;
                s.push(make_pair(right+1,1));
                if(left<right-1){
                    left+=1;
                    swap(v1[left],v1[n-1]);
                    tc+=pow(n-1-left,2);
                    nc++;
                    s.push(make_pair(left+1,n));
                }
            }
            cout<<tc<<endl;
            FOR(i,0,n) cout<<v1[i]<<" ";
            cout<<endl;
            cout<<nc<<endl;
            while(!s.empty()){
                pair<int,int> p=s.top();
                s.pop();
                cout<<p.first<<" "<<p.second<<endl;
            }

        }

        return 0;
    }