You have an array $a_1, a_2, \dots, a_n$ where $a_i = i$.

In one step, you can choose two indices $x$ and $y$ ($x \neq y$) and set $a_x = \left\lceil \frac{a_x}{a_y} \right\rceil$ (ceiling function).

Your goal is to make array $a$ consist of $n - 1$ ones and $1$ two in no more than $n + 5$ steps. Note that you don't have to minimize the number of steps.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.

The first and only line of each test case contains the single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the length of array $a$.

It's guaranteed that the sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$.


-----Output-----

For each test case, print the sequence of operations that will make $a$ as $n - 1$ ones and $1$ two in the following format: firstly, print one integer $m$ ($m \le n + 5$) — the number of operations; next print $m$ pairs of integers $x$ and $y$ ($1 \le x, y \le n$; $x \neq y$) ($x$ may be greater or less than $y$) — the indices of the corresponding operation.

It can be proven that for the given constraints it's always possible to find a correct sequence of operations.


-----Examples-----

Input
2
3
4
Output
2
3 2
3 2
3
3 4
4 2
4 2


-----Note-----

In the first test case, you have array $a = [1, 2, 3]$. For example, you can do the following:

choose $3$, $2$: $a_3 = \left\lceil \frac{a_3}{a_2} \right\rceil = 2$ and array $a = [1, 2, 2]$;

choose $3$, $2$: $a_3 = \left\lceil \frac{2}{2} \right\rceil = 1$ and array $a = [1, 2, 1]$.

You've got array with $2$ ones and $1$ two in $2$ steps.

In the second test case, $a = [1, 2, 3, 4]$. For example, you can do the following:

choose $3$, $4$: $a_3 = \left\lceil \frac{3}{4} \right\rceil = 1$ and array $a = [1, 2, 1, 4]$;

choose $4$, $2$: $a_4 = \left\lceil \frac{4}{2} \right\rceil = 2$ and array $a = [1, 2, 1, 2]$;

choose $4$, $2$: $a_4 = \left\lceil \frac{2}{2} \right\rceil = 1$ and array $a = [1, 2, 1, 1]$.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Number theory

Complete search

Mathematics

Constructive algorithms


Solutions:
from math import ceil
for ad in range(int(input())):
	n = int(input())
	print(n + 5)
	m = n + 5
	c = 0
	while True:
		k = ceil(n ** 0.5)
		if n == 2:
			break
		for i in range(n - 1, 0, -1):
			if i == k:
				print(n, i)
				print(n, i)
				n = k
				c += 2
				break
			else:
				print(i, n)
				c += 1
	for i in range(m - c):
		print(2, 1)


def readLine():
	return [int(s) for s in input().split(' ')]
zbior = {2, 4, 16, 256, 65536}

def solve():
	n = int(input())
	ans = []
	maxi = 2
	for i in range(2, n + 1):
		y = {i}
		if y.issubset(zbior):
			continue
		elif i < n:
			ans.append([i, n])
		else:
			while maxi * maxi <= n:
				maxi = maxi * maxi
			ans.append([n, maxi])
			ans.append([n, maxi])
	while maxi * maxi <= n:
		maxi = maxi * maxi
	specjalne = []
	cur = 2
	while cur <= maxi:
		specjalne.append(cur)
		cur = cur * cur
	specjalne.reverse()
	p = 0
	for cur in specjalne:
		if cur == 2:
			break
		pop = specjalne[p + 1]
		akt = specjalne[p]
		ans.append([akt, pop])
		ans.append([akt, pop])
		p = p + 1
	print(len(ans))
	for x in ans:
		print(x[0], x[1])
tt = int(input())
for x in range(tt):
	solve()


from math import sqrt
from math import ceil
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [0] * (n + 1)
	arr[-1] = n
	x = []
	y = []
	i = n
	while i > 2:
		j = i
		if i == n:
			j = n - 1
		while j > sqrt(arr[-1]) // 1 and j > 2:
			x.append(j)
			y.append(n)
			j -= 1
		i = j
		x.append(n)
		y.append(i)
		arr[-1] = ceil(arr[-1] / i)
	while arr[-1] != 1:
		arr[-1] = ceil(arr[-1] / 2)
		x.append(n)
		y.append(2)
	print(len(x))
	for i in range(len(x)):
		print(x[i], y[i])


from math import ceil, sqrt
for i in range(int(input())):
	n = int(input())
	spec = []
	m = n
	while m > 2:
		spec.append(m)
		m = ceil(sqrt(m))
	if len(spec) > 0 and spec[-1] != 2:
		spec.append(2)
	ans = []
	used = [0] * n
	for e in spec:
		used[e - 1] = 1
	for i in range(1, n - 1):
		if not used[i]:
			ans.append((i + 1, n))
	for i in range(1, len(spec)):
		ans.append((spec[i - 1], spec[i]))
		ans.append((spec[i - 1], spec[i]))
	print(len(ans))
	for e in ans:
		print(*e)


def dev(n, m):
	ret = n // m
	if n % m != 0:
		ret += 1
	return ret
T = int(input())
lis = [int(input()) for i in range(T)]
for t in range(T):
	n = lis[t]
	count = 1
	tmp = 2
	for i in range(10):
		tmp = tmp * tmp
		if tmp >= n:
			break
		count += 1
	dic = {2: 0, 4: 0, 16: 0, 256: 0, 65536: 0}
	de = [1, 2, 4, 16, 256, 65536]
	print(n + count - 2)
	for i in range(3, n):
		if not i in dic:
			print(i, n)
	print(n, de[count])
	print(n, de[count])
	for i in range(count, 1, -1):
		print(de[i], de[i - 1])
		print(de[i], de[i - 1])


import math
t = int(input())
for _ in range(t):
	n = int(input())
	k = n
	s = 0
	while k > 4:
		q = math.ceil(math.sqrt(k))
		for i in range(q + 1, k):
			s += 1
		s += 2
		k = q
	if k == 3:
		s += 2
	if k == 4:
		s += 3
	print(s)
	while n > 4:
		q = math.ceil(math.sqrt(n))
		for i in range(q + 1, n):
			print(i, n)
		print(n, q)
		print(n, q)
		n = q
	if n == 3:
		print(3, 2)
		print(3, 2)
	if n == 4:
		print(3, 4)
		print(4, 2)
		print(4, 2)


import math
sqrt = math.sqrt
ceil = math.ceil
iterations = input()
for e in range(int(iterations)):
	n = int(input())
	ans = []
	while n > 2:
		cap = ceil(sqrt(n))
		for i in range(n - 1, cap, -1):
			ans.append([i, n])
		ans.append([n, cap])
		ans.append([n, cap])
		n = cap
	print(len(ans))
	for v in ans:
		print(*v)


