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: from math import ceil
  import sys
  input=sys.stdin.readline
  
  t=int(input())
  #t=1
  for j in range(t):
      #temp1=input()
      #n,k=map(int,input().split())
      #n=int(input())
      #a=[int(x) for x in input().split()]
      #s=input().strip()
      #t=list(input().strip())
      temp=input().split()
  
      n=list(temp[0])
      n=[int(x) for x in n]
      k=int(temp[1])
  
      if k==1:
          temp=n[0]
          le=len(n)
          for i in range(le-1):
              if n[i+1]>n[i]:
                  temp+=1
                  break
              elif n[i+1]<n[i]:
                  break
          for i in range(le):
              print(temp,end="")
          print()
      else:
          temp1=n[0]
          temp2=-1
          ind=-1
          le=len(n)
          for i in range(le-1):
              if n[i+1]!=n[i]:
                  temp2=n[i+1]
                  ind = i+1
                  break
          if temp2==-1:
              for i in n:
                  print(i,end="")
              print()
              continue
          inde=-1
          for i in range(ind,le):
              if n[i]!= temp1 and n[i]!=temp2:
                  inde =i
                  break
          if inde==-1:
              for i in n:
                  print(i,end="")
              print()
              continue
          big = max(temp1,temp2)
          small=min(temp1,temp2)
  
          if n[inde]<small:
              for i in range(inde,le):
                  n[i]=small
          elif n[inde]<big:
              for i in range(inde,le):
                  n[i]=small
              n[inde]=big
          else:
                  if n[inde-1]==small:
                      if n[:inde].count(small)==1:
                          temp=n[:inde].index(small)
                          small=small+1
                          n[temp]=small
                          if small==big:
                              small=0
                          for i in range(temp+1,le):
                              n[i]=small
                      else:
                          for i in range(inde,le):
                              n[i]=small
                          n[inde-1]=big
                  else:
                      temp=-1
                      for i in range(inde):
                          if n[i]==small:
                              temp=i
                      flag=True
                      if n[0]==small:
                          flag=False
                          for i in range(temp):
                              if n[i]!=small:
                                  flag=True
                                  break
  
                      if flag:
                          if n[:inde].count(small)==1:
                              temp=n[:inde].index(small)
                              small=small+1
                              n[temp]=small
                              if small==big:
                                  small=0
                              for i in range(temp+1,le):
                                  n[i]=small
                          else:
                              n[temp]=big
                              for i in range(temp+1,le):
                                  n[i]=small
                      else:
                          n[temp+1]=big+1
                          for i in range(temp+2,le):
                              n[i]=small
                  
              
          for i in range(le):
              print(n[i],end="")
          print()