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.DataInputStream;
  import java.io.IOException;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.TreeSet;
  
  public class Main {
  
      private static int n, k;
      private static List<Integer> dig;
      private static long ans;
  
      private static void run() throws IOException {
          n = in.nextInt();
          k = in.nextInt();
  
          dig = to_dig(n);
          ans = Integer.MAX_VALUE;
          dfs(0, 0, new int[k]);
          out.println(ans);
      }
  
      private static long build_result(int[] option) {
          TreeSet<Integer> set = new TreeSet<>();
          for (int now : option) {
              set.add(now);
          }
  
          int can_change_pos = -1;
          boolean bigger = false;
          int[] result = new int[dig.size()];
          for (int i = dig.size() - 1; i >= 0; i--) {
              if (bigger) {
                  result[i] = set.first();
              } else if (set.contains(dig.get(i))) {
                  result[i] = dig.get(i);
                  if (dig.get(i) != set.last()) {
                      can_change_pos = i;
                  }
              } else {
                  Integer value = set.higher(dig.get(i));
                  bigger = true;
                  if (value != null) {
                      result[i] = value;
                  } else if (can_change_pos == -1) {
                      return -1;
                  } else {
                      result[can_change_pos] = set.higher(result[can_change_pos]);
                      i = can_change_pos;
                  }
              }
          }
  
          long ans = 0;
          long level = 1;
          for (int i = 0; i < dig.size(); i++) {
              ans += level * result[i];
              level *= 10;
          }
          return ans;
      }
  
      private static void dfs(int pos, int startFrom, int[] option) {
          if (pos == k) {
              long now = build_result(option);
              if (now != -1) {
                  ans = Math.min(ans, now);
              }
              return;
          }
  
          for (int i = startFrom; i < 10 - (k - pos - 1); i++) {
              option[pos] = i;
              dfs(pos + 1, i + 1, option);
          }
      }
  
      private static List<Integer> to_dig(int n) {
          List<Integer> ans = new ArrayList<>();
          while (n != 0) {
              ans.add(n % 10);
              n /= 10;
          }
          return ans;
      }
  
      public static void main(String[] args) throws IOException {
          in = new Reader();
          out = new PrintWriter(new OutputStreamWriter(System.out));
  
          int t = in.nextInt();
          for (int i = 0; i < t; i++) {
              run();
          }
  
          out.flush();
          in.close();
          out.close();
      }
  
      private static int gcd(int a, int b) {
          if (a == 0 || b == 0)
              return 0;
          while (b != 0) {
              int tmp;
              tmp = a % b;
              a = b;
              b = tmp;
          }
          return a;
      }
  
      static final long mod = 1000000007;
  
      static long pow_mod(long a, long b) {
          long result = 1;
          while (b != 0) {
              if ((b & 1) != 0) result = (result * a) % mod;
              a = (a * a) % mod;
              b >>= 1;
          }
          return result;
      }
  
      private static long multiplied_mod(long... longs) {
          long ans = 1;
          for (long now : longs) {
              ans = (ans * now) % mod;
          }
          return ans;
      }
  
      @SuppressWarnings("FieldCanBeLocal")
      private static Reader in;
      private static PrintWriter out;
  
      private static int[] read_int_array(int len) throws IOException {
          int[] a = new int[len];
          for (int i = 0; i < len; i++) {
              a[i] = in.nextInt();
          }
          return a;
      }
  
      private static long[] read_long_array(int len) throws IOException {
          long[] a = new long[len];
          for (int i = 0; i < len; i++) {
              a[i] = in.nextLong();
          }
          return a;
      }
  
      private static void print_array(int[] array) {
          for (int now : array) {
              out.print(now);
              out.print(' ');
          }
          out.println();
      }
  
      private static void print_array(long[] array) {
          for (long now : array) {
              out.print(now);
              out.print(' ');
          }
          out.println();
      }
  
      static class Reader {
          private static final int BUFFER_SIZE = 1 << 16;
          private final DataInputStream din;
          private final byte[] buffer;
          private int bufferPointer, bytesRead;
  
          Reader() {
              din = new DataInputStream(System.in);
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
  
          public String readLine() throws IOException {
              final byte[] buf = new byte[1024]; // line length
              int cnt = 0, c;
              while ((c = read()) != -1) {
                  if (c == '\n') {
                      break;
                  }
                  buf[cnt++] = (byte) c;
              }
              return new String(buf, 0, cnt);
          }
  
          public int nextSign() throws IOException {
              byte c = read();
              while ('+' != c && '-' != c) {
                  c = read();
              }
              return '+' == c ? 0 : 1;
          }
  
          private static boolean isSpaceChar(int c) {
              return !(c >= 33 && c <= 126);
          }
  
          public int skip() throws IOException {
              int b;
              // noinspection ALL
              while ((b = read()) != -1 && isSpaceChar(b)) {
                  ;
              }
              return b;
          }
  
          public char nc() throws IOException {
              return (char) skip();
          }
  
          public String next() throws IOException {
              int b = skip();
              final StringBuilder sb = new StringBuilder();
              while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
                  sb.appendCodePoint(b);
                  b = read();
              }
              return sb.toString();
          }
  
          public int nextInt() throws IOException {
              int ret = 0;
              byte c = read();
              while (c <= ' ') {
                  c = read();
              }
              final boolean neg = c == '-';
              if (neg) {
                  c = read();
              }
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
  
              if (neg) {
                  return -ret;
              }
              return ret;
          }
  
          public long nextLong() throws IOException {
              long ret = 0;
              byte c = read();
              while (c <= ' ') {
                  c = read();
              }
              final boolean neg = c == '-';
              if (neg) {
                  c = read();
              }
              do {
                  ret = ret * 10 + c - '0';
              }
              while ((c = read()) >= '0' && c <= '9');
              if (neg) {
                  return -ret;
              }
              return ret;
          }
  
          public double nextDouble() throws IOException {
              double ret = 0, div = 1;
              byte c = read();
              while (c <= ' ') {
                  c = read();
              }
              final boolean neg = c == '-';
              if (neg) {
                  c = read();
              }
  
              do {
                  ret = ret * 10 + c - '0';
              }
              while ((c = read()) >= '0' && c <= '9');
  
              if (c == '.') {
                  while ((c = read()) >= '0' && c <= '9') {
                      ret += (c - '0') / (div *= 10);
                  }
              }
  
              if (neg) {
                  return -ret;
              }
              return ret;
          }
  
          private void fillBuffer() throws IOException {
              bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
              if (bytesRead == -1) {
                  buffer[0] = -1;
              }
          }
  
          private byte read() throws IOException {
              if (bufferPointer == bytesRead) {
                  fillBuffer();
              }
              return buffer[bufferPointer++];
          }
  
          public void close() throws IOException {
              din.close();
          }
  
      }
  }