-----
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

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))
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))