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: /*
  stream Butter!
  eggyHide eggyVengeance
  I need U
  xiao rerun when
   */
  import static java.lang.Math.*;
  import java.util.*;
  import java.io.*;
  import java.math.*;
  
  public class x1560F2
  {
      static final int INF = Integer.MAX_VALUE-420;
      public static void main(String hi[]) throws Exception
      {
          BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
          StringTokenizer st = new StringTokenizer(infile.readLine());
          int T = Integer.parseInt(st.nextToken());
          StringBuilder sb = new StringBuilder();
          while(T-->0)
          {
              st = new StringTokenizer(infile.readLine());
              int N = Integer.parseInt(st.nextToken());
              int K = Integer.parseInt(st.nextToken());
              long res = Long.MAX_VALUE;
              for(int d=1; d <= 9; d++)
              {
                  long temp = d;
                  while(temp < N)
                  {
                      temp *= 10;
                      temp += d;
                  }
                  res = min(res, temp);
              }
              if(K == 1)
              {
                  sb.append(res+"\n");
                  continue;
              }
              char[] arr = (N+"").toCharArray();
              int M = arr.length;
              int mask = 0;
              //increase a digit
              for(int add=1; add < 10; add++)
                  for(int head=0; head < M; head++)
                      if((char)(arr[head]+add) <= '9')
                      {
                          mask = 0;
                          for(int i=0; i < head; i++)
                              mask |= (1<<(arr[i]-'0'));
                          char[] temp = new char[M];
                          for(int i=0; i < head; i++)
                              temp[i] = arr[i];
                          temp[head] = (char)(arr[head]+add);
                          mask |= (1<<(temp[head]-'0'));
                          if(Integer.bitCount(mask) > K)
                              continue;
                          int smallest = -1;
                          if(Integer.bitCount(mask) < K)
                              smallest = 0;
                          else
                          {
                              for(int b=0; b < 10; b++)
                                  if((mask&(1<<b)) > 0)
                                  {
                                      smallest = b;
                                      break;
                                  }
                          }
                          for(int i=head+1; i < M; i++)
                              temp[i] = (char)(smallest+'0');
                          long lmao = 0L;
                          for(char c: temp)
                          {
                              lmao *= 10;
                              lmao += c-'0';
                          }
                          res = min(res, lmao);
                      }
              int overall = 0;
              for(char c: arr)
                  overall |= (1<<(c-'0'));
              if(Integer.bitCount(overall) <= K)
                  res = N;
              sb.append(res+"\n");
          }
          System.out.print(sb);
      }
      public static int[] readArr(int N, BufferedReader infile, StringTokenizer st) throws Exception
      {
          int[] arr = new int[N];
          st = new StringTokenizer(infile.readLine());
          for(int i=0; i < N; i++)
              arr[i] = Integer.parseInt(st.nextToken());
          return arr;
      }
  }
  /*
  1
  9340400 2
  
   */