Chef Leonardo has a decimal integer $N$ and a non-zero decimal digit $d$. $N$ does not contain the digit zero; specifically, $N$ should always be treated as a decimal integer without leading zeroes.
Chef likes $d$ and does not like any other digit, so he decided to change $N$. He may apply the following operation any number of times (including zero): append the digit $d$ to the decimal representation of $N$ ($d$ becomes the least significant digit of $N$), then remove one occurrence of one digit from the decimal representation of $N$.
Chef has already changed a lot of numbers and he got bored. Now, he wants to know the smallest possible value of $N$ which can be obtained by performing the operation described above. Can you help him?

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $N$ and $d$.

-----Output-----
For each test case, print a single line containing one integer - the minimum number Chef can obtain.

-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le N \le 10^{18}$
- $N$ does not contain the digit $0$
- $1 \le d \le 9$

-----Subtasks-----
Subtask #1 (40 points):
- $1 \le T \le 100$
- $1 \le N \le 10^9$
Subtask #2 (60 points): original constraints

-----Example Input-----
3
35 4
42 4
24 9

-----Example Output-----
34
24
24

-----Explanation-----
Example case 1: Chef can remove the digit $5$ and add $4$ to the end of $N$. Then, $N$ becomes $34$.
Example case 2: Chef can remove the digit $4$ from the beginning of $N$, add $4$ to the end of $N$ and get $N = 24$.
Example case 3: Chef cannot make his number any smaller.

Difficulty: interview

Starter code:

Solutions:
for _ in range(int(input())):
 n,d=map(str,input().split())
 k=list(n)
 dd,c,n=d,0,len(n)
 for x in range(n):
  if int(k[n-x-1])>int(d):
   k.pop(n-x-1)
   c+=1 
  else:
   d=k[n-x-1]
 print(''.join(k)+c*dd)

# cook your dish here
from sys import stdin,stdout

for _ in range(int(input())):
 n,d=map(str,stdin.readline().split())
 li=list(n)
 s=d 
 # print(li)
 c=0
 for i in range(len(n)-1,-1,-1):
  if li[i]>d:
   li[i]=''
   c+=1 
  else:
   d=li[i]
 s=''.join(li)+c*s
 print(s)

# cook your dish here
for _ in range(int(input())):
 n,d = input().split()
 s = list(n)
 l=list(n)
 prev = d
 k=0
 for x in range(len(l)-1, -1, -1):
  if n[x]>prev:
   s[x]=''
   k+=1;
  else:
   prev=s[x]
   
 s = ''.join(s) + d*k
 print(s)

t = int(input())

while t:
 t = t-1
 n,d = input().split()
 n = list(n)
 d = int(d)
 
 n = list(map(int, n))
 
 x = []
 n.reverse()
 
 z = 0
 cc = d
 for c in n:
  # print(c, cc, z)
  if c > cc:
   z +=1
  else:
   x.append(c)
   cc = c
   
 x.reverse()
 
 for _ in range(z):
  x.append(d)
 x = list(map(str, x)) 
 print("".join(x))

t = int(input())
for i in range(t):
 l = list(map(int, input().split()))
 s = str(l[0])
 while 1:
  s = s+str(l[1])
  for x in range(len(s)-1):
   if s[x]>s[x+1]:
    s=s[:x]+s[x+1:]
    break
  else:
   s = s[:len(s)-1]
   break
 print(int(s))


t = int(input())
for i in range(t):
 l = list(map(int, input().split()))
 s = str(l[0])
 while 1:
  s = s+str(l[1])
  for x in range(len(s)-1):
   if s[x]>s[x+1]:
    s=s[:x]+s[x+1:]
    break
  else:
    s = s[:len(s)-1]
    break
 print(int(s))

import sys
test = int(input())
sol = []
for i in range(test):
 j = list(map(int, input().strip().split(" ")))
 n,d = j[0],j[1]
 digits = list(str(n))
 for i in range(len(digits)-1, -1, -1):
  first=0
  if(i<len(digits)-1):
   if (int(digits[i])>int(digits[i+1])):
    first=1
  if int(digits[i])>=d or first==1:
   digits.append(str(d))
   del(digits[i])
 str1 = "" 
 sol.append(str1.join(digits))
for i in range(test):
 print(int(sol[i]))

t = int(input())

while t:
 t=t-1
 n,d = list(map(int, input().split()))
 
 n = list(map(int,list(str(n))))
 n = n[::-1]

 
 new = []
 count = 0
 
 m = d 
 for i in range(len(n)):
  if(n[i] <= m):
   m = n[i]
   new.append(str(n[i]))
  else:
   count+=1
 
 new = new[::-1]
 
 for i in range(count):
  new.append(str(d))
  
 print("".join(new))
  
 
  
   