t = int(input())
lg = [0] * (2 * 10 ** 5 + 5)
for i in range(2, 2 * 10 ** 5 + 1):
	lg[i] = lg[i // 2] + 1
for _ in range(t):
	n = int(input())
	if n == 3:
		print(2)
		print(3, 2)
		print(3, 2)
	else:
		max_lg = lg[n]
		keep = []
		cand = set(range(2, n + 1))
		while max_lg > 0:
			keep.append(1 << max_lg)
			cand.remove(1 << max_lg)
			max_lg //= 2
		op = []
		keep.sort(reverse=True)
		if 1 << lg[n] != n:
			cand.remove(n)
			cand.add(2)
		for elm in cand:
			op.append((elm, n))
		if 1 << lg[n] != n:
			op.append((n, 1 << lg[n]))
		for i in range(1, len(keep)):
			cnt = lg[keep[i - 1]] // lg[keep[i]]
			if lg[keep[i - 1]] % lg[keep[i]] != 0:
				cnt += 1
			if i == len(keep) - 1 and 1 << lg[n] != n:
				for _ in range(cnt):
					op.append((keep[i - 1], n))
				continue
			for _ in range(cnt):
				op.append((keep[i - 1], keep[i]))
		print(len(op))
		for p in op:
			print(*p)


import math
for i in range(int(input())):
	num = int(input())
	list_key = [2, 4, 16, 256, 65536]
	list_i = [i for i in list_key if i < num]
	list_i.append(num)
	print(num - 2 + len(list_i) - 1)
	for i in range(3, num):
		if i not in list_i:
			print(i, num)
	list_i.reverse()
	for i in range(len(list_i) - 1):
		print(list_i[i], list_i[i + 1])
		print(list_i[i], list_i[i + 1])


from math import sqrt

def check(n, ans):
	if n == 2:
		return
	y = max(1, int(sqrt(n)) - 1)
	while y < (n + y - 1) // y:
		y += 1
	for x in range(y + 1, n):
		ans.append([x, n])
	ans.append([n, y])
	ans.append([n, y])
	check(y, ans)
t = int(input())
for z in range(t):
	n = int(input())
	ans = []
	check(n, ans)
	print(len(ans))
	for i in ans:
		print(*i)


import math
t = int(input())
for i in range(t):
	s = int(input())
	n = math.ceil(math.log(s))
	i = n
	cnt = 0
	arr = []
	while n >= 2:
		for j in range(n + 1, s):
			arr.append([j, s])
		cnt += s - n - 1
		x = s
		while s != 1:
			arr.append([x, n])
			s = math.ceil(s / n)
			cnt += 1
		s = n
		n = math.ceil(math.log(s))
	print(cnt)
	for i in range(cnt):
		print(f'{arr[i][0]} {arr[i][1]}')


from sys import stdin, stdout
T = int(stdin.readline().strip())
for caso in range(T):
	n = int(stdin.readline().strip())
	ans = []
	if n <= 15:
		for i in range(3, n):
			ans.append([i, i + 1])
		x = n
		while x != 1:
			ans.append([n, 2])
			x = (x + 1) // 2
	else:
		for i in range(16, n):
			ans.append([i, i + 1])
		x = n
		while x != 1:
			ans.append([n, 15])
			c = 0
			if x % 15 != 0:
				c = 1
			x = x // 15 + c
		for i in range(3, 15):
			ans.append([i, i + 1])
		x = 15
		while x != 1:
			ans.append([15, 2])
			x = (x + 1) // 2
	stdout.write('%d\n' % len(ans))
	for i in ans:
		stdout.write('%d %d\n' % (i[0], i[1]))


ansl = []
for _ in range(int(input())):
	al = []
	n = int(input())
	v = n
	if n <= 16:
		for i in range(3, n):
			al.append((i, n))
		while v > 1:
			al.append((n, 2))
			v = (v - 1) // 2 + 1
		ansl.append(al)
		continue
	for i in range(3, n):
		if i != 16:
			al.append((i, n))
	v = n
	while v > 1:
		al.append((n, 16))
		v = (v - 1) // 16 + 1
	for j in range(4):
		al.append((16, 2))
	ansl.append(al)
for row in ansl:
	print(len(row))
	for (a, b) in row:
		print(a, b)


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(int(inp())):
	n = int(inp())
	arr = [2, 2 ** 2, 2 ** 4, 2 ** 8, 2 ** 16]
	mx = n
	ans = []
	for i in range(n - 1, 1, -1):
		if i in arr:
			k = mx
			while mx != 1:
				mx = ceil(mx, i)
				ans.append((k, i))
			mx = i
		else:
			ans.append((i, mx))
	print(len(ans))
	for i in ans:
		print(*i)


import sys
for _ in range(int(sys.stdin.readline().strip())):
	n = int(sys.stdin.readline().strip())
	s = 0
	m = n
	o = []
	n -= 1
	while n > 1:
		while (n - 1) ** 2 > m:
			o.append(str(n) + ' ' + str(m) + '\n')
			s += 1
			n -= 1
		s += 2
		o.append(str(m) + ' ' + str(n) + '\n')
		o.append(str(m) + ' ' + str(n) + '\n')
		m = n
		n -= 1
	print(s)
	for i in o:
		sys.stdout.write(i)


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, 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')

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		res = []
		a = 2
		for j in range(2, math.ceil(math.sqrt(n)) + 1):
			cnt = 0
			x = n
			v = j
			(mn, mx) = (j, n)
			tmp = []
			while x > 1:
				if x >= v:
					x = math.ceil(x / v)
					tmp.append([mx, mn])
				else:
					v = math.ceil(v / x)
					tmp.append([mn, mx])
				cnt += 1
			if cnt <= 8 and v == 2:
				res = tmp
				a = j
				break
		ans = []
		for j in range(2, n):
			if j != a and j != n:
				ans.append([j, j + 1])
		for r in res:
			ans.append(r)
		print(len(ans))
		for a in ans:
			print(a[0], a[1])
main()


import math

def solution():
	for tc in range(int(input())):
		n = int(input())
		ls = [i for i in range(1, n + 1)]
		ans = []
		for i in range(n - 2, 0, -1):
			while math.ceil(ls[n - 1] / ls[i]) >= ls[i]:
				ans.append([n, i + 1])
				ls[n - 1] = math.ceil(ls[n - 1] / ls[i])
			ans.append([i + 1, n])
			ls[i] = math.ceil(ls[i] / ls[n - 1])
		print(len(ans))
		for i in range(len(ans)):
			print(*ans[i])
solution()


from math import ceil, log2, sqrt
t = int(input())
for testCaseCounter in range(t):
	n = int(input())
	origN = n
	extraTwos = 0
	extraTwos += 1
	endpoints = list()
	index = n
	while index >= 3:
		endpoints.append(index)
		index = int(ceil(sqrt(index)))
	endpoints.append(2)
	endpoints.reverse()
	res = list()
	operations = 0
	for i in range(1, len(endpoints)):
		for j in range(endpoints[i - 1] + 1, endpoints[i]):
			res.append(str(j) + ' ' + str(endpoints[i]))
			operations += 1
	for i in range(len(endpoints) - 1, 0, -1):
		res.append(str(endpoints[i]) + ' ' + str(endpoints[i - 1]))
		res.append(str(endpoints[i]) + ' ' + str(endpoints[i - 1]))
		endpoints[i] = int(ceil(endpoints[i] / endpoints[i - 1]))
		endpoints[i] = int(ceil(endpoints[i] / endpoints[i - 1]))
		operations += 2
	print(operations)
	for item in res:
		print(item)


import os
import sys
from io import BytesIO, IOBase
import math

def solution():
	n = int(input())
	ans = 0
	arr = []
	while n > 0:
		val = math.ceil(n ** 0.5)
		if n == 2:
			break
		for i in range(val + 1, n):
			arr.append([i, n])
		arr.append([n, val])
		arr.append([n, val])
		n = val
	print(len(arr))
	for i in arr:
		print(*i)
	return

def main():
	testcases = 1
	testcases = int(input())
	for _ in range(testcases):
		solution()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, 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')
main()


import sys
input = sys.stdin.readline
t = int(input())
for you in range(t):
	n = int(input())
	l = []
	if n == 3:
		print(2)
		print(3, 2)
		print(3, 2)
		continue
	curr = n
	for i in range(n - 1, 3, -1):
		if i == 65536 or i == 256 or i == 16 or (i == 4):
			z = curr
			while z != 1:
				l.append((curr, i))
				z = (z + i - 1) // i
			curr = i
		else:
			l.append((i, curr))
	l.append((3, 4))
	l.append((4, 2))
	l.append((4, 2))
	print(len(l))
	for i in l:
		print(i[0], i[1])


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ans = []
	if n <= 8:
		temp = n
		cnt = 0
		while temp != 1:
			temp = (temp + 1) // 2
			cnt += 1
		for i in range(3, n):
			ans.append(f'{i} {n}')
		for i in range(cnt):
			ans.append(f'{n} {2}')
		print(len(ans))
		print(*ans, sep='\n')
	else:
		temp = n
		cnt = 0
		while temp != 1:
			temp = (temp + 7) // 8
			cnt += 1
		for i in range(3, n):
			if i != 8:
				ans.append(f'{i} {n}')
		for i in range(cnt):
			ans.append(f'{n} {8}')
		ans.append(f'{8} {2}')
		ans.append(f'{8} {2}')
		ans.append(f'{8} {2}')
		print(len(ans))
		print(*ans, sep='\n')


import math
for _ in range(int(input())):
	n = int(input())
	ans = []
	if n <= 16:
		for i in range(3, n):
			ans.append((i, n))
		temp = n
		while temp > 1:
			ans.append((n, 2))
			temp = math.ceil(temp / 2)
	else:
		for i in range(3, n):
			if i == 16:
				continue
			ans.append((i, n))
		temp = n
		while temp > 1:
			ans.append((n, 16))
			temp = math.ceil(temp / 16)
		k = 16
		temp = k
		while temp > 1:
			ans.append((k, 2))
			temp = math.ceil(temp / 2)
	print(len(ans))
	for i in range(len(ans)):
		print(*ans[i])


from math import ceil, sqrt
t = int(input())
for _ in range(t):
	n = int(input())
	end = n
	steps = []
	while ceil(sqrt(end)) > 2:
		for i in range(int(ceil(sqrt(end))) + 1, end):
			steps.append((i, end))
		steps.append((end, ceil(sqrt(end))))
		steps.append((end, ceil(sqrt(end))))
		end = ceil(sqrt(end))
	if end == 3:
		steps.append((3, 2))
		steps.append((3, 2))
	elif end == 4:
		steps.append((3, 4))
		steps.append((4, 2))
		steps.append((4, 2))
	print(len(steps))
	for i in steps:
		print(i[0], i[1])


from sys import stdin, stdout
import math
import heapq
t = 1

def aint():
	return int(input().strip())

def lint():
	return list(map(int, input().split()))

def fint():
	return list(map(int, stdin.readline().split()))

def operate(ar, x, y):
	if x != y:
		ar[x] = (ar[x] - 1) // ar[y] + 1

def main():
	n = aint()
	i = n - 1
	ar = [i for i in range(n + 1)]
	ans = []
	while i > 2:
		if i * i >= ar[n]:
			operate(ar, i, n)
			ans.append([i, n])
			i -= 1
		else:
			operate(ar, n, i)
			ans.append([n, i])
	while ar[n] != 1:
		operate(ar, n, 2)
		ans.append([n, 2])
	print(len(ans))
	for i in ans:
		print(*i)
t = int(input())
for i in range(t):
	main()


import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def output(*args):
	sys.stdout.buffer.write(('\n'.join(map(str, args)) + '\n').encode('utf-8'))

def main():
	t = int(input())
	ans_a = [''] * t
	for ti in range(t):
		n = int(input())
		m = n
		ans = []
		for i in range(n - 1, 2, -1):
			if (m + i - 1) // i >= i:
				ans.append(f'{n} {i}')
				m = (m + i - 1) // i
			ans.append(f'{i} {n}')
		while m > 1:
			ans.append(f'{n} {2}')
			m = (m + 1) // 2
		assert len(ans) <= n + 5
		ans_a[ti] = f'{len(ans)}\n' + '\n'.join(ans)
	output(*ans_a)
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	if n == 3:
		ans = [[3, 2], [3, 2]]
	elif n == 4:
		ans = [[3, 4], [4, 2], [4, 2]]
	elif n < 9:
		ans = []
		for i in range(3, n):
			ans.append([i, n])
		for i in range(4):
			ans.append([n, 2])
	else:
		ans = []
		for i in range(3, n):
			if i == 4 or i == 8:
				continue
			ans.append([i, n])
		for i in range(6):
			ans.append([n, 8])
		ans.append([8, 4])
		ans.append([8, 4])
		ans.append([4, 2])
		ans.append([4, 2])
	print(len(ans))
	for i in ans:
		print(*i)


import sys
input = sys.stdin.readline
import math
import copy
import collections
from collections import deque
inf = 10 ** 18
for _ in range(int(input())):
	n = int(input())
	ans = []
	special = []
	num = n
	exp = 1
	while num > 2:
		special.append(num)
		num = math.ceil(num / 4 ** exp)
		exp += 1
	special.append(2)
	a = []
	for i in range(len(special) - 1):
		a.append(math.ceil(special[i] / special[i + 1]))
	a.append(2)
	s = set(special)
	for i in range(1, n):
		if i != 1 and i not in special:
			ans.append([i, i + 1])
	index = dict()
	for i in range(len(special)):
		index[i] = special[i]
	for i in range(len(special) - 1):
		ans.append([special[i], special[i + 1]])
	for i in range(len(a) - 2, 0, -1):
		a[i] = math.ceil(a[i] / a[i - 1])
		ans.append([index[i], index[i - 1]])
	for i in range(len(a) - 2, 0, -1):
		if a[i] != 1 and a[i - 1] != 1:
			a[i] = math.ceil(a[i] / a[i - 1])
			ans.append([index[i], index[i - 1]])
	while a[0] > 1:
		a[0] = math.ceil(a[0] / 2)
		ans.append([index[0], index[len(a) - 1]])
	print(len(ans))
	for i in range(len(ans)):
		(u, v) = ans[i]
		print(u, v)


from sys import stdout
from math import ceil
t = int(input())
for i in range(t):
	n = int(input())
	if n <= 256:
		temp = n
		count = 0
		while temp > 1:
			count += 1
			temp = ceil(temp / 2)
		print(count + (n - 3))
		for i in range(3, n):
			stdout.write(str(i) + ' ' + str(n) + '\n')
		for i in range(count):
			stdout.write(str(n) + ' ' + str(2) + '\n')
	else:
		for i in range(0, 11):
			if pow(2, 2 * i) >= n:
				p1 = pow(2, i)
				break
		for i in range(0, 11):
			if pow(2, 2 * i) >= p1:
				p2 = pow(2, i)
				break
		temp = p2
		count = 0
		while temp > 1:
			count += 1
			temp = ceil(temp / 2)
		print(count + 4 + (n - 5))
		for i in range(3, n):
			if i != p1 and i != p2:
				stdout.write(str(i) + ' ' + str(n) + '\n')
		stdout.write(str(n) + ' ' + str(p1) + '\n')
		stdout.write(str(n) + ' ' + str(p1) + '\n')
		stdout.write(str(p1) + ' ' + str(p2) + '\n')
		stdout.write(str(p1) + ' ' + str(p2) + '\n')
		for i in range(count):
			stdout.write(str(p2) + ' ' + str(2) + '\n')


import sys
import math, bisect, operator
(inf, mod) = (float('inf'), 10 ** 9 + 7)
sys.setrecursionlimit(10 ** 5)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict
I = lambda : int(sys.stdin.readline())
neo = lambda : map(int, sys.stdin.readline().split())
Neo = lambda : list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	n = I()
	Ans = []
	(low, t) = (3, n)
	while t > 2:
		low = math.ceil(t ** 0.5)
		for i in range(low + 1, t):
			Ans += [(i, t)]
		Ans += [(t, low)]
		Ans += [(t, low)]
		t = low
	print(len(Ans))
	for (i, j) in Ans:
		print(i, j)


import sys
import math
input = sys.stdin.readline

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)

def ceil(n):
	if n == int(n):
		return int(n)
	else:
		return int(n) + 1

