description: It is a complicated version of problem F1. The difference between them is the constraints (F1: k ≤ 2, F2: k ≤ 10).
  
  You are given an integer n. Find the minimum integer x such that x ≥ n and the number x is k-beautiful.
  
  A number is called k-beautiful if its decimal representation having no leading zeroes contains no more than k different digits. E.g. if k = 2, the numbers 3434443, 55550, 777 and 21 are k-beautiful whereas the numbers 120, 445435 and 998244353 are not.
  
  Input
  
  The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases. Then t test cases follow.
  
  Each test case consists of one line containing two integers n and k (1 ≤ n ≤ 10^9, 1 ≤ k ≤ 10).
  
  Output
  
  For each test case output on a separate line x — the minimum k-beautiful integer such that x ≥ n.
  
  Example
  
  Input
  
  
  6
  2021 3
  177890 2
  34512 3
  724533 4
  998244353 1
  12345678 10
  
  
  Output
  
  
  2021
  181111
  34533
  724542
  999999999
  12345678
solution: #!/usr/bin/env python3
  # set vim: fdm=marker sw=4 ts=4 et
  
  from collections import defaultdict
  from collections import deque
  from sys import stdout
  
  def get_digits(n):
      digits = deque()
      while n > 0:
          digits.appendleft(n % 10)
          n //= 10
      return digits
  
  def get_tail(s, k, n):
      avail_digits = 10 - len(s)
  
  
  def get_number(digits, left_unchanged, k):
      s = set()
      for i in range(left_unchanged):
          s.add(digits[i])
  
      if len(digits) == left_unchanged:
          if len(s) <= k:
              return int(''.join([str(i) for i in digits]))
          else:
              return -1
  
      if len(s) > k:
          return -1
  
      for i in range(digits[left_unchanged] + 1, 10):
          remove = i not in s
          s.add(i)
  
          j = 0
          d = deque()
          while j < 10 and len(s) + len(d) < k:
              d.append(j)
  
          if len(s) + len(d) != k or left_unchanged + 1 + len(d) > len(digits):
              if remove:
                  s.remove(i)
              continue
  
          # min_s_d = min(list(s) + list(d))
          min_s_d = None
          for s_i in s:
              if min_s_d is None or min_s_d > s_i:
                  min_s_d = s_i
          for d_i in d:
              if min_s_d is None or min_s_d > d_i:
                  min_s_d = d_i
  
          while left_unchanged + 1 + len(d) < len(digits):
              d.appendleft(min_s_d)
  
          d2 = []
          for j in range(left_unchanged):
              d2.append(digits[j])
          d2.append(i)
          d2 += d
          return int(''.join([str(i) for i in d2]))
  
      return -1
  
  
  def get_min_number(digits_count, k):
      digits_count = max(digits_count, k)
      digits = deque(range(2, k))
      while len(digits) < digits_count - 1:
          digits.appendleft(0)
      digits.appendleft(1)
      return ''.join([str(i) for i in digits])
  
  
  for _ in range(int(input())):
      n, k = map(int, input().split())
      digits = get_digits(n)
  
      for i in range(len(digits), -1, -1):
          ans = get_number(digits, i, k)
          if ans >= n:
              break
  
      if ans < n:
          ans = get_min_number(len(digits) + 1, k)
      print(ans)
  