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: import java.util.*;
  
  // 1900
  public class NearestBeautifulNumberEasy {
  
      static ArrayList<Character> getDigits(int x) {
          var xs = (""+x).toCharArray();
          var digits = new ArrayList<Character>();
          for (int i = 0; i < xs.length; i++){
              var c = xs[i];
              if (!digits.contains(c)) {
                  digits.add(c);
              }
          }
          Collections.sort(digits);
          return digits;
      }
  
      static int charArrayToInt(char[] xs) {
          int result = 0;
          for (int i = 0; i < xs.length; i++) {
              int digit = (int)xs[i] - (int)'0';
              result *= 10;
              result += digit;
          }
          return result;
      }
  
      static int getPrefix(char[] xs, int k) {
          var digits = new ArrayList<Character>();
          var sizeCount = 0;
          int result = 0;
          for (int i = 0; i < xs.length; i++){
              var c = xs[i];
              int digit = (int)c - (int)'0';
              result *= 10;
              result += digit;
              if (!digits.contains(c)) {
                  digits.add(c);
                  sizeCount++;
              }
              if (sizeCount == k) {
                  return result;
              }
          }
          return result;
      }
  
      static char[] makePaddedCharArray(int x, int size) {
          if (x <= 0) {
              var ys = new char[size];
              for (int i = 0; i < size; i++) {
                  ys[i] = '0';
              }
              return ys;
          }
  
          var zs = (""+x).toCharArray();
          var ys = new char[size];
          for (int i = 0; i < size - zs.length; i++) {
              ys[i] = '0';
          }
          for (int i = size - zs.length; i < size; i++) {
              ys[i] = zs [zs.length - size + i];
          }
          return ys;
      }
  
      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
  
          int t = sc.nextInt();
          for (int tc = 0; tc < t; tc++) {
              int n = sc.nextInt();
              int k = sc.nextInt();
              var xs = (""+n).toCharArray();
              
              int result = 0;
  
              int prefix = getPrefix(xs, k);
              int prefixSize = (""+prefix).length();
  
              var prefixDigits = getDigits(prefix);
              var resultDigits = getDigits(n);
              
              int remSize = xs.length - prefixSize;
              int paddedPrefix = prefix;
              for (int i = 0; i < remSize; i++) {
                  paddedPrefix *= 10;
              }
              int rem = n - paddedPrefix; 
              var ys = makePaddedCharArray(rem, remSize);
  
              boolean trivial = resultDigits.size() <= k;
              if (trivial) {
                  result = n;
              } else {
                  while (result < n || resultDigits.size() > k) {
                      // System.out.println("Prefix: " + prefix);
                      // System.out.println("Padded Prefix: " + paddedPrefix);
                      // System.out.println("Prefix Digits: " + prefixDigits);
                      if (!(resultDigits.size() <= k)) {
                          result = paddedPrefix;
                          boolean failed = false;
                          int minDigit = Collections.min(prefixDigits) - (int)'0';
                          int maxDigitChar = Collections.max(prefixDigits);
                          
                          boolean smallestWillSuffice = (paddedPrefix >= n);
                          for (int i = 0; i < remSize; i++) {
                              // System.out.println("Result is currently " + result);
                              if (smallestWillSuffice) {
                                  result += (minDigit * Math.pow(10, remSize - 1 - i));
                              } else {
                                  boolean foundSufficientDigit = false;
                                  for (char c : prefixDigits) {
                                      // System.out.println("Considering " + c);
                                      boolean digitWillSuffice = (c >= ys[i]);
                                      if (c > ys[i]) {
                                          smallestWillSuffice = true;
                                      } else if (c == ys[i]) {
                                          for (int j = i+1; j < remSize; j++) {
                                              if (maxDigitChar > ys[j]) {
                                                  break;
                                              }
                                              if (maxDigitChar < ys[j]) {
                                                  digitWillSuffice = false;
                                                  if (maxDigitChar == 9) return;
                                              }
                                          } 
                                      }
                                      if (digitWillSuffice) {
                                          int digit = (int)c - (int)'0';
                                          result += (digit * Math.pow(10, remSize - 1 - i));
                                          foundSufficientDigit = true;
                                          break;
                                      }
                                  }
                                  if (!foundSufficientDigit) {
                                      failed = true;
                                      break;
                                  }
                              }
                          }
      
                          if (failed) {
                              prefix++;
                              paddedPrefix += Math.pow(10, remSize);
                              result = paddedPrefix;
                              rem = n - paddedPrefix;
                              ys = makePaddedCharArray(rem, remSize);
                              prefixDigits = getDigits(prefix);
                              resultDigits = getDigits(paddedPrefix);
                              prefixSize = (""+prefix).length();
                              remSize = xs.length - prefixSize;
                          } else {
                              resultDigits = getDigits(result);
                          }
                      }
                  }
              }
  
              
              System.out.println(result);
          }
  
          sc.close();
      }
  }