Numbers on Whiteboard (Codeforces 1430C)
Tags: Greedy, data structures

Numbers $$$1, 2, 3, \dots n$$$ (each integer from $$$1$$$ to $$$n$$$ once) are written on a board. In one operation you can erase any two numbers $$$a$$$ and $$$b$$$ from the board and write one integer $$$\frac{a + b}{2}$$$ rounded up instead.You should perform the given operation $$$n - 1$$$ times and make the resulting number that will be left on the board as small as possible. For example, if $$$n = 4$$$, the following course of action is optimal:  choose $$$a = 4$$$ and $$$b = 2$$$, so the new number is $$$3$$$, and the whiteboard contains $$$[1, 3, 3]$$$;  choose $$$a = 3$$$ and $$$b = 3$$$, so the new number is $$$3$$$, and the whiteboard contains $$$[1, 3]$$$;  choose $$$a = 1$$$ and $$$b = 3$$$, so the new number is $$$2$$$, and the whiteboard contains $$$[2]$$$. It's easy to see that after $$$n - 1$$$ operations, there will be left only one number. Your goal is to minimize it.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.The only line of each test case contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of integers written on the board initially.It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output: For each test case, in the first line, print the minimum possible number left on the board after $$$n - 1$$$ operations. Each of the next $$$n - 1$$$ lines should contain two integers — numbers $$$a$$$ and $$$b$$$ chosen and erased in each operation.


Sample Inputs:
1
4

Sample Outputs:
2
2 4
3 3
3 1

Solution (Python):
for _ in range(int(input(''))):
    n = int(input(''))
    print('2')
    print(f'{n} {n - 1}')
    i = n
    while i > 2:
        print(f'{i} {i - 2}')
        i -= 1

Solution (Python):
for j in range(int(input())):
    n = int(input())
    print(2)
    print(f'{n} {n - 1}')
    for i in range(n - 2):
        print(f'{n - 2} {n}')
        n -= 1

Solution (Python):
import os
import sys
from io import BytesIO, IOBase
if os.path.exists('/home/sunny/Desktop/VS Codes/CF/input.txt'):
    sys.stdin = open('/home/sunny/Desktop/VS Codes/CF/input.txt', 'r')
    sys.stdout = open('/home/sunny/Desktop/VS Codes/CF/output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
R = lambda : list(map(int, input().split()))
odd = lambda x: bool(x & 1)
even = lambda x: not bool(x & 1)
summation = lambda x: x * (x + 1) // 2
from collections import defaultdict, deque
from bisect import bisect_left, bisect_right
import math

def solve():
    n = int(input())
    l = [i for i in range(1, n + 1)]
    print(2)
    for i in range(len(l) - 1):
        print(l[-1], l[-2])
        x = l.pop()
        l[-1] = math.ceil((l[-1] + x) / 2)
for _ in range(int(input())):
    solve()