-----
CHEF N TIMINGS
-----

One day chef was working with some random numbers. Then he found something
interesting. He observed that no 240, 567, 9999 and 122 and called these numbers
nice as the digits in numbers are in increasing order. Also he called 434, 452, 900
are not nice as digits are in decreasing order

	

Now you are given a no and chef
wants you to find out largest "nice" integer which is smaller than or equal to the
given integer.

		
		

-----Constraints-----

1< t < 1000 

1< N < 10^18
	

-----Input Format-----

First line contains no. of test cases t. Then t test cases follow. Each test case
contain a integer n.

-----Output-----

Output a integer for each test case in a new line which is largest nice
integer smaller or equal to the given integer. 

		
		

-----Example Text Case-----
Input:

1
132

Output:

129

Source: codechef | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
for _ in range(int(input())):
	n = input().rstrip()
	n = [ele for ele in n]
	l = len(n)
	m = 10 ** 18 + 8
	ini = 1
	for i in range(l - 1, -1, -1):
		if int(n[i]) <= m:
			if ini == 1:
				m = int(n[i])
			else:
				m = max(m, n[i])
		else:
			m = int(n[i]) - 1
			n[i] = str(m)
			for j in range(l - 1, i, -1):
				n[j] = '9'
	i = 0
	while n[i] == '0':
		i += 1
	print(''.join(n[i:]))


for _ in range(int(input())):
	num = int(input())
	if num < 10:
		print(num)
		continue
	digs = str(num)
	new = ''
	i = 1
	bad = False
	while i < len(digs):
		if digs[i - 1] > digs[i]:
			bad = True
			if digs[i - 1] != '1':
				new += str(int(digs[i - 1]) - 1) + '9' * (len(digs) - i)
			else:
				n = i - 1
				while n > 0 and digs[n] == '1':
					n -= 1
				new = new[:n]
				new += str(int(digs[n]) - 1) + '9' * (len(digs) - n - 1)
			break
		else:
			new += digs[i - 1]
		i += 1
	if not bad:
		new = digs
	print(int(new))


t = int(input())
for _ in range(t):
	n = int(input())
	c = str(n)
	a = list(c)
	i2 = len(a) - 1
	for i in range(len(a) - 1, 0, -1):
		if int(a[i]) < int(a[i - 1]):
			a[i - 1] = int(a[i - 1]) - 1
			i2 = i - 1
	if a[0] != 0 and a[0] != '0':
		for i in range(i2 + 1):
			print(a[i], end='')
		for i in range(i2 + 1, len(a)):
			print(9, end='')
	else:
		for i in range(1, i2):
			print(a[i], end='')
		for i in range(i2 + 1, len(a)):
			print(9, end='')
	print()


try:

	def increasing(n):
		if 0 <= n <= 9:
			return n

		def fi(s):
			for i in range(len(s) - 1):
				if s[i] > s[i + 1]:
					return i
			return -1
		s = str(n)
		n = len(s)
		r = fi(s)
		l = s.index(s[r])
		if r == -1:
			return n
		else:
			return int(s[:l] + str(int(s[l]) - 1) + '9' * (n - l - 1))
	for _ in range(int(input())):
		n = int(input())
		ans = increasing(n)
		print(ans)
except EOFError as e:
	pass


from sys import stdin, stdout
input = stdin.readline
from collections import defaultdict as dd
import math

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

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

def gets():
	return input()

def geta():
	return int(input())

def print_s(s):
	stdout.write(s + '\n')

def solve():
	for _ in range(geta()):
		n = geta()
		num = list(map(int, list(str(n))))
		index = -1
		for i in range(len(num) - 1):
			if num[i] > num[i + 1]:
				index = i
				break
		else:
			print(n)
			continue
		if index != -1:
			fake = num[index]
			for i in range(index, -1, -1):
				if num[i] == fake:
					num[i] -= 1
					last = i
			for i in range(last + 1, len(num)):
				num[i] = 9
			ans = 0
			for i in num:
				ans *= 10
				ans += i
			print(ans)

def __starting_point():
	solve()
__starting_point()


import sys

def func(number):
	for i in range(len(number) - 1):
		if int(number[i]) > int(number[i + 1]):
			cifra = int(number[i]) - 1
			stringa = number[:i] + str(cifra) + '9' * (len(number) - i - 1)
			return func(stringa)
	return number