t=int(input())
for _ in range(t):
 n,k=list(map(int,input().split()))
 s=list(str(n))
 l=len(s)
 s1=""
 if(int(min(s))>k):
  for i in range(l):
   s1+=str(k)
  print(s1)
 else:
  c=min(s)
  while(int(c)!=k and len(s)!=0):
   s1+=c
   ind=s.index(c)
   s=s[ind+1:]
   #print(s)
   for i in range(ind+1):
    s.append(str(k))
    #print(s)
   c=min(s)
  z=len(s1)
  for i in range(l-z):
   s1+=str(k)
  print(s1)
   
    
   


# cook your dish here
t=int(input())
for _ in range(t):
 n,d=input().split(" ")
 q=int(d)
 k=[]
 for i in n:
  k.append(i)
   
 k2=k[::]

 min=int(d)
 for j in range(len(k2)-1,-1,-1):
  if int(k2[j])>min:
    k2.pop(j)
  else:
   min=int(k2[j])
 

 print(*k2,sep="",end="")
 print(d*(len(k)-len(k2)))

# cook your dish here
for i in range(int(input())):
 n,d=input().split()
 n=list(n)
 x=int(d)
 y=n.copy()
 for j in range(len(n)-1,-1,-1):
  if int(y[j])<=x:
   x=int(y[j])
  else:
   y.pop(j)
 l=len(y)
 for k in range(len(n)-l):
  y.append(d)
 print(''.join(y))

t = int(input())
for T in range(t):
 n, d = [x for x in input().split()]
 l = list(n)
 a = [l[0]]
 c = 0
 
 for i in range(1, len(l)):
  if (l[i] >= a[-1]):
   a.append(l[i])
  else:
   while(len(a) > 0 and a[-1] > l[i]):
    a = a[:len(a)-1]
   a.append(l[i])
   
 for i in a:
  if i <= d:
   print(i, end='')
   c += 1
  else:
   break
 print(d*(len(l)-c))


# cook your dish here
t=int(input())
while(t>0):
 num,d=input().split()
 a=list(map(int,num))
 mi=min(a)
 d=int(d)
 
 l=len(a)
 l1=l
 if mi < d:
  last=l - a[::-1].index(mi)-1
  c=0
  while(last < l):
   
   cmi=a.count(mi)
   for k in range(cmi):
    print(mi,end='')
    c=c+1
   if(last == l-1):
    break
   a =a[last+1 :]
   mi=min(a)
   if(mi >=d):
    break;
   l=len(a) 
   last=l - a[::-1].index(mi)-1
   
  while c< l1:
   print(d,end='')
   c=c+1
  
 else:
  for i in range(l1):
   print(d,end='')
 print() 
 t=t-1

t=int(input())
for _ in range(t):
 a,b=input().split()
 d={}
 for i in range(len(a)):
  try:
   d[a[i]]+=[i]
  except:
   d[a[i]]=[i]
 x=list(d.keys())
 x.sort()
 c=-1
 ans=""
 for i in x:
  if i>b:
   break
  for j in d[i]:
   if j>c:
    c=j
    ans+=i
    
 print(ans+b*(len(a)-len(ans)))
 


# cook your dish here
T= int(input())
for i in range(T):
 n,d=input().split()
 d=int(d)
 a=[]
 l=len(n)
 mn=d
 
 for j in range(l):
  a.append(int(n[j]))
  
  
 for k in range(l-1,-1,-1):
  if(a[k]>mn):
   a.pop(k)
   a.append(d)
  mn=min(mn,a[k])
  
  
 print(*a,sep="",end="\n")
 T=T-1 
  
 

t = int(input())

while t:
 n, d = [int(x) for x in input().split()]

 ans = 0
 if n == 1:
  ans = d if n > d else n
  print(ans)
 else:
  temp = str(n)
  l = len(temp)
  orig = temp[l - 1]
  if int(orig) > d:
   updat = str(d)
  else:
   updat = orig
  
  orig = updat

  for i in range(l-2, -1, -1):
   orig = temp[i] + updat
   updat = updat + str(d)
   if int(orig) > int(updat):
    orig = updat
   else:
    updat = orig
   
  print(int(orig))
 t -= 1

t = int(input())

