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.io.PrintWriter;
  import java.util.*;
  
  public class Main {
      public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          PrintWriter out = new PrintWriter(System.out);
          int t = in.nextInt();
          while (t > 0) {
              int n = in.nextInt();
              int k = in.nextInt();
              List<Integer> l = digs(n);
              long ans = Long.MAX_VALUE;
  
              int len = l.size();
              for (int i = 1; i <= 9; i++) {
                  long x = get(len, i);
                  if (x >= n) {
  //                        out.println(x);
                      ans = x;
  //                        len = -1;
                      break;
                  }
              }
              for (int i = 1; i <= 9; i++) {
                  long x = get(len + 1, i);
                  if (x >= n) {
                      ans = Math.min(ans, x);
                      break;
                  }
              }
              if (k == 2) {
                  long ans1 = 1;
                  for (int i = 0; i < l.size(); i++) {
                      ans1 *= 10L;
                  }
                  ans = Math.min(ans, ans1);
                  for (int i = 0; i < 10; i++) {
                      for (int j = i + 1; j < 10; j++) {
                          ans = Math.min(ans, smallest(n, l, i, j, 0));
                      }
                  }
              }
              out.println(ans);
              t--;
          }
          out.close();
      }
  
      static long smallest(int n, List<Integer> l, int a, int b, int i) {
          int mn = Math.min(a, b);
          int mx = Math.max(a, b);
          long biggest = get(l.size() - 1, mx) * 10L + mn;
          long smallest = get(l.size() - 1, mn) * 10L + mx;
          if (smallest >= n) {
              return smallest;
          }
          if (n <= biggest) {
              long res = 0;
              boolean mxused = false;
              boolean mnused = false;
              boolean big = false;
              for (int j = 0; j < l.size(); j++) {
                  if (j == l.size() - 1) {
                      if (!mxused) {
                          res *= 10L;
                          res += mx;
                          continue;
                      } else if (!mnused) {
                          res *= 10L;
                          res += mn;
                          continue;
                      }
                  }
                  if (big) {
                      res *= 10L;
                      res += mn;
                      mnused = true;
                      continue;
                  }
                  if (l.get(j) < mn) {
                      big = true;
                      res *= 10L;
                      res += mn;
                      mnused = true;
                      continue;
                  }
                  if (l.get(j) == mn) {
                      res *= 10L;
                      res += mn;
                      mnused = true;
                      continue;
                  }
                  if (l.get(j) < mx) {
                      big = true;
                      res *= 10L;
                      res += mx;
                      mxused = true;
                      continue;
                  }
                  if (l.get(j) == mx) {
                      res *= 10L;
                      res += mx;
                      mxused = true;
                      continue;
                  }
                  big = true;
                  int c = 0;
                  while (res % 10L == mx) {
                      res /= 10L;
                      c++;
                  }
                  res /= 10L;
                  res *= 10L;
                  res += mx;
                  for (int k = 0; k < c + 1; k++) {
                      res *= 10L;
                      res += mn;
                  }
                  mnused = true;
                  mxused = true;
              }
              if (res >= n) {
                  return res;
              } else {
                  return Long.MAX_VALUE;
              }
          } else {
              return Long.MAX_VALUE;
          }
      }
  
      static long get(int len, int d) {
          long res = d;
          for (int i = 0; i < len - 1; i++) {
              res *= 10L;
              res += d;
          }
          return res;
      }
  
      static int include(List<Integer> p, List<Integer> q) {
          int cnt = 0;
          int it = 0;
          for (int i = 0; i < q.size(); i++) {
              while (it < p.size() && !p.get(it).equals(q.get(i))) {
                  it++;
              }
              if (it < p.size()) {
                  cnt++;
              } else {
                  break;
              }
              it++;
          }
          return cnt;
      }
  
      static List<Integer> digs(long a) {
          List<Integer> res = new ArrayList<>();
          while (a != 0) {
              res.add((int) (a % 10L));
              a /= 10L;
          }
          Collections.reverse(res);
          return res;
      }
  }