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: # ------------------- fast io --------------------
  import os
  import sys
  from io import BytesIO, IOBase
  
  BUFSIZE = 8192
  
  
  class FastIO(IOBase):
      newlines = 0
      
      def __init__(self, file):
          self._fd = file.fileno()
          self.buffer = BytesIO()
          self.writable = "x" in file.mode or "r" not in file.mode
          self.write = self.buffer.write if self.writable else None
      
      def read(self):
          while True:
              b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
              if not b:
                  break
              ptr = self.buffer.tell()
              self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
          self.newlines = 0
          return self.buffer.read()
      
      def readline(self):
          while self.newlines == 0:
              b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
              self.newlines = b.count(b"\n") + (not b)
              ptr = self.buffer.tell()
              self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
          self.newlines -= 1
          return self.buffer.readline()
      
      def flush(self):
          if self.writable:
              os.write(self._fd, self.buffer.getvalue())
              self.buffer.truncate(0), self.buffer.seek(0)
  
  
  class IOWrapper(IOBase):
      def __init__(self, file):
          self.buffer = FastIO(file)
          self.flush = self.buffer.flush
          self.writable = self.buffer.writable
          self.write = lambda s: self.buffer.write(s.encode("ascii"))
          self.read = lambda: self.buffer.read().decode("ascii")
          self.readline = lambda: self.buffer.readline().decode("ascii")
  
  
  sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
  input = lambda: sys.stdin.readline().rstrip("\r\n")
  
  # ------------------- fast io --------------------
  from math import ceil
  
  
  def prod(a, mod=10 ** 9 + 7):
      ans = 1
      for each in a:
          ans = (ans * each) % mod
      return ans
  
  
  def gcd(x, y):
      while y:
          x, y = y, x % y
      return x
  
  
  def lcm(a, b): return a * b // gcd(a, b)
  
  
  def binary(x, length=16):
      y = bin(x)[2:]
      return y if len(y) >= length else "0" * (length - len(y)) + y
  
  
  def solve(n, k):
      ans = int('9' * 10) - n
      x = [int(p) for p in str(n)]
      d = len(x)
      if k == 1:
          for i in '123456789':
              y = int(i * d)
              if y >= n:
                  ans = min(ans, y - n)
          return (n + ans)
      
      if len(set(x)) <= 2:
          return n
      
      d1 = x[0]
      d2 = d1
      for y in x:
          if y != x[0]:
              d2 = y
              break
      
      bx = [(d1, d2), (d1, 0)]
      if d2 != 9:
          bx += [(d1, d2 + 1)]
      
      for d1, d2 in bx:
          if d1 == d2: continue
          anss = [d1]
          d1, d2 = min(d1, d2), max(d1, d2)
          
          exc = False
          for i in range(1, len(x)):
              if exc:
                  anss += [d1]
                  continue
              if x[i] == d1 or x[i] == d2:
                  anss += [x[i]]
              else:
                  exc = True
                  if d1 > x[i]:
                      anss += [d1]
                  else:
                      anss += [d2]
          y = int("".join(str(k) for k in anss))
          if y >= n:
              ans = min(ans, y - n)
          
          anss = [d1]
          d1, d2 = min(d1, d2), max(d1, d2)
          
          exc = False
          for i in range(1, len(x)):
              if exc:
                  anss += [d1]
                  continue
              if x[i] == d1 or x[i] == d2:
                  anss += [x[i]]
              else:
                  exc = True
                  if d1 > x[i]:
                      anss += [d1]
                  else:
                      anss += [d2]
          y = int("".join(str(k) for k in anss))
          if y >= n:
              ans = min(ans, y - n)
          
          if d1 in x:
              anss = list(x)
              j = 0
              for i in range(len(x)):
                  if x[i] == d1:
                      j = i
                  if x[i] != d1 and x[i] != d2: break
              anss[j] = d2
              for i in range(j + 1, len(anss)): anss[i] = d1
              y = int("".join(str(k) for k in anss))
              if y >= n and len(set(anss)) <= 2:
                  ans = min(ans, y - n)
      
      return ans + n
  
  for _ in range(int(input()) if True else 1):
      n, k = map(int, input().split())
      #a, b,c = map(int, input().split())
      # c, d = map(int, input().split())
      # a = list(map(int, input().split()))
      # b = list(map(int, input().split()))
      print(solve(n, k))