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.util.*;
  import java.io.*;
  public class Sol{
   
  /*   
  ->check n=1, int overflow , array bounds , all possibilites(dont stuck on 1 approach)
  ->Problem = Observation(constraints(m<=n/3 or k<=min(100,n)) 
             + Thinking + Technique (seg_tree,binary lift,rmq,bipart,dp,connected comp etc)
  ->solve or leave it (- tutorial improves you in minimal way -)
  */
   
  public static void main (String []args) {
  //precomp();
   
  int times=ni();while(times-->0){solve();}out.close();}
   
  static void solve() {
   int n=ni();int k=ni();
  
   int len=length(n);
   int a[]=new int[len];
   int cn=n;
  
   for(int i=len-1;i>=0;i--){a[i]=cn%10;cn/=10;}
   //edge case 
   
   if(valid(n,k)){out.println(n);return;}
  
   for(int i=len-1;i>=0;i--){
      int ans=get(a,i,k,n);
      if(ans==0){}
      else{out.println(ans);return;}
   }
   
   return;}
  static boolean valid (int n,int k){
     HashSet<Integer> hs=new HashSet<>();while(n>0){hs.add(n%10);n/=10;}return hs.size()<=k;
   }
  static int length(int n){int c=0;while(n>0){n/=10;c++;}return c;} 
  
  //1 5 7 8 9 k=3 => 1 5 7 1 1 => 1 5 8 1 1
  //1 5 7 2 2 k=3 > 1 5 7 5 1
  
  static int get(int a[],int p,int k,int n){
     
      int len=a.length; 
      a[p]++;
     
      while(a[p]<=9){
        HashSet<Integer> hs=new HashSet<>();int input=0;int min=10;
        for(int i=0;i<=p;i++){input=10*input+a[i];min=Math.min(a[i],min);hs.add(a[i]);}
       
        int size=hs.size();
        int rem=len-p-1;
        
        if( k >= size ){ 
           
           if(!hs.contains(0)&&k>=size+1){min=0;}
           while(rem>0){input=10*input+min;rem--;}
           return input;
        }
        else{}
  
      a[p]++;}
   
      return 0;
   }
  //-----------------Utility--------------------------------------------
   
  static class Pair{int id,last;Pair(int id,int last){this.id=id;this.last=last;}}
   
  static long gcd(long a,long b){if(b==0)return a; return gcd(b,a%b);}
   
  static int Max=Integer.MAX_VALUE; static long mod=1000000007;
   
  //static int v(char c){return (int)(c-'a');}
   
  public static long power(long x, long y )
      {
          //0^0 = 1
          long res = 1L;
          x = x%mod;
          while(y > 0)
          {
              if((y&1)==1)
                  res = (res*x)%mod;
              y >>= 1;
              x = (x*x)%mod;
          }
          return res;
      }
   
  /*static class Pair implements Comparable<Pair>{
          int id;int value;Pair next;
          public Pair(int id,int value) {
           
            this.id=id;this.value=value;next=null;
          }
          @Override
          public int compareTo(Pair p){return Long.compare(value,p.value);}
      }
  */
   
  //----------------------I/O---------------------------------------------
   
  static InputStream inputStream = System.in;
  static OutputStream outputStream = System.out;
  static FastReader in=new FastReader(inputStream);
  static PrintWriter out=new PrintWriter(outputStream);
   
  static class FastReader 
  { 
    BufferedReader br; 
    StringTokenizer st; 
   
          FastReader(InputStream is) {
              br = new BufferedReader(new InputStreamReader(is));
          }
   
    public String next() 
    { 
        while (st == null || !st.hasMoreElements()) 
        { 
            try
            { 
                st = new StringTokenizer(br.readLine()); 
            } 
            catch (IOException  e) 
            { 
                e.printStackTrace(); 
            } 
        } 
        return st.nextToken(); 
    } 
   
    public int nextInt() 
    { 
        return Integer.parseInt(next()); 
    } 
   
    public long nextLong() 
    { 
        return Long.parseLong(next()); 
    } 
   
   public  double nextDouble() 
    { 
        return Double.parseDouble(next()); 
    } 
   
   
    String nextLine() 
    { 
        String str = ""; 
        try
        { 
            str = br.readLine(); 
        } 
        catch (IOException e) 
        { 
            e.printStackTrace(); 
        } 
        return str; 
    } 
  }
  /*static int ni() {
          try {
              boolean in = false;
              int res = 0;
              for (;;) {
                  int b = System.in.read() - '0';
                  if (b >= 0) {
                      in = true;
                      res = 10 * res + b;
                  }
                  else if (in)
                      return res;
              }
          } catch (IOException e) {
              throw new Error(e);
          }
      }*/
  static int ni(){return in.nextInt();}
  static long nl(){return in.nextLong();}
  static double nd(){return in.nextDouble();}
  static String ns(){return in.nextLine();}
  }