#include <bits/stdc++.h>
using namespace std;
 
#define fo(a,b) for(int a=0;a<b;++a)
#define pb push_back
#define printList(v) for(auto _x:(v)) cout<<_x<<" ";cout<<endl;

////////////// DEBUG UTILS //////////////////
#define debug(...) __print(#__VA_ARGS__, __VA_ARGS__)
template<class K,class V> ostream& operator<<(ostream& os,const pair<K,V>& p) {os<<"("<<p.first<<", "<<p.second<<")"; return os;}
template<class T> ostream& __dPrint(ostream& os, const T& v) {os<<v; return os;}
template<> ostream& __dPrint(ostream& os, const string& v) {os<<'"'<<v<<'"'; return os;}
template<class T> ostream& operator <<(ostream& os,const vector<T>& v) {
  os<<v.size()<<" vals: [ ";
	fo(i,v.size()) { if(i)os<<", "; __dPrint(os, v[i]); } os<<" ]";
	return os;
}
template <typename Arg> void __print(const char* name, Arg&& arg){ cerr << name << " : " << arg << endl; }
template <typename Arg, typename... Args>
void __print(const char* names, Arg&& arg, Args&&... args){
	const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg<< endl;
  __print(comma+1, args...);
}
////////////// END OF DEBUG UTILS //////////////////
 
typedef long long int lint;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<lint> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int,int> pii;
 
int ri() {int x;if(scanf(" %d",&x) == EOF) return -1;return x;}
lint rl() {lint x;if(cin>> x) return x; return -1;}
string rs() {string s;if(cin>>s) return s; return "";}
/////////////////////////// END TEMPLATE /////////////////
 
struct SingleCase {
  int n;

  bool solveCase() {
    n = ri(); if(n < 0) return false;
    vector<pii> kills;
    
    lint score = 0;
    for(int i=1;i<n;i++) {
      int a = i, b = n-1-i;
      int t = max(a,b);      
      if(a > b) {
        kills.pb({0, i});
      } else {
        kills.pb({n-1, i});
      }
      score += (lint)t*t;
    }

    VI ret(n, -1);
    VI head(n); fo(i,n) head[i] = i;
    VI destroyed(n, 0);
    for(const auto& [a, b] : kills) {
      ret[head[a]] = b;
      head[a] = head[b];
      destroyed[b] = 1;
    }
    fo(i,n) if(!destroyed[i]) {
      ret[head[i]] = i;
      break;
    }

    cout << score << endl;

    fo(i,n) cout << ret[i] + 1 <<" "; cout << endl;
    cout << kills.size() << endl;
    for(const auto& [a, b]: kills) {
      cout << b+1 << " " << a + 1 << endl;
    }

    return true;
  }
};
 
int main() {
  ri();
  while(SingleCase().solveCase()) {};
  return 0;
}
