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.beans.VetoableChangeListener;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.List;
  import java.util.Map;
  import java.util.StringTokenizer;
  import java.util.TreeSet;
  
  import javafx.util.Pair;
  
  import static java.util.Arrays.binarySearch;
  import static java.util.Arrays.copyOfRange;
  import static java.util.Arrays.fill;
  
  public class Main {
  
      public static void main(String[] args) throws Exception {
          InputStream inputStream = System.in;
          OutputStream outputStream = System.out;
  
          InputReader in = new InputReader(inputStream);
          PrintWriter out = new PrintWriter(outputStream);
  
          Task solver = new Task();
          solver.solve(1, in, out);
          out.close();
      }
  }
  
  class Task {
  
      long ans = Long.MAX_VALUE;
  
      public void solve(int testNumber, InputReader in, PrintWriter out) {
          int T = in.nextInt();
          for (int tc = 1; tc <= T; tc++) {
              ans = Long.MAX_VALUE;
              String s = in.next();
              int k = in.nextInt();
              cal(s, k, 0, 0, 0, false);
              out.println(ans);
          }
      }
  
      private void cal(String s, int K, int st, long n, int c, boolean isGreater) {
          if (Integer.bitCount(st) > K) {
              return;
          }
          if (c == s.length()) {
              ans = Math.min(ans, n);
              return;
          }
          if (isGreater) {
              long nn = n;
              int mn = 0;
              if (Integer.bitCount(st) == K) {
                  mn = Integer.numberOfTrailingZeros(st);
              }
              for (int i = c; i < s.length(); i++) {
                  nn = nn * 10 + mn;
              }
              ans = Math.min(ans, nn);
              return;
          }
          int nn = s.charAt(c) - '0';
          for (int i = nn; i <= 9; i++) {
              cal(s, K, st | (1 << i), n * 10 + i, c + 1, i > nn ? true : isGreater);
          }
      }
  }
  
  class InputReader {
  
      private final BufferedReader reader;
      private StringTokenizer tokenizer;
  
      public InputReader(InputStream stream) {
          reader = new BufferedReader(new InputStreamReader(stream));
          tokenizer = null;
      }
  
      public String nextLine() {
          try {
              return reader.readLine();
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }
  
      public String next() {
          while (tokenizer == null || !tokenizer.hasMoreTokens()) {
              tokenizer = new StringTokenizer(nextLine());
          }
          return tokenizer.nextToken();
      }
  
      public int nextInt() {
          return Integer.parseInt(next());
      }
  
      public long nextLong() {
          return Long.parseLong(next());
      }
  
      public double nextDouble() {
          return Double.parseDouble(next());
      }
  
  }