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.net.Inet4Address;
  import java.util.*;
  import java.io.*;
  
  public class Div739 {
  
  
      static int k;
  
      static long solve(int msk, long val, int size) {
          int min = 0;
          for (int i = 0; i < 10; i++) {
              if ((msk & (1 << i)) != 0) {
                  min = i;
                  break;
              }
          }
          if (Integer.bitCount(msk) < k) {
              min = 0;
          }
          int need = size - ("" + val).length();
          long ret = val;
          while (need > 0) {
              ret *= 10;
              ret += min;
              need--;
          }
          return ret;
      }
  
      public static void main(String[] args) throws IOException {
  
          Scanner sc = new Scanner(System.in);
          PrintWriter pw = new PrintWriter(System.out);
          int t = sc.nextInt();
  
          while (t-- > 0) {
              int n = sc.nextInt();
              char[] s = ("" + n).toCharArray();
              k = sc.nextInt();
  
              int msk = 0;
              long cur = 0;
              long ans = (long) 1e16;
              for (int i = 0; i < s.length; i++) {
                  for (int change = s[i] - '0' + 1; change < 10; change++) {
                      if (Integer.bitCount(msk | (1 << change)) <= k) {
                          ans = Math.min(ans, solve(msk | (1 << change), cur * 10 + change, s.length));
                      }
                  }
                  cur *= 10;
                  cur += (s[i] - '0');
                  msk |= (1 << (s[i] - '0'));
              }
  
              if (Integer.bitCount(msk) <= k) {
                  pw.println(n);
              } else pw.println(ans);
          }
          pw.flush();
      }
  
  
      static class Scanner {
          StringTokenizer st;
          BufferedReader br;
  
          public Scanner(FileReader r) {
              br = new BufferedReader(r);
          }
  
          public Scanner(InputStream s) {
              br = new BufferedReader(new InputStreamReader(s));
          }
  
          public String next() throws IOException {
              while (st == null || !st.hasMoreTokens())
                  st = new StringTokenizer(br.readLine());
              return st.nextToken();
          }
  
          public int[] nextIntArr(int n) throws IOException {
              int[] a = new int[n];
              for (int i = 0; i < n; i++) {
                  a[i] = nextInt();
              }
              return a;
          }
  
          public int nextInt() throws IOException {
              return Integer.parseInt(next());
          }
  
          public long nextLong() throws IOException {
              return Long.parseLong(next());
          }
  
          public String nextLine() throws IOException {
              return br.readLine();
          }
  
          public double nextDouble() throws IOException {
              String x = next();
              StringBuilder sb = new StringBuilder("0");
              double res = 0, f = 1;
              boolean dec = false, neg = false;
              int start = 0;
              if (x.charAt(0) == '-') {
                  neg = true;
                  start++;
              }
              for (int i = start; i < x.length(); i++)
                  if (x.charAt(i) == '.') {
                      res = Long.parseLong(sb.toString());
                      sb = new StringBuilder("0");
                      dec = true;
                  } else {
                      sb.append(x.charAt(i));
                      if (dec)
                          f *= 10;
                  }
              res += Long.parseLong(sb.toString()) / f;
              return res * (neg ? -1 : 1);
          }
  
          public boolean ready() throws IOException {
              return br.ready();
          }
      }
  
  }