def main():
	for _ in range(int(input())):
		n = int(input())
		a = []
		k = n
		ind = n
		for i in range(n - 1, 2, -1):
			if ceil(k / i) > i:
				a.append([ind, i])
				k = ceil(k / i)
				a.append([i, ind])
			else:
				a.append([i, ind])
		m = k
		while ceil(k / 2) > 1:
			a.append([ind, 2])
			k = ceil(k / 2)
			if ceil(k / 2) == 1:
				a.append([ind, 2])
				break
		print(len(a))
		for i in a:
			print(*i)
	return
main()


from math import ceil

def test():
	n = int(input())
	res = []
	while n > 2:
		rt = ceil(n ** (1 / 2))
		for i in range(rt + 1, n):
			res.append((i, i + 1))
		res.append((n, rt))
		res.append((n, rt))
		n = rt
	print(len(res))
	for sol in res:
		print(f'{sol[0]} {sol[1]}')
tc = int(input())
for t in range(tc):
	test()


import math
for _ in range(int(input())):
	n = int(input())
	res = []
	copy = n
	for i in range(n - 1, 2, -1):
		if i * i <= copy:
			res.append((n, i))
			copy = math.ceil(copy / i)
		res.append((i, n))
	while copy > 1:
		res.append((n, 2))
		copy = (copy + 1) // 2
	print(len(res))
	for i in res:
		print(*i)


from math import ceil, sqrt
t = int(input())
for i in range(t):
	n = int(input())
	keep = []
	temp_n = n
	while temp_n >= 3:
		keep.append(temp_n)
		temp_n = ceil(sqrt(temp_n))
	keep.append(2)
	print(n - 3 + len(keep))
	keep_index = 0
	for j in range(n, 1, -1):
		if keep[keep_index] == j:
			keep_index += 1
		else:
			print(str(j) + ' ' + str(n))
	for j in range(len(keep) - 1):
		print(str(keep[j]) + ' ' + str(keep[j + 1]))
		print(str(keep[j]) + ' ' + str(keep[j + 1]))


import math

def ans(N):
	arr = list(range(1, N + 1))
	ret = []
	if N <= 64:
		for i in range(3, N):
			ret += [(i, N)]
			arr[i - 1] = math.ceil(arr[i - 1] / N)
		while arr[-1] != 1:
			ret += [(N, 2)]
			arr[-1] = math.ceil(arr[-1] / 2)
	else:
		REDUCER = 13
		for i in range(3, N):
			if i == REDUCER:
				continue
			ret += [(i, N)]
			arr[i - 1] = math.ceil(arr[i - 1] / N)
		while arr[-1] != 1:
			ret += [(N, REDUCER)]
			arr[-1] = math.ceil(arr[-1] / REDUCER)
		while arr[REDUCER - 1] != 1:
			ret += [(REDUCER, 2)]
			arr[REDUCER - 1] = math.ceil(arr[REDUCER - 1] / 2)
	assert len(ret) <= N + 5
	assert arr.count(1) == N - 1
	assert arr.count(2) == 1
	print(len(ret))
	for r in ret:
		print(*r)
for _ in range(int(input())):
	N = int(input())
	ans(N)


t = int(input())
from math import *

def f(n, arr, val, i):
	if val == 0:
		return True
	if n == 0 or val < 0:
		return False
	if arr[i] > val:
		return f(n - 1, arr, val, i + 1)
	return f(n - 1, arr, val, i + 1) or f(n - 1, arr, val - arr[i], i + 1)
for i in range(t):
	n = int(input())
	count = 0
	arr = []
	if n > 10:
		for i in range(n - 1, 2, -1):
			if i != 10 and i != 4:
				arr.append([str(i), str(n)])
				count += 1
		c = n
		while c != 1:
			arr.append([str(n), str(10)])
			c = ceil(c / 10)
			count += 1
		c = 10
		while c != 1:
			arr.append([str(10), str(4)])
			c = ceil(c / 4)
			count += 1
		c = 4
		while c != 1:
			arr.append([str(4), str(2)])
			c = c // 2
			count += 1
		print(count)
		for jp in range(count):
			print(' '.join(arr[jp]))
			els = 1
	else:
		for i in range(n - 1, 2, -1):
			arr.append([str(i), str(n)])
			count += 1
		c = n
		while c != 1:
			arr.append([str(n), str(2)])
			c = ceil(c / 2)
			count += 1
		print(count)
		for jp in range(count):
			print(' '.join(arr[jp]))
			els = 1


t = int(input())
for _ in range(t):
	n = int(input())
	ans = []
	if n > 12:
		for i in range(13, n):
			ans.append((i, n))
		prod = 1
		while prod < n:
			ans.append((n, 12))
			prod *= 12
		for i in range(5, 12):
			ans.append((i, 12))
		ans.append((12, 4))
		ans.append((12, 3))
		ans.append((3, 4))
		ans.append((4, 2))
		ans.append((4, 2))
	else:
		for i in range(3, n):
			ans.append((i, n))
		prod = 1
		while prod < n:
			ans.append((n, 2))
			prod *= 2
	print(len(ans))
	for (i, j) in ans:
		print(i, j)


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	if n <= 2:
		print(0)
		return
	if n < 10:
		mv = [n]
	else:
		num = n
		mv = []
		while num > 2:
			mv.append(num)
			num = num // 10
		if mv[-1] > 10:
			mv.append(5)
	ans = []
	for i in range(3, n + 1):
		if i in mv:
			continue
		ans.append((i, n))
	m = len(mv)
	for i in range(m - 1):
		(n1, n2) = (mv[i], mv[i + 1])
		while n1 > 1:
			ans.append((mv[i], mv[i + 1]))
			n1 = n1 // n2 if n1 % n2 == 0 else n1 // n2 + 1
	mi = mv[-1]
	while mi > 1:
		ans.append((mv[-1], 2))
		mi = mi // 2 if mi % 2 == 0 else mi // 2 + 1
	print(len(ans))
	for (a, b) in ans:
		print(a, b)
	return
for _ in range(int(input())):
	main()


from math import ceil, sqrt
import bisect
t = int(input())
for _ in range(t):
	n = int(input())
	exclude = []
	m = n
	while m > 2:
		bisect.insort(exclude, ceil(sqrt(m)))
		m = ceil(sqrt(m))
	print(n - 2 + len(exclude))
	for i in range(2, n):
		if i not in exclude:
			print(i, n, sep=' ')
	for i in range(len(exclude) - 1, -1, -1):
		if i == len(exclude) - 1:
			print(n, exclude[i], sep=' ')
			print(n, exclude[i], sep=' ')
		else:
			print(exclude[i + 1], exclude[i], sep=' ')
			print(exclude[i + 1], exclude[i], sep=' ')


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	ans = []
	if n > 8:
		for i in range(3, n):
			if i != 8:
				ans.append([i, n])
				pass
		nk = n
		while n != 1:
			ans.append([nk, 8])
			n = ceil(n / 8)
		ans.append([8, 2])
		ans.append([8, 2])
		ans.append([8, 2])
		print(len(ans))
		for i in ans:
			print(*i)
	else:
		for i in range(3, n):
			if i != 8:
				ans.append([i, n])
				pass
		nk = n
		while n != 1:
			ans.append([nk, 2])
			n = ceil(n / 2)
		print(len(ans))
		for i in ans:
			print(*i)


t = int(input())
for _ in range(t):
	n = int(input())
	if n <= 2 ** 5:
		print(n + 2)
		for i in range(3, n):
			print(i, i + 1)
		for i in range(1, 6):
			print(n, 2)
	else:
		print(n + 5)
		so = 3
		while so ** 5 <= n:
			so += 1
		for i in range(3, so):
			print(i, i + 1)
		for i in range(so + 1, so ** 3):
			print(i, i + 1)
		for i in range(so ** 3 + 1, n):
			print(i, i + 1)
		print(n, so ** 3)
		print(n, so ** 3)
		print(so ** 3, so)
		print(so ** 3, so)
		print(so ** 3, so)
		for i in range(5):
			print(so, 2)


from collections import Counter
import math

def solve(N):
	nums = [i for i in range(N + 1)]
	cnt = 0
	steps = []
	can = set()
	n = N
	while n > 2:
		can.add(n)
		n = math.ceil(math.sqrt(n))
	for i in range(3, N, 1):
		if i in can:
			continue
		nums[i] = math.ceil(nums[i] / nums[-1])
		cnt += 1
		steps.append((i, N))
	for idx in sorted(can, reverse=True):
		target = math.ceil(math.sqrt(nums[idx]))
		while nums[idx] > 1:
			nums[idx] = math.ceil(nums[idx] / nums[target])
			steps.append((idx, target))
	cn = Counter(nums)
	assert len(cn) == 3
	assert cn[0] == 1
	assert cn[1] == N - 1
	assert cn[2] == 1
	assert cnt <= N + 5
	return steps
T = int(input())
for _ in range(T):
	N = int(input())
	ans = solve(N)
	print(len(ans))
	for (x, y) in ans:
		print(x, y)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, 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')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	b = n
	a = min(n - 1, 8)
	ans = []
	for i in range(3, n):
		if i == a:
			continue
		ans.append((i, n))
	ia = a
	ib = b
	while b != 1:
		ans.append((ib, ia))
		b = Ceil(b, a)
	while a != 1 and ia != 2:
		ans.append((ia, 2))
		a = Ceil(a, 2)
	print(len(ans))
	for i in ans:
		print(*i)


from math import ceil
t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	l.append(n)
	while l[-1] != 2:
		l.append(ceil(l[-1] ** 0.5))
	ans = []
	for i in range(2, n):
		if i not in l:
			ans.append((i, n))
	for i in range(len(l) - 1):
		ans.append((l[i], l[i + 1]))
		ans.append((l[i], l[i + 1]))
	print(len(ans))
	for item in ans:
		print(item[0], item[1])


t = int(input())
while t:
	t -= 1
	n = int(input())
	l = []
	x = 2
	while x < n:
		l.append(x)
		x *= x
	l.append(n)
	ans = []
	for i in range(3, n):
		if not i in l:
			ans.append([i, n])
	l = l[::-1]
	y = len(l)
	for i in range(0, y - 1):
		ans.append([l[i], l[i + 1]])
		ans.append([l[i], l[i + 1]])
	print(len(ans))
	for i in ans:
		print(f'{i[0]} {i[1]}')


import math

def calc(x):
	if x <= 2:
		return
	else:
		y = math.ceil(math.sqrt(x))
		for i in range(y + 1, x):
			S.append((i, x))
		S.append((x, y))
		S.append((x, y))
		calc(y)

def solution(n):
	calc(n)
	return S
for t in range(int(input())):
	S = []
	S = solution(int(input()))
	print(len(S))
	for (x, y) in S:
		print(x, y)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	ans = []
	while n != 2:
		s = int(math.ceil(math.sqrt(n)))
		for j in range(s + 1, n):
			ans.append((j, n))
		ans += [(n, s)] * 2
		n = s
	print(len(ans))
	for i in ans:
		print(i[0], i[1])


import math
for _ in range(int(input())):
	n = int(input())
	i = n
	a = []
	a.append(2)
	a.append(n)
	while math.sqrt(i) > 2:
		if int(math.ceil(math.sqrt(i))) ** 2 == i:
			a.append(int(math.ceil(math.sqrt(i))) + 1)
		else:
			a.append(int(math.ceil(math.sqrt(i))))
		i = a[-1]
	j = 3
	print(n - 1 - len(a) + 2 * (len(a) - 1))
	for i in range(3, n):
		if j <= i:
			j += 1
		if i in a:
			continue
		print(i, j)
	a.sort()
	for i in range(len(a) - 2, -1, -1):
		print(a[i + 1], a[i])
		print(a[i + 1], a[i])


