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: //package training;
  import java.util.*;
  public class Training {
      public static int[] numOfDigits(int n ){
          int [] a =new int [2];
          int c=0;
          int m =n;
          boolean f=true;
          while(m!=0){
              if(m/10==0)a[1]=m;
  //            if(m/100==0&&f){a[2]=m%10;f=false;}
              c++;
              m/=10;
          }
          a[0]=c;
          return a;
      }
      public static int gen (int n, int d ){
          int m=d;
          int v=n;
          while(v!=1){
              m*=10;
              m+=d;
              v--;
          }
          return m;
      }
      public static String gen2(int fir,int sec,int index, int index2,int len ){
          StringBuilder sb = new StringBuilder("");
          for (int i = 0; i < len; i++) {
              if(i<index)sb.append(intToChar(fir));
              else sb.append(intToChar(sec));
          }
          return sb.toString();
      }
      public static String gen3(String str,int fir,int sec,int index, int index2){
          StringBuilder sb = new StringBuilder("");
          for (int i = 0; i < str.length(); i++) {
              if(i<index)sb.append(intToChar(fir));
              else if(i>=index&&i<=index2)sb.append(intToChar(sec));
              else if(i==index2+1)sb.append(intToChar(fir));
              else sb.append(intToChar(sec));
          }
          return sb.toString();
      }
      public static String gen4(String str,int fir, int sec, int index, int index2){
          StringBuilder sb = new StringBuilder("");
          for (int i = 0; i < str.length(); i++) {
              if(i==index)sb.append(intToChar(sec+1));
              else sb.append(intToChar(fir));
          }
          return sb.toString();
      }
      public static String gen5(String str,int fir, int sec,int index, int index2){
          StringBuilder sb = new StringBuilder("");
          for (int i = 0; i < str.length(); i++) {
              if(i>=index&&i<=index2+1)sb.append(intToChar(sec));
              else sb.append(fir);
          }
          return sb.toString();
      }
      public static String gen6(String str,int fir, int sec,int index,int index2){
          StringBuilder sb = new StringBuilder("");
          for (int i = 0; i < str.length(); i++) {
              if(i>=index&&i<=index2)sb.append(intToChar(sec));
              else sb.append(fir);
          }
          return sb.toString();
      }
      public static String gen7(String str,int fir, int sec,int index){
          StringBuilder sb = new StringBuilder("");
          for (int i = 0; i < str.length(); i++) {
              if(i<=index)sb.append(intToChar(fir));
              else sb.append('0');
          }
          return sb.toString();
      }
      public static String gen8(String str, int fir, int sec, int index1, int index2, int ind){
          StringBuilder sb = new StringBuilder("");
          for (int i = 0; i < str.length(); i++) {
              if(i<index1)sb.append(intToChar(fir));
              else if(i>=index1&&i<=ind)sb.append(intToChar(sec));
              else if(i>ind&&i<=index2+1)sb.append(intToChar(fir));
              else sb.append(intToChar(sec));
          }
          return sb.toString();
      }
      public static String gen9(String str, int fir, int sec, int index1, int index2, int ind){
          StringBuilder sb = new StringBuilder("");
          for (int i = 0; i < str.length(); i++) {
              if(i<index1)sb.append(intToChar(fir));
              else if(i>=index1&&i<=ind)sb.append(intToChar(sec));
              else if(i>ind&&i<=index2)sb.append(intToChar(fir));
              else sb.append(intToChar(sec));
          }
          return sb.toString();
      }
      public static String gen10(String str, int fir, int sec, int index1, int index2, int ind){
          StringBuilder sb = new StringBuilder("");
          for (int i = 0; i < str.length(); i++) {
              if(i<index1)sb.append(intToChar(fir));
              else if(i>=index1&&i<=ind)sb.append(intToChar(sec));
              else if(i>ind&&i<=index2)sb.append(intToChar(fir));
              else sb.append(intToChar(fir));
          }
          return sb.toString();
      }
      public static int [] isThirdEqualFirst(String str, int fir , int sec, int index1, int index2){
          int [] a = new int [2];
          for (int i = index1; i <= index2; i++) {
              if(charToInt(str.charAt(i))!=sec){
                  a[0]=1;
                  a[1]=i-1;
                  return a;
              }
          }
          a[0]=a[1]=-1;
          return a;
      }
      public static int [] sec(String  str){
          char firts = str.charAt(0);
          int [] a = new int[2];
          for (int i = 1; i < str.length(); i++) {
              if(str.charAt(i)!=firts){
                  String s="";s+=str.charAt(i);
                  a[0]=Integer.parseInt(s);
                  a[1]=i;
                  return a;
              }
          }
          String s2="";s2+=firts;
          a[0]=Integer.parseInt(s2);
          a[1]=-1;
          return a;
      }
      public static int [] third(String str){
          int [] a = new int [2];
          char fir = str.charAt(0);
          char sec = intToChar(sec(str)[0]);
          for (int i = 0; i < str.length(); i++) {
              if(str.charAt(i)!=fir&&str.charAt(i)!=sec){
                  a[0]=charToInt(str.charAt(i));
                  a[1]=i-1;
                  return a;
              }
          }
          a[0]=a[1]=-1;
          return a;
      }
      public static char intToChar(int n){
          if(n==0)return '0';
          if(n==1)return '1';
          if(n==2)return '2';
          if(n==3)return '3';
          if(n==4)return '4';
          if(n==5)return '5';
          if(n==6)return '6';
          if(n==7)return '7';
          if(n==8)return '8';
          else return '9';     
      }
      public static int charToInt(char ch){
          if(ch=='0')return 0;
          if(ch=='1')return 1;
          if(ch=='2')return 2;
          if(ch=='3')return 3;
          if(ch=='4')return 4;
          if(ch=='5')return 5;
          if(ch=='6')return 6;
          if(ch=='7')return 7;
          if(ch=='8')return 8;
          else return 9;
      }
      public static boolean isOk1(String s){
          char a = s.charAt(0);
          for (int i = 0; i < s.length(); i++) {
              if(s.charAt(i)!=a)return false;
          }
          return true;
      }
      public static boolean isOk2(String s){
          char a = s.charAt(0);
          char b = intToChar(sec(s)[0]);
          for (int i = 0; i < s.length(); i++) {
              if(s.charAt(i)!=a&&s.charAt(i)!=b)return false;
          }
          return true;
      }
      public static char rais(char ch){
          if(ch=='0')return '1';
          if(ch=='1')return '2';
          if(ch=='2')return '3';
          if(ch=='3')return '4';
          if(ch=='4')return '5';
          if(ch=='5')return '6';
          if(ch=='6')return '7';
          if(ch=='7')return '8';
          if(ch=='8')return '9';
          else return '0';
      }
      public static int rais(int ch){
          if(ch==0)return 1;
          if(ch==1)return 2;
          if(ch==2)return 3;
          if(ch==3)return 4;
          if(ch==4)return 5;
          if(ch==5)return 6;
          if(ch==6)return 7;
          if(ch==7)return 8;
          if(ch==8)return 9;
          else return 0;
      }
      public static void main(String args[]) { 
          Scanner in = new Scanner (System.in);
          int t = in.nextInt();
          for (int i = 0; i < t; i++) {
              int n = in.nextInt();
              int k = in.nextInt();
              char [] chars = String.valueOf(n).toCharArray();
              while (true){
                  Set<Character> s = new HashSet<>();
                  for (char c : chars) {
                      s.add(c);
                  }
                  if (s.size() <= k) {
                      break;
                  }
                  s.clear();
   
                  int ptr = 0;
                  for (; ; ptr++) {
                      s.add(chars[ptr]);
                      if (s.size() > k) {
                          while (chars[ptr] == '9') {
                              ptr--;
                          }
                          chars[ptr]++;
                          for (int j = ptr + 1; j < chars.length; j++) {
                              chars[j] = '0';
                          }
                          break;
                      }
                  }
              }
              System.out.println(new String(chars));
          }
      }
  }
  