#include<bits/stdc++.h> 
using namespace std;
typedef unsigned long long ll;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int tc;
    cin >> tc;
    while(tc--){
        int n;
        cin >> n;
        set<int> spec = {2,4,16,256,65536};
        vector<pair<int,int>> res;
        vector<int> arr(n+1);
        for(int i = 0;i <= n;i++)arr[i] = i;
        for(int i = n-1;i > 1;i--){
            if(spec.find(i) == spec.end()){
                res.push_back({n,i});
                arr[i] = arr[i]/n + arr[i]%n?1:0;
            }
        }
        int end = n;
        

        while(end <= *spec.rbegin())spec.erase(*spec.rbegin());
        int k = *spec.rbegin();
        res.push_back({k,end});
        int End = end;
        end = end/k + ((end%k)?1:0);
        arr[End] = arr[End]/(*spec.rbegin()) + arr[End]%(*spec.rbegin())?1:0;
        if(end != 1){
            res.push_back({*spec.rbegin(),End});
            arr[End] = arr[End]/(*spec.rbegin()) + arr[End]%(*spec.rbegin())?1:0;
            end = end/(*spec.rbegin()) + end%(*spec.rbegin())?1:0;
        }
        assert(end == 1);
        while(spec.size() > 1){
            end = *spec.rbegin();
            spec.erase(*spec.rbegin());
            arr[end] = arr[end]/(*spec.rbegin()) + arr[end]%(*spec.rbegin())?1:0;
            arr[end] = arr[end]/(*spec.rbegin()) + arr[end]%(*spec.rbegin())?1:0;
            res.push_back({*spec.rbegin(),end});
            res.push_back({*spec.rbegin(),end});
        }
        cout << res.size() << "\n";
        for(auto& x:res)cout << x.second << " " << x.first << "\n";
    }           
}