from math import exp, log, ceil
for i in range(int(input())):
	n = int(input())
	bud = ceil(exp(0.618 * log(n)))
	nv = n
	bv = bud
	ops = []
	for i in range(3, n):
		if i != bud:
			ops += [(i, n)]
	while nv > 1 and bv > 1:
		if nv > bv:
			nv = ceil(nv / bv)
			ops += [(n, bud)]
		else:
			bv = ceil(bv / nv)
			ops += [(bud, n)]
	if bv > 1:
		nv = bv
		n = bud
	bud = 2
	bv = 2
	while nv > 1 and bv > 1:
		if nv > bv:
			nv = ceil(nv / bv)
			ops += [(n, bud)]
		else:
			bv = ceil(bv / nv)
			ops += [(bud, n)]
	print(len(ops))
	for op in ops:
		print(*op)


import bisect
from itertools import accumulate
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, 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))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def SieveOfEratosthenes(n):
	prime = []
	primes = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if primes[p] == True:
			prime.append(p)
			for i in range(p * p, n + 1, p):
				primes[i] = False
		p += 1
	return prime
for j in range(int(input())):
	n = int(input())
	y = []
	while n > 2:
		i = n - 1
		while (i - 1) ** 2 >= n:
			y.append([i, n])
			i -= 1
		y.append([n, i])
		y.append([n, i])
		n = i
	print(len(y))
	for i in y:
		print(*i)


for _ in range(int(input())):
	n = int(input())
	a = [2, 3, 5, 22, 448, 200000]
	na = []
	for i in range(len(a)):
		if a[i] < n:
			na.append(a[i])
		else:
			na.append(n)
			break
	na.reverse()
	ans = []
	for i in range(1, len(na)):
		for j in range(na[i] + 1, na[i - 1]):
			ans.append([j, na[i - 1]])
		ans.append([na[i - 1], na[i]])
		ans.append([na[i - 1], na[i]])
	print(len(ans))
	for x in ans:
		print(*x)


from math import ceil, sqrt
for i in range(int(input())):
	n = int(input())
	nsave = n
	count = 0
	while n > 2:
		y = ceil(sqrt(n))
		count += n - y + 1
		n = y
	print(count)
	n = nsave
	while n > 2:
		y = ceil(sqrt(n))
		for x in range(y + 1, n):
			print(x, n)
		print(n, y)
		print(n, y)
		n = y


from sys import stdin
input = stdin.readline
from math import ceil, sqrt

def help():
	n = int(input().strip())
	toleave = []
	ii = n
	while ii > 2:
		toleave.append(ii)
		ii = ceil(sqrt(ii))
	toleave.sort(reverse=True)
	tt = 0
	toprint = []
	for i in range(3, n):
		if i not in toleave:
			tt += 1
			toprint.append([i, n])
	for i in range(len(toleave) - 1):
		toprint.append([toleave[i], toleave[i + 1]])
		toprint.append([toleave[i], toleave[i + 1]])
	ap = [i for i in range(1, n + 1)]
	for i in toprint:
		ap[i[0] - 1] = ceil(ap[i[0] - 1] / ap[i[1] - 1])
	for i in range(2, n):
		while ap[i] != 1:
			toprint.append([i + 1, 2])
			ap[i] = ceil(ap[i] / 2)
	print(len(toprint))
	for i in toprint:
		print(*i)
for _ in range(int(input())):
	help()