t = int(input().strip())
for _ in range(t):
	number = input().strip()
	stringa = func(number)
	while stringa[0] == '0':
		stringa = stringa[1:]
	print(stringa)


def nondecdigits(s):
	m = len(s)
	a = [0] * m
	for i in range(m):
		a[i] = ord(s[i]) - ord('0')
	level = m - 1
	for i in range(m - 1, 0, -1):
		if a[i] < a[i - 1]:
			a[i - 1] -= 1
			level = i - 1
	if a[0] != 0:
		for i in range(level + 1):
			print(a[i], end='')
		for i in range(level + 1, m):
			print('9', end='')
	else:
		for i in range(1, level):
			print(a[i], end='')
		for i in range(level + 1, m):
			print('9', end='')
x = int(input())
for i in range(x):
	y = int(input())
	nondecdigits(str(y))
	print()


def check(s):
	for i in range(len(s) - 1):
		if int(s[i + 1]) < int(s[i]):
			return 1
	return 0
t = int(input())
for i in range(t):
	ok = 0
	n = list(input())
	l = []
	for k in n:
		try:
			l.append(int(k))
		except:
			pass
	while check(l):
		for j in range(len(l) - 1):
			if l[j + 1] < l[j]:
				l[j] = l[j] - 1
				for k in range(j + 1, len(l)):
					l[k] = 9
				break
	s = ''
	for i in l:
		s += str(i)
	print(int(s))


T = int(input())

def solve(n):
	li = list(map(int, list(str(n))))
	prev = 0
	f = {}
	for (ind, it) in enumerate(li):
		if it < prev:
			ind = f[prev]
			li[ind] -= 1
			for j in range(ind + 1, len(li)):
				li[j] = 9
			break
		prev = it
		if it not in f:
			f[it] = ind
	ans = 0
	for it in li:
		ans = 10 * ans + it
	return ans
for case in range(T):
	n = int(input())
	ans = solve(n)
	print(ans)


def check(n):
	l = list(map(int, str(n)))
	t = len(l)
	p = t - 1
	for i in range(t - 1, 0, -1):
		if l[i] < l[i - 1]:
			l[i - 1] -= 1
			p = i - 1
	for i in range(p + 1, t):
		l[i] = 9
	return int(''.join(map(str, l)))
try:
	for _ in range(int(input())):
		n = int(input())
		print(check(n))
except EOFError as e:
	pass


T = int(input())
for _ in range(T):
	N = int(input())
	V = list(str(N))
	ans = ''
	n = len(V)
	for i in range(n):
		if i != n - 1 and V[i] > V[i + 1]:
			ans = list(ans + str(int(V[i]) - 1) + '9' * (n - i - 1))
			for j in range(i - 1, -1, -1):
				if ans[j] > ans[j + 1]:
					ans[j + 1] = '9'
					ans[j] = str(int(ans[j]) - 1)
				else:
					break
			break
		else:
			ans = ans + V[i]
	ans1 = ''
	for i in ans:
		ans1 = ans1 + i
	print(int(ans1))


try:

	def convert(list):
		s = [str(i) for i in list]
		res = int(''.join(s))
		return res
	for _ in range(int(input())):
		x = int(input())
		while x > 0:
			a = str(x)
			b = list(a)
			b.sort()
			if b == list(a):
				break
			else:
				k = 0
				for j in b:
					if str(j) == a[k]:
						k += 1
					else:
						break
				k = convert(list(a[k + 1:]))
				x = x - k - 1
		print(x)
except:
	pass


def num(l):
	ans = 0
	for i in range(len(l)):
		ans = ans * 10 + l[i]
	return ans

def numof(n):
	ans = 0
	l = []
	while n > 0:
		l.append(n % 10)
		n = n // 10
		ans += 1
	l.reverse()
	return l
t = int(input())
for you in range(t):
	n = int(input())
	done = 0
	z = numof(n)
	l = []
	for i in range(len(z)):
		if min(z[i:]) == z[i]:
			l.append(z[i])
		else:
			l.append(min(z[i:]))
			done = 1
			break
	k = len(l)
	for i in range(len(z) - k):
		l.append(9)
	print(num(l))