while t:
 n, d = [int(x) for x in input().split()]

 ans = 0
 if n == 1:
  ans = d if n > d else n
  print(ans)
 else:
  temp = str(n)
  l = len(temp)
  orig = temp[l - 1]
  if int(orig) > d:
   updat = str(d)
  else:
   updat = orig
  
  orig = updat

  for i in range(l-2, -1, -1):
   orig = temp[i] + updat
   updat = updat + str(d)
   if int(orig) > int(updat):
    orig = updat
   else:
    updat = orig
   
  print(int(orig))
 t -= 1

for _ in range(int(input())):
 N,d=[int(x) for x in input().split()]
 S=str(N)
 New=str()
 List=[]
 List1=[]
 for i in S:
  List.append(int(i))
 for i in range(len(List)):
  if(List[i]==min(List[i:len(List)])):
   New+=str(List[i])
 for i in New:
  List1.append(int(i))
 for i in range(len(List1)-1,-1,-1):
  if(List1[i]>=d):
   List1[i]=d
  else:
   break
 BadNew=str()
 for i in List1:
  BadNew+=str(i)
 
 X=len(S)-len(BadNew)
 for i in range(X):
  BadNew+=str(d)
 print(BadNew)


T = int(input())
while T != 0:
 T = T-1
 N,d = map(int,input().split())
 digits = [int(i) for i in list(str(N))]
 change = True
 digits.append(d)
 while change != False:
  change = False
  for i in range(0,len(digits)-1):
   ##print(digits)
   if digits[i] > digits[i+1]:
    change = True
    digits.pop(i)
    digits.append(d)
    ##print(digits.pop(i))
    break
 digits.pop()
 ans = ''.join(map(str,digits))
 ans = int(ans)
 print(ans)

for x in range(int(input())):
 n,d=list(map(int,input().split()))
 l=list(str(n))
 l1=[]
 y=0
 t=0
 while(True):
  m=min(l)
  pos=l.index(m)
  if(int(m)>=d):
   break
  else:
   l1.append(m)
  if(pos+1>=len(l)):
   break
  for x in range(pos+1):
   l.remove(l[0])
 t1="".join(l1)
 t1=str(t1)+str(d)*(len(str(n))-len(str(t1)))
 print(t1)
   


for __ in range(int(input())):

 N, d = tuple(map(int, input().split()))

 current_num = list(map(int, list(str(N))))

 length = len(current_num)

 while True:
  for i in range(1, length + 1):

   if i == length:
    num = current_num[i - 1]
    if num > d:
     del current_num[i - 1]
     current_num.append(d)
     break
   else:
    num = current_num[i - 1]
    if num > current_num[i]:
     del current_num[i - 1]
     current_num.append(d)
     break
  else:
   break

 num = ''
 for digit in current_num:
  num += str(digit)

 print(num)

for __ in range(int(input())):

 N, d = tuple(map(int, input().split()))

 current_num = list(map(int, list(str(N))))

 length = len(current_num)

 while True:
  for i in range(1, length + 1):

   if i == length:
    num = current_num[i - 1]
    if num > d:
     del current_num[i - 1]
     current_num.append(d)
     break
   else:
    num = current_num[i - 1]
    if num > current_num[i]:
     del current_num[i - 1]
     current_num.append(d)
     break
  else:
   break

 num = ''
 for digit in current_num:
  num += str(digit)

 print(num)

t = int(input())
for T in range(t):
 n, d = [x for x in input().split()]
 l = list(n)
 a = [l[0]]
 c = 0
 
 for i in range(1, len(l)):
  if (l[i] >= a[-1]):
   a.append(l[i])
  else:
   while(len(a) > 0 and a[-1] > l[i]):
    a = a[:len(a)-1]
   a.append(l[i])
   
 for i in a:
  if i <= d:
   print(i, end='')
   c += 1
  else:
   break
 print(d*(len(l)-c))


# your code goes here
# your code goes here
for _ in range(int(input())):
 n,d=map(int,input().split())
 l=list(map(int,list(str(n))))
 if l==sorted(l):
  a=[x for x in l if x<=d]
  l=a+[d]*(len(l)-len(a))
 else:
  i=0
  while(True):
   if l[i]>l[i+1]:
    l.pop(i)
    l.append(d)
    i=0
   else:
    i+=1
   if l==sorted(l):
    break
 l=list(map(str,l)) 
 print(int("".join(l)))

def show(t):
 for _ in range(t):
  a,n = map(int,input().rstrip().split())
  res = []
  res1 = []
  a = str(a)
  for i in a:
   res.append(int(i))
  a = n
  for i in range(len(res)-1,-1,-1):
   if a>=res[i]:
    a = res[i]
    res1.append(a)
  for i in range(len(res1)-1,-1,-1):
   print(res1[i],end="")
  for i in range(len(res)-len(res1)):
   print(n,end="")
  print("")
show(int(input()))