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: 
  def brute(n, k):
      for i in range(int(n),  10 ** len(n)):
          if len(set(str(i))) <= k:
              return i
  
  def strange(n, k):
      if len(set(n)) <= k:
          return int(n)
      str_n = n
      n = list(map(int, list(n)))
      minn = float("inf")
  
      for i in range(len(n) - 1, -1, -1):
          #firstvar = str_n[:(i+1)] + str(min(n[:(i+1)])) * (  (len(n) - 1) - i )
          #boeti = min(filter(lambda x: x >= n[i], n[:i]))
          #secondvar = str_n[:i] + str(n[i] + j) + str(min(n[:i] + [n[i] + j])) * (  (len(n) - 1) - i )
          # lp = a[0:i], m = a[i], rp = a[i+1:n]
          # if len(set(a[0:i])) > k: continue
          # if len(set(a[0:i])) == k: check(lp, min(lp[lp >= m]), min(lp) )
          # if len(set(a[0:i])) == (k - 1): check(lp, m, min(lp + [m])) and check(lp, min(lp[lp >= m]), 0 * len(rp)) and check(lp, m + 1, min(lp, m + 1))
          # if len(set(a[0:i])) < (k - 1): check(lp, m, 0 * len(rp))
          rp = len(n) - 1 - i
          if len(set(n[0:i])) > k: 
              continue
          if len(set(n[0:i])) == k:
              ll = list(filter(lambda x: x > n[i], n[0:i]))
              if  len(ll):
                  v1 = int( str_n[0:i] + str(min(ll)) + str(min(n[0:i])) * (len(n) - 1 - i) )
                  if v1 >= int(str_n) and v1 < minn:
                      minn = v1
          elif len(set(n[0:i])) == (k - 1):
              v1 = int( str_n[0:i] + str(n[i]) + str(min(n[0:(i + 1)])) * (len(n) - 1 - i) )
              ll = list(filter(lambda x: x > n[i], n[0:i]))
              v2 = None
              if len(ll):
                  v2 = int( str_n[0:i] + str(min( ll )) + "0" * (len(n) - 1 - i) )
              v3 = int( str_n[0:i] + str(n[i] + 1) + str(min(n[0:i] + [ n[i] + 1 ])) * (len(n) - 1 - i) )
              for v in [v1, v2, v3]:
                  if not v:
                      continue
                  if v >= int(str_n) and v < minn:
                      minn = v
          else:
              v1 = int( str_n[0:i] + str(n[i]) + str(min(n[0:(i + 1)])) * (len(n) - 1 - i) )
              ll = list(filter(lambda x: x > n[i], n[0:i]))
              v2 = None
              if len(ll):
                  v2 = int( str_n[0:i] + str(min( ll )) + "0" * (len(n) - 1 - i) )
              v3 = int( str_n[0:i] + str(n[i] + 1) + str(min(n[0:i] + [ n[i] + 1 ])) * (len(n) - 1 - i) )
              v4 = int( str_n[0:i] + str(n[i]) + "0" * (len(n) - 1 - i) )
              v5 = int( str_n[0:i] + str(n[i] + 1) + "0" * (len(n) - 1 - i) )
              for v in [v1, v2, v3, v4, v5]:
                  if not v:
                      continue
                  if v >= int(str_n) and v < minn:
                      minn = v
  
      return minn
  
  
  
  
  
  
  t = int(input())
  for _ in range(t):
      # x >= n; x has no more than k unique digits
      # 9249429 -> 7922324
      # if k == 1 we either xi + 1, and remaining fill with smallest we got before
      # 
  
      n, k = input().split(" ")
      print(strange(n, int(k)))
  
  '''
  tests = [
      ("252258861", 2),
      ("402143038", 5),
      ("34215184", 5),
      ("544627347", 2),
      ("167346669", 5),
      ("238084491", 5)
  
  ]
  for nnn, k in tests:
      s, b = strange(nnn, k), brute(nnn, k)
      if s != b:
          print(nnn, k, s, b)
  
  '''
  
  """
  
  for nnn in range(1, 10**9 + 1):
      for k in range(1, 11):
          s, b = strange(str(nnn), k), brute(str(nnn), k)
          if s != b:
              print(nnn, k, s, b)
  """