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: //Created by Aminul on 18/8/21.
  
  import java.io.*;
  import java.util.*;
  
  import static java.lang.Math.*;
  
  public class F2 {
      static long pow[];
      static int[] arr;
      static long[][][][][] dp = new long[11][2][2][11][1025];
      static int[][][][][] vis = new int[11][2][2][11][1025];
      static int tc;
  
      public static void main(String[] args) throws Exception {
          InputReader in = new InputReader(System.in);
          PrintWriter pw = new PrintWriter(System.out);
          pow = new long[12];
          pow[0] = 1;
          for (int i = 1; i < pow.length; i++) {
              pow[i] = pow[i - 1] * 10;
          }
  
          int test = in.nextInt();
          for (int t = 1; t <= test; t++) {
              tc = t;
              int n = in.nextInt();
              int k = in.nextInt();
              String s = String.valueOf(n);
              arr = new int[11];
              for (int i = 0, j = s.length() - 1; j >= 0; j--, i++) {
                  arr[i] = s.charAt(j) - '0';
              }
  
              long res = solve(10, 0, 0, k, 0);
              pw.println(res);
          }
  
          pw.close();
      }
  
      static long solve(int i, int isLarge, int isNonZero, int rem, int mask) {
          if (i < 0) {
              return 0;
          }
          if (isLarge == 1) {
              if (vis[i][isLarge][isNonZero][rem][mask] != 0) {
                  return dp[i][isLarge][isNonZero][rem][mask];
              }
          }
  
          if (vis[i][isLarge][isNonZero][rem][mask] == tc) {
              return dp[i][isLarge][isNonZero][rem][mask];
          }
  
          vis[i][isLarge][isNonZero][rem][mask] = tc;
  
          long res = Integer.MAX_VALUE;
          int x = arr[i];
          for (int d = 0; d < 10; d++) {
              if (isLarge == 0 && d < x) {
                  continue;
              }
              if (rem == 0 && !check(mask, d)) {
                  continue;
              }
              int nextIsLarge = isLarge | ((d > x) ? 1 : 0);
              int nextNonZero = isNonZero | ((d != 0) ? 1 : 0);
              int nextMask = nextNonZero == 0 ? mask : set(mask, d);
              int nextRem = nextNonZero == 0 ? rem : (check(mask, d) ? rem : rem - 1);
  
              res = min(res, (d * pow[i]) + solve(i - 1, nextIsLarge, nextNonZero, nextRem, nextMask));
          }
  
          //debug(i, res);
          dp[i][isLarge][isNonZero][rem][mask] = res;
          return res;
      }
  
  
      static boolean check(int N, int pos) {
          return (N & (1 << pos)) != 0;
      }
  
      static int set(int N, int pos) {
          return N = N | (1 << pos);
      }
  
      static int reset(int N, int pos) {
          return N = N & ~(1 << pos);
      }
  
  
      static void debug(Object... obj) {
          System.err.println(Arrays.deepToString(obj));
      }
  
      static class InputReader {
          public BufferedReader reader;
          public StringTokenizer tokenizer;
  
          public InputReader(InputStream stream) {
              reader = new BufferedReader(new InputStreamReader(stream));
              tokenizer = null;
          }
  
          public String next() {
  
              try {
                  while (tokenizer == null || !tokenizer.hasMoreTokens())
                      tokenizer = new StringTokenizer(reader.readLine());
              } catch (IOException e) {
                  return null;
              }
  
              return tokenizer.nextToken();
          }
  
          public int nextInt() {
              return Integer.parseInt(next());
          }
      }
  }