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: import java.io.OutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintWriter;
  import java.util.StringTokenizer;
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.io.InputStream;
  
  /**
   * Built using CHelper plug-in
   * Actual solution is at the top
   */
  public class Main {
      public static void main(String[] args) {
          InputStream inputStream = System.in;
          OutputStream outputStream = System.out;
          Scanner in = new Scanner(inputStream);
          PrintWriter out = new PrintWriter(outputStream);
          F2NearestBeautifulNumberHardVersion solver = new F2NearestBeautifulNumberHardVersion();
          solver.solve(1, in, out);
          out.close();
      }
  
      static class F2NearestBeautifulNumberHardVersion {
          String s;
          int k;
          boolean[][][][] memo;
          StringBuilder sb;
  
          public void readInput(Scanner sc) {
              s = sc.next();
              k = sc.nextInt();
          }
  
          public void solve(int testNumber, Scanner sc, PrintWriter pw) {
              int tc = sc.nextInt();
              while (tc-- > 0) {
                  readInput(sc);
                  s = '0' + s;
                  sb = new StringBuilder();
                  memo = new boolean[2][2][s.length()][1 << 10];
                  trace(0, 0, 0);
                  pw.println(sb);
              }
          }
  
          private void trace(int idx, int bool, int msk) {
              if (idx == s.length())
                  return;
              int start = bool == 1 ? 0 : s.charAt(idx) - '0';
              for (int i = start; i <= 9; i++) {
                  if (dp(idx + 1, i > s.charAt(idx) - '0' ? 1 : bool, i == 0 && Integer.bitCount(msk) == 0 ? msk : msk | 1 << i)) {
                      if (i == 0) {
                          if (sb.length() > 0)
                              sb.append(0);
                      } else
                          sb.append(i);
                      trace(idx + 1, i > s.charAt(idx) - '0' ? 1 : bool, i == 0 && Integer.bitCount(msk) == 0 ? msk : msk | 1 << i);
                      return;
                  }
              }
          }
  
          private boolean dp(int idx, int bool, int msk) {
              if (Integer.bitCount(msk) > k)
                  return false;
              if (idx == s.length())
                  return true;
              if (memo[0][bool][idx][msk])
                  return memo[1][bool][idx][msk];
              memo[0][bool][idx][msk] = true;
              int start = bool == 1 ? 0 : s.charAt(idx) - '0';
              for (int i = start; i <= 9; i++) {
                  if (dp(idx + 1, i > s.charAt(idx) - '0' ? 1 : bool, i == 0 && Integer.bitCount(msk) == 0 ? msk : msk | 1 << i))
                      return memo[1][bool][idx][msk] = true;
              }
              return memo[1][bool][idx][msk] = false;
          }
  
      }
  
      static class Scanner {
          StringTokenizer st;
          BufferedReader br;
  
          public Scanner(InputStream s) {
              br = new BufferedReader(new InputStreamReader(s));
          }
  
          public String next() {
              try {
                  while (st == null || !st.hasMoreTokens())
                      st = new StringTokenizer(br.readLine());
                  return st.nextToken();
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
          }
  
          public int nextInt() {
              return Integer.parseInt(next());
          }
  
      }
  }
  
  