description: It is a simplified version of problem F2. 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 ≤ 2).
  
  Output
  
  For each test case output on a separate line x — the minimum k-beautiful integer such that x ≥ n.
  
  Example
  
  Input
  
  
  4
  1 1
  221 2
  177890 2
  998244353 1
  
  
  Output
  
  
  1
  221
  181111
  999999999
solution: // Don't place your source in a package
  
  
  import javax.swing.*;
  import java.lang.reflect.Array;
  import java.text.DecimalFormat;
  import java.util.*;
  import java.lang.*;
  import java.io.*;
  import java.math.*;
  import java.util.stream.Stream;
  
  
  
  // Please name your class Main
  public class Main {
      static FastScanner fs=new FastScanner();
      static class FastScanner {
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          StringTokenizer st=new StringTokenizer("");
          public String next() {
              while (!st.hasMoreElements())
                  try {
                      st=new StringTokenizer(br.readLine());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              return st.nextToken();
          }
          int Int() {
              return Integer.parseInt(next());
          }
  
          long Long() {
              return Long.parseLong(next());
          }
  
          String Str(){
              return next();
          }
      }
  
  
      public static void main (String[] args) throws java.lang.Exception {
          PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
          //reading /writing file
          //Scanner sc=new Scanner(new File("input.txt"));
          //PrintWriter pr=new PrintWriter("output.txt");
  
  
  
          int T=Int();
          for(int t=0;t<T;t++){
              int n=Int();
              int k=Int();
  
              Solution sol1=new Solution(out);
              sol1.solution(n,k);
          }
          out.close();
  
      }
  
  
      public static int[] Arr(int n){
          int A[]=new int[n];
          for(int i=0;i<n;i++)A[i]=Int();
          return A;
      }
      public static int Int(){
          return fs.Int();
      }
      public static long Long(){
          return fs.Long();
      }
      public static String Str(){
          return fs.Str();
      }
  
  }
  
  
  
  
  
  
  
  
  class Solution {
      PrintWriter out;
      int INF = Integer.MAX_VALUE;
      int MOD = 1000000007;
      long mod = 1000000007;
  
      public Solution(PrintWriter out) {
          this.out = out;
      }
  
  
      public void solution(int n,int k) {
          long res=Long.MAX_VALUE;
          String s= n+"";
          char A[]=s.toCharArray();
  
          Set<Character>set=new HashSet<>();
          for(char c:A)set.add(c);
          if(set.size()<=k){
              out.println(n);
              return;
          }
  
          int bit= 0 ;
          long cur = 0;
          for(int i=0;i<A.length;i++){
              int d = A[i]-'0';
              int cnt = Integer.bitCount(bit);
              if(cnt>k)break;
  
              for(int j = d+1;j<=9;j++){
                  int newb=bit|(1<<j);
                  if(Integer.bitCount(newb)<=k){
                      if(Integer.bitCount(newb)<k){
                          long temp=cur*10+j;
                          for(int x=i+1;x<A.length;x++){
                              temp=temp*10+0;
                          }
                          res=Math.min(res,temp);
                      }
                      else{
  
                          int mn = 100;
                          for(int x=0;x<10;x++){
                              if((newb&(1<<x))!=0){
                                  mn=x;
                                  break;
                              }
                          }
  
                          long temp=cur*10+j;
                          for(int x=i+1;x<A.length;x++){
                              temp=temp*10+mn;
                          }
                          res=Math.min(res,temp);
                      }
                  }
              }
  
              bit=bit|(1<<d);
              cur=cur*10+d;
          }
  
  
          out.println(res);
      }
  }
  
  
  