from sys import stdin
from math import sqrt, floor, ceil
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	A = [x for x in range(n + 1)]
	curr = n
	prev = n
	res = []
	while curr > 3:
		curr_sqrt = floor(sqrt(curr))
		for i in range(curr_sqrt + 1, prev):
			res.append((i, n))
			A[i] = ceil(i // n)
		prev = curr_sqrt + 1
		res.append((n, curr_sqrt))
		curr = ceil(curr / curr_sqrt)
	if n == 3:
		res.append((3, 2))
		res.append((3, 2))
	elif A[3] == 3:
		while curr > 1:
			res.append((n, 3))
			curr = ceil(curr / 3)
		res.append((3, 2))
		res.append((3, 2))
	else:
		while curr > 1:
			res.append((n, 2))
			curr = ceil(curr / 2)
		res.append((n, 2))
	print(len(res))
	for (x, y) in res:
		print(x, y)


from sys import stdin
input = stdin.readline
from heapq import heapify, heappush, heappop, heappushpop
from collections import defaultdict as dd, deque as dq, Counter as C
from math import factorial as f, ceil, gcd, sqrt, log
from bisect import bisect_left as bl, bisect_right as br
from itertools import combinations as c, permutations as p
mp = lambda : map(int, input().split())
it = lambda : int(input())
ls = lambda : list(input().strip())
for _ in range(it()):
	a = it()
	v = []
	for j in range(a - 1, 1, -1):
		y = ceil(sqrt(a))
		if j != y:
			v.append([j, a])
		else:
			v.append([a, y])
			v.append([a, y])
			a = y
	print(len(v))
	for k in v:
		print(*k)


import math

def div(n):
	if n == 2:
		return
	x = math.ceil(math.sqrt(n))
	for i in range(x + 1, n):
		print(i, n)
	print(n, x)
	print(n, x)
	div(x)
for _ in range(int(input())):
	n = int(input())
	count = 0
	x = n
	while x != 2:
		count += 1
		x = math.ceil(math.sqrt(x))
	print(n - 2 + count)
	div(n)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, 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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def solve(case):
	n = int(inp())
	ans = []
	if n > 8:
		for i in range(n - 1, 8, -1):
			ans.append((i, n))
		temp = n
		while temp != 1:
			ans.append((n, 8))
			temp = ceil(temp / 8)
		for i in range(3, 8):
			ans.append((i, 8))
		for i in range(3):
			ans.append((8, 2))
	else:
		for i in range(3, n):
			ans.append((i, n))
		temp = n
		while temp != 1:
			ans.append((n, 2))
			temp = ceil(temp / 2)
	print(len(ans))
	for (i, j) in ans:
		print(i, j)
testcase(int(inp()))


t = int(input())
for i in range(t):
	n = int(input())
	if n == 3:
		print(2)
		print(3, 2)
		print(3, 2)
		continue
	if n < 28:
		num = n
		temp = 0
		while num > 1:
			temp2 = int(num % 3 != 0)
			num //= 3
			num += temp2
			temp += 1
		print(n - 4 + temp + 2)
		for i in range(3, n - 1):
			print(i + 1, n)
		for i in range(temp):
			print(n, 3)
		print(3, 2)
		print(3, 2)
	elif n < 257:
		num = n
		temp = 0
		while num > 1:
			temp2 = int(num % 27 != 0)
			num //= 27
			num += temp2
			temp += 1
		print(n - 5 + temp + 5)
		for i in range(3, 26):
			print(i + 1, n)
		for i in range(27, n - 1):
			print(i + 1, n)
		for i in range(temp):
			print(n, 27)
		print(27, 3)
		print(27, 3)
		print(27, 3)
		print(3, 2)
		print(3, 2)
	else:
		num = n
		temp = 0
		while num > 1:
			temp2 = int(num % 256 != 0)
			num //= 256
			num += temp2
			temp += 1
		print(n - 5 + temp + 6)
		print(3, n)
		for i in range(4, 255):
			print(i + 1, n)
		for i in range(256, n - 1):
			print(i + 1, n)
		for i in range(temp):
			print(n, 256)
		print(256, 4)
		print(256, 4)
		print(256, 4)
		print(256, 4)
		print(4, 2)
		print(4, 2)


import math
for i in range(int(input())):
	n = int(input())
	a = [i for i in range(1, n + 1)]
	g = [-1] * n
	g[0] = 0
	g[1] = 0
	k = n
	x = []
	y = []
	c = 0
	while k != 2:
		g[k - 1] = 0
		k = int(math.ceil(math.sqrt(k)))
	for j in range(1, n):
		if g[j] == -1:
			c = c + 1
			x.append(j + 1)
			y.append(n)
	for j in range(n - 1, 1, -1):
		if g[j] == 0:
			c = c + 2
			y.append(int(math.ceil(math.sqrt(j + 1))))
			y.append(int(math.ceil(math.sqrt(j + 1))))
			x.append(j + 1)
			x.append(j + 1)
	print(c)
	for j in range(c):
		print(x[j], y[j])


from math import ceil
for _ in range(int(input())):
	n = int(input())
	num = n
	Div = []
	if n > 8:
		for i in range(3, n):
			if i != 8:
				Div.append([i, n])
		while n != 1:
			Div.append([num, 8])
			n = ceil(n / 8)
		n = 8
		while n != 1:
			Div.append([8, 2])
			n //= 2
	else:
		for i in range(3, n):
			Div.append([i, n])
		while n != 1:
			Div.append([num, 2])
			n = ceil(n / 2)
	print(len(Div))
	for i in Div:
		print(*i)


import math

def cout(n):
	if n < 3:
		return
	k = math.ceil(n ** 0.5)
	for i in range(k + 1, n):
		ans[0] += 1
		hello.append([i, n])
	ans[0] += 2
	hello.append([n, k])
	hello.append([n, k])
	cout(k)
	return
for _ in range(int(input())):
	ans = [0]
	hello = []
	n = int(input())
	cout(n)
	print(ans[0])
	for i in hello:
		print(*i)


def cd(x, y):
	return (x + y - 1) // y
t = int(input())
for _ in range(t):
	out = []
	n = int(input())
	large = n
	for i in range(n - 1, 1, -1):
		while cd(large, i) in [i, i + 1]:
			out.append(str(n) + ' ' + str(i))
			large = cd(large, i)
		out.append(str(i) + ' ' + str(n))
	print(len(out))
	print('\n'.join(out))


import math
t = int(input())
for k in range(t):
	n = int(input())
	if n > 8:
		print(n - 1 + math.ceil(math.log10(n) / math.log10(8)))
		for g in range(3, n):
			if g != 8:
				print(g, n)
		for y in range(math.ceil(math.log10(n) / math.log10(8))):
			print(n, 8)
		print(8, 2)
		print(8, 2)
		print(8, 2)
	else:
		print(math.ceil(math.log2(n)) + n - 3)
		for g in range(3, n):
			print(g, n)
		b = n
		while b > 1:
			print(n, 2)
			b = math.ceil(b / 2)


import math
for t in range(int(input())):
	ans = []
	n = int(input())
	while n > 2:
		x = n
		for i in range(n - 1, math.ceil(n ** 0.5), -1):
			ans.append([i, n])
		ans.append([n, math.ceil(n ** 0.5)])
		ans.append([n, math.ceil(n ** 0.5)])
		n = math.ceil(n ** 0.5)
	print(len(ans))
	for i in ans:
		print(*i)


import sys
from math import ceil, sqrt
input = lambda : sys.stdin.readline().strip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def ii():
	return int(input())

def fi():
	return float(input())

def si():
	return input().strip()
for _ in range(ii()):
	n = ii()
	a = list([i + 1 for i in range(n)])
	l = []
	temp = a[-1]
	while temp > 2:
		l.append(temp)
		temp = ceil(sqrt(temp))
	l += [2]
	i = 0
	ans = []
	y = a[-1]
	for x in range(n, 2, -1):
		if l[i] >= x:
			i += 1
		else:
			ans.append([x, y])
	for i in range(len(l) - 1):
		ans.append([l[i], l[i + 1]])
		ans.append([l[i], l[i + 1]])
	m = len(ans)
	print(m)
	for i in range(m):
		print(ans[i][0], ans[i][1])


for i in range(int(input())):
	n = int(input())
	if n <= 4:
		print(n - 1)
		for i in range(3, n):
			print(i, i + 1)
		print(n, 2)
		print(n, 2)
	elif n <= 8:
		print(n)
		for i in range(3, n):
			print(i, i + 1)
		for i in range(3):
			print(n, 2)
	else:
		print(n + 5)
		for i in range(9, n):
			print(i, i + 1)
		for i in range(6):
			print(n, 8)
		for i in range(3, 8):
			print(i, i + 1)
		for i in range(3):
			print(8, 2)


import sys
LI = lambda : list(map(int, sys.stdin.readline().split()))
MI = lambda : map(int, sys.stdin.readline().split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline())
from math import ceil
for _ in range(II()):
	n = II()
	(l, r) = (1, n)
	ans = []
	while r > 2:
		m = ceil(r ** (1 / 2))
		for i in range(m + 1, r):
			ans.append((i, r))
		ans.append((r, m))
		ans.append((r, m))
		r = m
	print(len(ans))
	for (a, b) in ans:
		print(a, b)


for no_test in range(int(input())):
	k = int(input())
	if k == 3:
		print('2\n3 2\n3 2')
		continue
	(a5, a1, a3) = (k, 1, 1)

	def ceil(x):
		if x > int(x):
			return int(x) + 1
		else:
			return int(x)
	a3 = ceil(a5 ** (3 / 5))
	a1 = ceil(a5 ** (1 / 5))
	n1 = 0
	if a1 > 2:
		a = a1
		while a >= 2:
			a = ceil(a / 2)
			n1 += 1
	if k > 32:
		print(k - 5 + 3 + 2 + n1)
	else:
		print(k - 4 + 3 + 2 + n1)
	for i in range(3, k):
		if i in [a1, a3, a5]:
			continue
		else:
			print(i, i + 1)
	for i in range(2):
		print(a5, a3)
	for i in range(3):
		print(a3, a1)
	for i in range(n1):
		print(a1, 2)


import sys
input = sys.stdin.readline
import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = [0] + list(range(1, n + 1))
	ans = []
	cnt = 0
	for i in range(n - 1, 2, -1):
		if i * i < a[n]:
			ans.append([n, i])
			a[n] = math.ceil(a[n] / i)
			cnt += 1
		ans.append([i, n])
		a[i] = math.ceil(a[i] / n)
		cnt += 1
	while a[n] > 1:
		ans.append([n, 2])
		a[n] = math.ceil(a[n] / 2)
		cnt += 1
	print(len(ans))
	for i in range(len(ans)):
		print(*ans[i])


from math import ceil, log
for t in range(int(input())):
	n = int(input())
	if n <= 256:
		m = n - 3 + ceil(log(n, 2))
		print(m)
		for i in range(3, n):
			print(i, i + 1)
		for _ in range(ceil(log(n, 2))):
			print(n, 2)
	else:
		d = 8
		l = [d]
		while l[-1] * d < n:
			l += [l[-1] * d]
		l += [n]
		print(n + 5)
		m = 0
		for i in range(3, n):
			if i not in l:
				m += 1
				print(i, i + 1)
		a = {i: i for i in l + [2]}
		s = sorted(a)
		if n > 2 ** 17:
			print(s[-1], s[-2])
			print(s[-1], s[1])
			del a[n]
			m += 2
		while len(a) > 1:
			s = sorted(a, key=lambda x: a[x])
			a[s[-1]] = ceil(a[s[-1]] / a[s[-2]])
			print(s[-1], s[-2])
			a = {i: a[i] for i in a if a[i] != 1}
			m += 1
		for _ in range(n + 5 - m):
			print(1, 2)


from sys import stdin, stdout
from math import ceil, floor, sqrt
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	output = []
	prev = n
	target = floor(sqrt(n))
	for i in range(n - 1, 2, -1):
		if i != target:
			output.append(f'{i} {n}\n')
		else:
			output.append(f'{n} {i}\n')
			output.append(f'{i} {n}\n')
			prev = ceil(prev / target)
			target = floor(sqrt(prev))
	while prev != 1 and n > 2:
		prev = ceil(prev / 2)
		output.append(f'{n} 2\n')
	stdout.write(f'{len(output)}\n')
	for item in output:
		stdout.write(item)


from math import ceil, sqrt

def solve():
	n = int(input())
	remain = [1] * (n + 1)
	ans = []
	ans_suffix = []
	cur = n
	while cur > 2:
		nxt = int(ceil(sqrt(cur)))
		ans_suffix += [(cur, nxt), (cur, nxt)]
		remain[nxt] = 0
		cur = nxt
	for i in range(2, n):
		if remain[i]:
			ans += [(i, n)]
	ans += ans_suffix
	print(len(ans))
	for (x, y) in ans:
		print(x, y)
import sys, os, io
input = sys.stdin.readline
stdout = io.BytesIO()
sys.stdout.write = lambda s: stdout.write(s.encode('ascii'))
for _ in range(int(input())):
	solve()
os.write(1, stdout.getvalue())


import math
import sys
input = iter(sys.stdin.read().splitlines()).__next__

def solve():
	n = int(input())
	ops = []
	while n > 2:
		division = math.ceil(math.sqrt(n))
		for i in range(division + 1, n):
			ops.append(f'{i} {n}')
		ops.append(f'{n} {division}')
		ops.append(f'{n} {division}')
		n = division
	return ops
t = int(input())
output = []
for _ in range(t):
	ops = solve()
	output.append(len(ops))
	output.extend(ops)
print(*output, sep='\n')


from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, Counter, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 998244353
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('0')
file = 1

def ceil(a, b):
	return (a + b - 1) // b

def solve():
	for _ in range(ii()):
		n = ii()
		ans = []
		while n > 2:
			s = int(sqrt(n)) + 1
			for i in range(s + 1, n):
				ans.append([i, i + 1])
			ans.append([n, s])
			ans.append([n, s])
			n = s
		print(len(ans))
		for i in ans:
			print(*i)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


from math import ceil

def solve(n):
	if n <= 256:
		m = 0
		x = n
		while x > 1:
			m += 1
			x = ceil(x / 2)
		print(n - 3 + m)
		for i in range(3, n):
			print(i, ' ', n)
		a = n
		while a > 1 and n > 2:
			print(n, ' ', 2)
			a = ceil(a / 2)
	else:
		m = 0
		x = n
		while x > 1:
			m += 1
			x = ceil(x / 32)
		print(n - 4 + m + 5)
		for i in range(3, n):
			if i != 32:
				print(i, ' ', n)
		a = n
		while a > 1:
			print(n, ' ', 32)
			a = ceil(a / 32)
		print(32, ' ', 2)
		print(32, ' ', 2)
		print(32, ' ', 2)
		print(32, ' ', 2)
		print(32, ' ', 2)
t = int(input())
while t:
	t -= 1
	n = int(input())
	solve(n)


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()

def FACT(n, mod):
	s = 1
	facts = [1]
	for i in range(1, n + 1):
		s *= i
		s %= mod
		facts.append(s)
	return facts[n]

def C(n, k, mod):
	return FACT(n, mod) * pow(FACT(k, mod) * FACT(n - k, mod) % mod, mod - 2, mod) % mod
t = II()
for q in range(t):
	n = II()
	d = []
	temp = n
	xx = 8
	while xx < temp:
		d.append(xx)
		xx *= 8
	d.append(temp)
	d.reverse()
	ans = []
	for i in range(3, n):
		if i not in d:
			ans.append([i, i + 1])
	for i in range(len(d) - 1):
		ans.append([d[i], d[i + 1]])
	for i in range(len(d) - 1):
		ans.append([d[i], d[i + 1]])
	temp = d[-1]
	while temp != 1:
		ans.append([d[-1], 2])
		temp = math.ceil(temp / 2)
	print(len(ans))
	for i in ans:
		print(*i)


from sys import stdin, stdout
import math
import heapq
import collections
input = stdin.readline

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputstrings():
	return [x for x in input().split()]

def inputstringnum():
	return [ord(x) - ord('a') for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	n = inputnum()
	cur = n
	rtn = []
	for i in reversed(range(3, n)):
		if cur // i + (0 != cur % i) > i:
			rtn.append(n)
			rtn.append(i)
			cur = cur / i + (0 != cur % i)
		rtn.append(i)
		rtn.append(n)
	while cur != 1:
		rtn.append(n)
		rtn.append(2)
		cur = cur // 2 + (0 != cur % 2)
	print(len(rtn) // 2)
	for i in range(0, len(rtn), 2):
		print(rtn[i], rtn[i + 1])


def main():
	from sys import stdin
	input = stdin.buffer.readline
	for _ in range(int(input())):
		n = int(input())
		for i in range(1, n):
			x = n
			y = i
			c = 0
			while min(x, y) > 1:
				if x < y:
					(x, y) = (y, x)
				x = (x + y - 1) // y
				c += 1
			if max(x, y) == 2 and c <= 8:
				print(n - 3 + c)
				for j in range(2, n):
					if j != i:
						print(j, n)
				x = n
				y = i
				while min(x, y) > 1:
					if x < y:
						print(i, n)
						y = (y + x - 1) // x
					else:
						print(n, i)
						x = (x + y - 1) // y
				break
main()


import math
for i in range(int(input())):
	n = int(input())
	steps = []
	if n <= 64:
		for i in range(n - 1, 2, -1):
			steps.append((i, n))
		val = n
		while n > 1:
			steps.append((val, 2))
			n = math.ceil(n / 2)
	else:
		for i in range(n - 1, 2, -1):
			if i == 64:
				continue
			steps.append((i, n))
		val = n
		while n > 1:
			steps.append((val, 64))
			n = math.ceil(n / 64)
		x = 64
		while x > 1:
			steps.append((64, 2))
			x = math.ceil(x / 2)
	print(len(steps))
	for i in steps:
		print(i[0], i[1])


def readInt():
	return int(input())

def readLine():
	return [int(s) for s in input().split(' ')]

def readString():
	return input()

def isGoodPivot(k, n):
	s1 = k
	s2 = n
	S1 = s1 > s2
	while True:
		if S1:
			s1 = -(s1 // -s2)
		else:
			s2 = -(s2 // -s1)
		S1 = not S1
		if min(s1, s2) == 1:
			if max(s1, s2) == 2:
				return True
			else:
				return False
from math import sqrt, ceil

def calPath(n):
	y = lambda x: ceil(sqrt(x))
	ret = []
	while n > 2:
		n = y(n)
		ret.append(n)
	return ret

def ask(n):
	path = calPath(n)
	stuff = set(path)
	ret = []
	for i in range(2, n, 1):
		if i in stuff:
			continue
		ret.append((i, n))
	last = n
	for t in path:
		ret.append((last, t))
		ret.append((last, t))
		last = t
	return ret
t = readInt()
for _ in range(t):
	n = readInt()
	result = ask(n)
	print(len(result))
	for (a, b) in result:
		print(a, b)


def solve1(n):
	a = []
	c = 0
	for i in range(3, n):
		a.append(i)
		a.append(i + 1)
		c = c + 1
	t = 0
	z = n
	while z != 1:
		z = (z + 1) // 2
		t = t + 1
	for i in range(t):
		a.append(n)
		a.append(2)
	print(c + t)
	d = 0
	for i in range(0, len(a)):
		print(a[i], end=' ')
		d = d + 1
		if d == 2:
			d = 0
			print()

def solve2():
	n = int(input())
	if n <= 8:
		solve1(n)
	else:
		a = []
		c = 0
		for i in range(3, n):
			if i != 8:
				a.append(i)
				a.append(n)
		nn = n
		while nn != 1:
			a.append(n)
			a.append(8)
			nn = (nn + 7) // 8
		a.append(8)
		a.append(2)
		a.append(8)
		a.append(2)
		a.append(8)
		a.append(2)
		print(len(a) // 2)
		d = 0
		for i in range(0, len(a)):
			print(a[i], end=' ')
			d = d + 1
			if d == 2:
				d = 0
				print()
for testis in range(int(input())):
	solve2()


import math
for nt in range(int(input())):
	n = int(input())
	if n > 8:
		ans = []
		k = n
		for i in range(9, n):
			ans.append((i, n))
		while k != 1:
			ans.append((n, 8))
			k = math.ceil(k / 8)
		ans.append((3, 8))
		for i in range(5, 8):
			ans.append((i, 8))
		ans.append((8, 4))
		ans.append((8, 4))
		ans.append((4, 2))
		ans.append((4, 2))
	elif n > 4:
		ans = []
		for i in range(3, n):
			if i != 4:
				ans.append((i, n))
		ans.append((n, 4))
		ans.append((n, 4))
		ans.append((4, 2))
		ans.append((4, 2))
	elif n == 3:
		ans = [(3, 2), (3, 2)]
	elif n == 4:
		ans = [(3, 4), (4, 2), (4, 2)]
	print(len(ans))
	for i in ans:
		print(i[0], i[1])


def solve():
	N = int(input())
	s = set()
	i = 2
	while i < N:
		s.add(i)
		i **= 2
	s.add(N)
	ans = []
	for i in range(2, N + 1):
		if i not in s:
			ans.append((i, N))
	s = sorted(list(s))
	for i in range(len(s) - 2, -1, -1):
		ans.append((s[i + 1], s[i]))
		ans.append((s[i + 1], s[i]))
	print(len(ans))
	for i in ans:
		print(i[0], i[1])
		pass
for tc in range(int(input())):
	solve()


from math import ceil
t = int(input())
for _ in range(t):
	n = int(input())
	res = []
	while n > 2:
		s = ceil(n ** 0.5)
		for i in range(s + 1, n):
			res.append([i, i + 1])
		res.append([n, s])
		res.append([n, s])
		n = s
	print(len(res))
	for (a, b) in res:
		print(a, b)


import sys
input = sys.stdin.buffer.readline

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1 or n == 2:
		print(0)
	else:
		ans = []
		top = n
		while top > 2:
			nex = int(top ** 0.5 + 0.999)
			for i in range(nex + 1, top):
				ans.append([i, top])
			ans.append([top, nex])
			ans.append([top, nex])
			top = nex
		print(len(ans))
		multiLineArrayOfArraysPrint(ans)


t = int(input())
while t > 0:
	n = int(input())
	ans = []
	if n <= 8:
		for i in range(n - 1, 2, -1):
			ans.append([i, n])
		temp = n
		while temp > 1:
			ans.append([n, 2])
			if temp / 2 == temp // 2:
				temp = temp / 2
			else:
				temp = temp // 2 + 1
	else:
		for i in range(n - 1, 8, -1):
			ans.append([i, n])
		temp = n
		while temp > 1:
			ans.append([n, 8])
			if temp / 8 == temp // 8:
				temp = temp / 8
			else:
				temp = temp // 8 + 1
		for i in range(7, 2, -1):
			ans.append([i, 8])
		temp = 8
		while temp > 1:
			ans.append([8, 2])
			if temp / 2 == temp // 2:
				temp = temp / 2
			else:
				temp = temp // 2 + 1
	print(len(ans))
	for i in ans:
		print(i[0], i[1])
	t -= 1


def solve():
	n = int(input())
	num = n - 1
	den = n
	ans = ''
	count = 0
	while den > 2:
		while not (den / num <= num and den / (num - 1) > num - 1):
			ans += '\n' + str(num) + ' ' + str(den)
			count += 1
			num -= 1
		ans += '\n' + str(den) + ' ' + str(num)
		ans += '\n' + str(den) + ' ' + str(num)
		count += 2
		den = num
		num -= 1
	ans = str(count) + ans
	print(ans)

def main():
	T = int(input())
	for c in range(T):
		solve()
main()


UPOW = 4
for _ in ' ' * int(input()):
	n = int(input())
	sq = int(n ** (1 / UPOW))
	if sq ** UPOW != n:
		sq += 1
	nt = n
	sqt = sq
	ops = []
	for i in range(3, n):
		if i != sq:
			ops.append((i, n))
	while nt > 1:
		ops.append((n, sq))
		nt = (nt + sq - 1) // sq
	while sqt > 1 and sq != 2:
		ops.append((sq, 2))
		sqt = (sqt + 1) // 2
	print(len(ops))
	for o in ops:
		print(*o)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):

	def __init__(self, file):
		self.newlines = 0
		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()

def RL():
	return map(int, sys.stdin.readline().split())

def RLL():
	return list(map(int, sys.stdin.readline().split()))

def N():
	return int(input())

def S():
	return input().strip()

def print_list(l):
	print(' '.join(map(str, l)))
from math import ceil
for _ in range(N()):
	n = N()
	res = []
	for i in range(3, min(64, n)):
		res.append((i, n))
	for i in range(65, min(64 * 64, n)):
		res.append((i, n))
	for i in range(64 * 64 + 1, n):
		res.append((i, n))
	if n > 64 * 64:
		res.append((n, 64 * 64))
		res.append((n, 64 * 64))
		res.append((64 * 64, 64))
		res.append((64 * 64, 64))
		res += [(64, 2)] * 6
	elif n > 64:
		res.append((n, 64))
		res.append((n, 64))
		res += [(64, 2)] * 6
	elif n > 2:
		nn = n
		while n > 1:
			res.append((nn, 2))
			n = ceil(n / 2)
	print(len(res))
	for (i, j) in res:
		print(i, j)


t = int(input())
while t:
	t += -1
	n = int(input())
	check = [0] * (n + 1)
	ans = []
	tmp = []
	a = n
	while a > 2:
		check[a] = 1
		tmp.append(a)
		b = a ** 0.5
		if b != int(b):
			b = int(b) + 1
		a = int(b)
	for i in range(3, n + 1):
		if check[i] == 0:
			ans.append([i, n])
	for i in range(0, len(tmp) - 1):
		ans.append([tmp[i], tmp[i + 1]])
		ans.append([tmp[i], tmp[i + 1]])
	n = len(tmp)
	ans.append([tmp[n - 1], 2])
	ans.append([tmp[n - 1], 2])
	print(len(ans))
	for i in ans:
		print(*i)


from sys import stdin, stdout
import math, bisect
from collections import Counter, deque, defaultdict
L = lambda : list(map(int, stdin.readline().strip().split()))
I = lambda : int(stdin.readline().strip())
S = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()

def pr(a):
	print(' '.join(list(map(str, a))))

def solve():
	n = I()
	x = n
	ans = []
	m = 16
	for i in range(n - 1, 2, -1):
		if i != m:
			ans += [(i, n)]
	if n > 16:
		while x > 1:
			ans += [(n, m)]
			x = (x + 15) // m
		te = m
		while te > 1:
			ans += [(m, 2)]
			te = (te + 1) // 2
	else:
		while x > 1:
			ans += [(n, 2)]
			x = (x + 1) // 2
	print(len(ans))
	for i in ans:
		pr(i)
for _ in range(I()):
	solve()


from sys import stdin, exit
from bisect import bisect_left as bl, bisect_right as br
input = lambda : stdin.readline()[:-1]
intput = lambda : int(input())
sinput = lambda : input().split()
intsput = lambda : map(int, sinput())

def dprint(*args, **kwargs):
	if debugging:
		print(*args, **kwargs)
debugging = 1
t = intput()
for _ in range(t):
	n = intput()
	if n > 8:
		print(n + 5)
		for i in range(2, n):
			if i not in (4, 8):
				print(i, n)
		for j in range(6):
			print(n, 8)
		print(8, 4)
		print(4, 8)
		print(4, 8)
	else:
		inst = []
		for i in range(2, n - 1):
			inst.append((i, n))
		(a, b) = (n - 1, n)
		swap = False
		while (a, b) != (1, 2):
			b = (b + a - 1) // a
			if not swap:
				inst.append((n, n - 1))
			else:
				inst.append((n - 1, n))
			if a > b:
				swap = not swap
				(a, b) = (b, a)
		print(len(inst))
		for (a, b) in inst:
			print(a, b)


import io
import os
from collections import Counter, defaultdict, deque

def solve(N):
	ans = []
	A = list(range(1, N + 1))

	def ceilDiv(i, j):
		ans.append(str(i + 1) + ' ' + str(j + 1))
		assert i != j
		x = A[i]
		y = A[j]
		A[i] = (x + y - 1) // y
	for i in reversed(range(1, N - 1)):
		if (A[N - 1] + A[i] - 1) // A[i] >= A[i]:
			ceilDiv(N - 1, i)
		if A[i] != 2:
			ceilDiv(i, N - 1)
	assert A[1] == 2
	while A[N - 1] != 1:
		ceilDiv(N - 1, 1)
	return str(len(ans)) + '\n' + '\n'.join(ans)
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	ans = solve(N)
	print(ans)


T = int(input())
r = 1

def getans(target, n):
	ans = []
	num1 = target
	num2 = n
	flag = True
	while True:
		if num1 > num2:
			num1 = (num1 - 1) // num2 + 1
			ans.append([target, n])
		elif num1 < num2:
			num2 = (num2 - 1) // num1 + 1
			ans.append([n, target])
		else:
			if num1 == 2 and num2 == 2:
				ans.append([target, n])
				break
			else:
				flag = False
			break
	if len(ans) > 7 or not flag:
		return []
	ans = ans[::-1]
	for i in range(n - 2):
		if i + 2 == target:
			continue
		ans.append([i + 2, n])
	return ans[::-1]
while r <= T:
	n = int(input())
	for target in range(2, int(n ** 0.5 + 1) + 1):
		ans = getans(target, n)
		if len(ans) == 0:
			continue
		else:
			break
	print(len(ans))
	for ele in ans:
		print(ele[0], ele[1])
	r += 1


t = int(input())

def chk(n):
	g = n
	c = 0
	if n > 12:
		while n != 0:
			n = int(n / 12)
			c += 1
		print(g + c)
		for i in range(g - 4):
			if i < 9:
				print(i + 3, g)
			else:
				print(i + 4, g)
		for i in range(c):
			print(g, 12)
		print(12, 2)
		print(12, 2)
		print(12, 2)
		print(12, 2)
	else:
		if n > 8 and n < 13:
			print(n + 1)
		elif n < 9 and n > 4:
			print(n)
		else:
			print(n - 1)
		for i in range(n - 3):
			print(i + 3, n)
		while n != 1:
			print(g, 2)
			n = n - int(n / 2)
			c += 1
l = []
for i in range(t):
	l.append(int(input()))
for i in l:
	chk(i)


import math

def ceil_divisions(num):
	num_index = num
	old_value = num
	pairs = []
	while num > 3:
		next_value = int(math.floor(math.sqrt(num)))
		for value in range(next_value + 1, old_value):
			pairs.append((value, num_index))
		old_value = next_value + 1
		pairs.append((num_index, next_value))
		num = int(math.ceil(num / next_value))
	if old_value > 3:
		pairs.append((3, num_index))
	if num > 2:
		pairs.append((num_index, 2))
	pairs.append((num_index, 2))
	return pairs
num_test = int(input())
for i in range(num_test):
	num = int(input())
	pairs = ceil_divisions(num)
	print(len(pairs))
	for pair in pairs:
		print(f'{pair[0]} {pair[1]}')


from typing import List

def fun(n: int, R: List[str]):
	if n == 2:
		return
	x = n - 1
	while (n + x - 1) // x <= x:
		x -= 1
	x += 1
	for i in range(x + 1, n):
		R.append(f'{i} {i + 1}')
	R.append(f'{n} {x}')
	R.append(f'{n} {x}')
	fun(x, R)
for _ in range(int(input())):
	n = int(input())
	R = []
	fun(n, R)
	print(len(R))
	print('\n'.join(R))


from math import ceil
t = int(input())
for i in range(t):
	n = int(input())
	z = n
	res = []
	for j in range(n - 1, 2, -1):
		if ceil(z / j) >= j:
			res.append(str(n) + ' ' + str(j))
			z = ceil(z / j)
		res.append(str(j) + ' ' + str(n))
	while z > 2:
		z = ceil(z / 2)
		res.append(str(n) + ' 2')
	res.append('2 ' + str(n))
	print(len(res))
	print('\n'.join(res))


import math
for _ in range(int(input())):
	n = int(input())
	if n < 8:
		(n2, a) = (n, 0)
		while n2 > 1:
			n2 = math.ceil(n2 / 2)
			a += 1
		print(n - 3 + a)
		for i in range(3, n):
			print(i, n)
		for i in range(a):
			print(n, 2)
	else:
		(n2, a) = (n, 0)
		while n2 > 1:
			n2 = math.ceil(n2 / 8)
			a += 1
		if n != 8:
			print(n - 1 + a)
		else:
			print(8)
		for i in range(3, 8):
			print(i, n)
		for i in range(9, n):
			print(i, n)
		if n != 8:
			for i in range(a):
				print(n, 8)
		for i in range(3):
			print(8, 2)


q = int(input())
import math
for _ in range(q):
	n = int(input())
	(res, r) = (0, 0)
	l = [2, 4, 16, 16 * 16, 16 * 16 * 16 * 16]
	if n in l:
		k = l.index(n)
		print(n - 3 + 2 * k - (k - 1))
		for i in range(3, n):
			if not i in l:
				print(str(i) + ' ' + str(i + 1))
		for i in range(k):
			print(str(n) + ' ' + str(int(math.sqrt(n))))
			print(str(n) + ' ' + str(int(math.sqrt(n))))
			n = int(math.sqrt(n))
	else:
		for i in range(5):
			if n < l[i]:
				r = l[i - 1]
				break
		if r == 0:
			r = l[-1]
		k = l.index(r)
		print(n - 3 + 2 * k + 2 - k)
		for i in range(3, n):
			if not i in l:
				print(str(i) + ' ' + str(i + 1))
		print(str(n) + ' ' + str(r))
		print(str(n) + ' ' + str(r))
		for i in range(k):
			print(str(r) + ' ' + str(int(math.sqrt(r))))
			print(str(r) + ' ' + str(int(math.sqrt(r))))
			r = int(math.sqrt(r))


def oot(n):
	lst1 = [n]
	nt = n
	while nt > 2:
		nt = nt ** (1 / 2)
		if nt != int(nt):
			nt = int(nt) + 1
		lst1.append(int(nt))
	print(n - 3 + len(lst1))
	for i in range(3, n):
		if i not in lst1:
			print(i, i + 1)
	for i in range(len(lst1) - 1):
		print(lst1[i], lst1[i + 1])
		print(lst1[i], lst1[i + 1])
t = int(input())
for i in range(t):
	x = int(input())
	oot(x)


for _ in range(int(input())):
	n = int(input())
	cur = n
	down = {cur}
	while cur > 2:
		(bsl, bsr) = (1, cur)
		while bsl + 1 < bsr:
			bsm = (bsl + bsr) // 2
			if bsm ** 2 < cur:
				bsl = bsm
			else:
				bsr = bsm
		cur = bsr
		down.add(cur)
	ans = [(i, n) for i in range(3, n) if i not in down]
	down = sorted(down, reverse=True)
	for (i, j) in zip(down, down[1:]):
		ans.append((i, j))
		ans.append((i, j))
	print(len(ans))
	for (i, j) in ans:
		print(i, j)


from math import ceil

def isvalid(a, b):
	nops = 0
	while a != 1 and b != 1:
		nops += 1
		(a, b) = (ceil(max(a, b) / min(a, b)), min(a, b))
	return nops <= 7 and b == 2 or a == 2
for tc in range(int(input())):
	n = int(input())
	i = 1
	while True:
		i += 1
		if isvalid(i, n):
			target = i
			break
	ops = []
	for i in range(2, n):
		if i != target:
			ops.append((i, n))
	(a, b) = (target, n)
	while (a, b) not in ((1, 2), (2, 1)):
		if a > b:
			ops.append((target, n))
			a = ceil(a / b)
		else:
			ops.append((n, target))
			b = ceil(b / a)
	print(len(ops))
	for (x, y) in ops:
		print(x, y)


for u in range(int(input())):
	n = int(input())
	y = []
	while n > 2:
		i = n - 1
		while (i - 1) ** 2 >= n:
			y.append([i, n])
			i -= 1
		y.append([n, i])
		y.append([n, i])
		n = i
	print(len(y))
	for i in y:
		print(*i)


for _ in range(int(input())):
	n = int(input())
	if n <= 8:
		temp = n
		cnt = 0
		while temp != 1:
			temp = (temp + 1) // 2
			cnt += 1
		print(cnt + n - 3)
		for i in range(3, n):
			print(i, n)
		for i in range(cnt):
			print(n, 2)
	else:
		temp = n
		cnt = 0
		while temp != 1:
			temp = (temp + 7) // 8
			cnt += 1
		print(cnt + n - 1)
		for i in range(3, n):
			if i != 8:
				print(i, n)
		for i in range(cnt):
			print(n, 8)
		print(8, 2)
		print(8, 2)
		print(8, 2)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass

def pmat(A):
	for ele in A:
		print(*ele, end='\n')

def seive():
	prime = [1 for i in range(10 ** 6 + 1)]
	prime[0] = 0
	prime[1] = 0
	for i in range(10 ** 6 + 1):
		if prime[i]:
			for j in range(2 * i, 10 ** 6 + 1, i):
				prime[j] = 0
	return prime
for term in range(L()[0]):
	n = L()[0]
	c = min(32, int(n ** 0.5) + 1)
	A = [i for i in range(n + 1)]
	ans = []
	for i in range(3, c):
		if A[i] != 1:
			A[i] = ceil(A[i] / c)
			ans.append([i, c])
	for i in range(c + 1, n):
		if A[i] != 1:
			A[i] = ceil(A[i] / n)
			ans.append([i, n])
	while A[n] != 1 and n != 2:
		A[n] = ceil(A[n] / c)
		ans.append([n, c])
	while A[c] != 1 and c != 2:
		A[c] = ceil(A[c] / 2)
		ans.append([c, 2])
	print(len(ans))
	for ele in ans:
		print(*ele)
endtime = time.time()


import sys
input = sys.stdin.buffer.readline

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
numbers = [2, 4]
while numbers[-1] < 200000:
	numbers.append(numbers[-1] * numbers[-2])
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1 or n == 2:
		print(0)
	elif n == 3:
		print(2)
		print('3 2')
		print('3 2')
	else:
		ans = []
		for i in range(len(numbers) - 1):
			if numbers[i + 1] > n:
				b = numbers[i]
				a = numbers[i - 1]
				break
		for i in range(2, n):
			if i != a and i != b:
				ans.append([i, n])
		if n != b:
			ans.append([n, b])
			ans.append([n, b])
		aa = a
		bb = b
		while not (aa == 1 or bb == 1):
			if bb > aa:
				ans.append([b, a])
				bb = (bb + aa - 1) // aa
			else:
				ans.append([a, b])
				aa = (aa + bb - 1) // bb
		print(len(ans))
		multiLineArrayOfArraysPrint(ans)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp = lambda : list(map(int, sys.stdin.readline().rstrip('\r\n').split()))
from math import *
tc = 1
(tc,) = inp()
for _ in range(tc):
	(n,) = inp()
	m = n
	a = [n]
	while m != 2:
		t = ceil(m ** 0.5)
		a.append(t)
		m = t
	s = set(a)
	ans = [i for i in range(1, n + 1)]
	res = []
	for j in range(3, n):
		if j not in s:
			res.append((j, j + 1))
	for i in range(1, len(a)):
		res.append((a[i - 1], a[i]))
		res.append((a[i - 1], a[i]))
	print(len(res))
	for (i, j) in res:
		print(i, j)


from math import ceil, sqrt

def solve():
	n = int(input())
	remain = [1] * (n + 1)
	ans = []
	ans_suffix = []
	cur = n
	while cur > 2:
		nxt = int(ceil(sqrt(cur)))
		ans_suffix.append('{} {}'.format(cur, nxt))
		ans_suffix.append('{} {}'.format(cur, nxt))
		remain[nxt] = 0
		cur = nxt
	for i in range(2, n):
		if remain[i]:
			ans.append('{} {}'.format(i, n))
	return ans + ans_suffix
res = []
for _ in range(int(input())):
	ans = solve()
	res.append(len(ans))
	res.append('\n'.join(ans))
print(*res, sep='\n')


import sys
t = int(sys.stdin.readline().strip())
for _ in range(t):
	n = int(sys.stdin.readline().strip())
	if n <= 8:
		cnt = n - 1 - 2
		tl = 0
		x = n
		while x != 1:
			x = (x + 1) // 2
			tl += 1
		print(cnt + tl)
		for j in range(n - 1, 2, -1):
			print(j, n)
		for j in range(tl):
			print(n, 2)
	else:
		cnt = n - 1
		x = n
		tl = 0
		while x != 1:
			x = (x + 7) // 8
			tl += 1
		print(cnt + tl)
		for j in range(n - 1, 2, -1):
			if j != 8:
				print(j, n)
		for j in range(tl):
			print(n, 8)
		for j in range(3):
			print(8, 2)


import sys
import os
import math
import copy
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations, accumulate

def Int():
	return int(sys.stdin.readline())

def Mint():
	return map(int, sys.stdin.readline().split())

def Lstr():
	return list(sys.stdin.readline().strip())

def Str():
	return sys.stdin.readline().strip()

def Mstr():
	return map(str, sys.stdin.readline().strip().split())

def List():
	return list(map(int, sys.stdin.readline().split()))

def Hash():
	return dict()

def Mod():
	return 1000000007

def Mat2x2(n):
	return [List() for _ in range(n)]

def Lcm(x, y):
	return x * y // gcd(x, y)

def dtob(n):
	return bin(n).replace('0b', '')

def btod(n):
	return int(n, 2)

def watch(x):
	return print(x)

def common(l1, l2):
	return set(l1).intersection(l2)

def Most_frequent(list):
	return max(set(list), key=list.count)

def solution():
	for _ in range(Int()):
		n = Int()
		ans = []
		while n > 2:
			m = ceil(n ** 0.5)
			for i in range(m + 1, n):
				ans.append([i, i + 1])
			ans.append([n, m])
			ans.append([n, m])
			n = m
		print(len(ans))
		for i in ans:
			print(i[0], i[1])
solution()


from math import ceil

def ceiling(n):
	blanck = []
	lst = [i for i in range(1, n + 1)]
	for i in range(2, len(lst) - 1):
		lst[i] = 1
		blanck.append([i + 1, len(lst)])
	maxi = max(lst)
	while maxi != 1:
		maxi = ceil(maxi / 2)
		blanck.append([len(lst), 2])
	if len(blanck) <= n + 5:
		print(len(blanck))
		for i in blanck:
			print(*i)
		return ''
	else:
		cnst = 25
		blanck = []
		lst = [i for i in range(1, n + 1)]
		for i in range(2, len(lst) - 1):
			if lst[i] != cnst:
				lst[i] = 1
				blanck.append([i + 1, len(lst)])
		maxi = max(lst)
		while maxi != 1:
			maxi = ceil(maxi / cnst)
			blanck.append([len(lst), cnst])
		lst[-1] = 1
		maxi = max(lst)
		while maxi != 1:
			maxi = ceil(maxi / 2)
			blanck.append([cnst, 2])
		if len(blanck) <= n + 5:
			print(len(blanck))
			for i in blanck:
				print(*i)
			return ''
for i in range(int(input())):
	print(ceiling(int(input())))


import sys

def input():
	return sys.stdin.readline().strip()

def solve():
	for t in range(int(input())):
		n = int(input())
		j = n
		res = []
		while j != 2:
			x = int(j ** 0.5)
			if x * x < j:
				x += 1
			for i in range(x + 1, j):
				res.append((i, i + 1))
			res.append((j, x))
			res.append((j, x))
			j = x
		print(len(res))
		for (a, b) in res:
			print(a, b)
solve()


from math import sqrt, ceil
for _ in range(int(input())):
	ans = []

	def go(n):
		N = n
		x = ceil(sqrt(N))
		N -= 1
		while N > x:
			ans.append((N, n))
			N -= 1
		ans.append((n, x))
		ans.append((n, x))
		return int(x)
	n = int(input())
	while n > 2:
		n = go(n)
	print(len(ans))
	for i in range(len(ans)):
		print(*ans[i])


import math

def move2(n):
	move = 0
	while n != 1:
		n = math.ceil(n / 2)
		move += 1
	return move

def moven(n, k):
	move = 0
	while n != 1:
		n = math.ceil(n / k)
		move += 1
	return move

def solve():
	n = int(input())
	ans = []
	for i in range(3, n + 1):
		move1 = move2(i)
		move = moven(n, i)
		if move + move1 <= 9:
			divisor = i
			break
	if n == 3:
		print(2)
		print(3, 2)
		print(3, 2)
	else:
		for i in range(3, n):
			if i == divisor:
				continue
			ans.append([i, i + 1])
		for i in range(move):
			ans.append([n, divisor])
		for i in range(move1):
			ans.append([divisor, 2])
		print(len(ans))
		for lst in ans:
			print(lst[0], lst[1])
for i in range(int(input())):
	solve()


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
from bisect import bisect, bisect_left
from itertools import permutations as per
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
nCr = lambda x, y: f[x] * inv(f[y] * f[x - y] % N) % N
inv = lambda x: pow(x, N - 2, N)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	n = I()
	ans = []
	for i in range(3, n):
		if i != 16:
			ans += ((i, n),)
	p = n
	if n > 16:
		while n != 1:
			ans += ((p, 16),)
			n = (n + 15) // 16
		p = 16
	f = p
	while p != 1:
		ans += ((f, 2),)
		p = (p + 1) // 2
	print(len(ans))
	for i in ans:
		print(*i)


import sys, math, time
t = int(sys.stdin.readline().strip(' '))
for _ in range(t):
	n = int(sys.stdin.readline().strip(' '))
	k = n
	ans = []
	c = 0
	while math.ceil(k ** 0.5) >= 2:
		ele = math.ceil(k ** 0.5)
		for i in range(k - 1, ele, -1):
			ans.append(str(i) + ' ' + str(k))
		c += k + 1 - ele
		ans.append(str(k) + ' ' + str(ele))
		ans.append(str(k) + ' ' + str(ele))
		k = ele
		if k == 2:
			break
	print(c)
	print('\n'.join(ans))


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, 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')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=300006, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
		return pCrawl != None and pCrawl.isEndOfWord

class Node:

	def __init__(self, data):
		self.data = data
		self.height = 0
		self.left = None
		self.right = None

class BinaryTrie:

	def __init__(self):
		self.root = Node(0)

	def insert(self, pre_xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = pre_xor & 1 << i
			if val == 0:
				if not self.temp.right:
					self.temp.right = Node(0)
				self.temp = self.temp.right
			elif val >= 1:
				if not self.temp.left:
					self.temp.left = Node(0)
				self.temp = self.temp.left

	def do(self, temp):
		if not temp:
			return 0
		ter = temp
		temp.height = self.do(ter.left) + self.do(ter.right)
		if temp.height == 0:
			temp.height += 1
		return temp.height

	def query(self, xor):
		self.temp = self.root
		cur = 0
		i = 31
		while i > -1:
			val = xor & 1 << i
			if not self.temp:
				return cur
			if val >= 1:
				self.opp = self.temp.right
				if self.temp.left:
					self.temp = self.temp.left
				else:
					return cur
			else:
				self.opp = self.temp.left
				if self.temp.right:
					self.temp = self.temp.right
				else:
					return cur
			if self.temp.height == pow(2, i):
				cur += 1 << i
				self.temp = self.opp
			i -= 1
		return cur
for ik in range(int(input())):
	n = int(input())
	if n <= 32:
		ans = []
		c = 0
		for i in range(3, n):
			ans.append((i, n))
			c += 1
		t = n
		while n > 1:
			c += 1
			ans.append((t, 2))
			n = int(math.ceil(n / 2))
		print(c)
		for i in ans:
			print(*i)
		continue
	c = 0
	ans = []
	for i in range(3, 32):
		ans.append((i, n))
		c += 1
	for i in range(33, n):
		ans.append((i, n))
		c += 1
	t = n
	c1 = 0
	while n > 1:
		c += 1
		c1 += 1
		ans.append((t, 32))
		n = math.ceil(n / 32)
		n = int(n)
	ans.append((32, 2))
	ans.append((32, 2))
	ans.append((32, 2))
	ans.append((32, 2))
	ans.append((32, 2))
	c += 5
	print(c)
	for i in ans:
		print(*i)


import math as m

def solve():
	n = int(input())
	x = n
	y = n
	i = -2
	while x > 2:
		i += 1
		x = m.ceil(m.sqrt(x))
	print(n + i)
	while n > 2:
		for j in range(m.ceil(m.sqrt(n)) + 1, n):
			print(j, n)
		print(n, m.ceil(m.sqrt(n)))
		print(n, m.ceil(m.sqrt(n)))
		n = m.ceil(m.sqrt(n))
t = int(input())
for s in range(0, t):
	solve()


import math
for _ in range(int(input())):
	n = int(input())
	operations = []
	r = int(n ** 0.5)
	sqrtlist = []
	while r > 2:
		sqrtlist.append(r)
		r = int(r ** 0.5)
	for i in range(3, n):
		if i not in sqrtlist:
			operations.append([i, n])
	r = n
	for item in sqrtlist:
		operations.append([r, item])
		operations.append([item, r])
		n = math.ceil(n / item)
	while n > 1:
		operations.append([r, 2])
		n = math.ceil(n / 2)
	print(len(operations))
	for operation in operations:
		print(operation[0], operation[1])


import math
t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	ans = []
	low = 3
	mx = n
	while mx > 2:
		low = math.ceil(math.sqrt(mx))
		for i in range(low + 1, mx):
			ans.append((i, mx))
		ans.append((mx, low))
		ans.append((mx, low))
		mx = low
		low = math.ceil(math.sqrt(mx))
	print(len(ans))
	for i in ans:
		print(i[0], i[1])


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [i + 1 for i in range(n)]
	ans = []
	for i in range(3, n)[::-1]:
		(x, y) = (i, n)
		if -(-a[y - 1] // a[x - 1]) >= a[x - 1]:
			ans.append((y, x))
			a[y - 1] = -(-a[y - 1] // a[x - 1])
		ans.append((x, y))
		a[x - 1] = -(-a[x - 1] // a[y - 1])
	(x, y) = (2, n)
	ans.append((y, x))
	a[y - 1] = -(-a[y - 1] // a[x - 1])
	ans.append((y, x))
	a[y - 1] = -(-a[y - 1] // a[x - 1])
	ans.append((y, x))
	a[y - 1] = -(-a[y - 1] // a[x - 1])
	print(len(ans))
	for res in ans:
		print(*res)


T = int(input())
for t in range(T):
	d = int(input())
	n = d
	ans = []
	while n > 2:
		for i in range(n):
			if i * i >= n:
				if n >= (i - 1) * (i - 1) + i - 1:
					for j in range(n - 1, i, -1):
						ans.append([j, n])
					ans.append([n, i])
					ans.append([n, i])
				else:
					for j in range(n - 1, i, -1):
						ans.append([j, n])
					ans.append([n, i - 1])
					ans.append([n, i])
				n = i
				break
	print(len(ans))
	for i in ans:
		print(*i, sep=' ')


import sys
import math
t = int(input())
ops = []

def divide(n):
	if n == 2:
		return
	newn = math.ceil(math.sqrt(n))
	for i in range(newn + 1, n):
		ops.append((i, n))
	ops.append((n, newn))
	ops.append((n, newn))
	divide(newn)
for i in range(t):
	n = int(input())
	ops = []
	divide(n)
	print(len(ops))
	for (a, b) in ops:
		print('{} {}'.format(a, b))


import math
for tt in range(int(input())):
	n = int(input())
	kn = n
	if n > 8:
		ops = []
		for i in range(3, n):
			if i == 8:
				continue
			ops.append([i, n])
		while n != 1:
			n = math.ceil(n / 8)
			ops.append([kn, 8])
		ops.append([8, 2])
		ops.append([8, 2])
		ops.append([8, 2])
	else:
		ops = []
		for i in range(3, n):
			ops.append([i, n])
		for i in range(math.ceil(math.log(n, 2))):
			ops.append([n, 2])
	print(len(ops))
	for i in ops:
		print(*i)


from math import *
for y in range(int(input())):
	n = int(input())
	lst = []
	while n > 2:
		temp = ceil(n ** 0.5)
		for i in range(n - 1, temp, -1):
			lst.append([i, n])
		lst.append([n, temp])
		lst.append([n, temp])
		n = temp
	print(len(lst))
	for i in lst:
		print(*i)


import os
import sys
from io import BytesIO, IOBase
import math
from collections import defaultdict, deque
from itertools import combinations
import random
BUFSIZE = 8192
MOD = pow(10, 9) + 7

class FastIO(IOBase):
	newlines = 0

	def __init__(self, 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')
for _ in range(int(input())):
	n = int(input())
	ans = []
	t = n
	if n <= 64:
		for i in range(3, n):
			ans.append((i, n))
		while n != 1:
			ans.append((t, 2))
			n = math.ceil(n / 2)
		print(len(ans))
		for i in ans:
			print(*i)
	else:
		for i in range(3, n):
			if i != 8 and i != 64:
				ans.append((i, n))
		while n != 1:
			n = math.ceil(n / 64)
			ans.append((t, 64))
		for i in range(2):
			ans.append((64, 8))
		for i in range(3):
			ans.append((8, 2))
		print(len(ans))
		for i in ans:
			print(*i)