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 sys
  import os
  import math
  from io import BytesIO, IOBase
  from collections import defaultdict,Counter,deque
  import bisect
  # input=sys.stdin.readline
  # def print(x):
  #     sys.stdout.write(str(x)+"\n")
  
  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")
  
  # sys.stdout=open("CP1/output.txt",'w')
  # sys.stdin=open("CP1/input.txt",'r')
  
  # mod=pow(10,9)+7
  
  t=int(input())
  for i in range(t):
  	n,k=input().split()
  	k=int(k)
  	n1=list(map(int,n))
  	# a=list(map(int,input().split()))
  	# n=input()
  	s1=set()
  	l=len(n1)
  	ans=[]
  	for j in range(l):
  		if n1[j] in s1:
  			ans.append(n1[j])
  		elif len(s1)<k:
  			s1.add(n1[j])
  			ans.append(n1[j])
  		else:
  			ma=10
  			for kk in s1:
  				if kk>n1[j]:
  					ma=min(ma,kk)
  
  			if ma!=10:
  				ans.append(ma)
  				ans.extend([min(s1)]*(l-len(ans)))
  			break
  
  
  	if len(ans)==l:
  		ans=''.join(map(str,ans))
  		print(ans)
  	else:
  		if k==1:
  			print(str(ans[0]+1)*l)
  			continue
  
  		final=sorted(s1)
  		l1=len(final)
  		ans1=10**10
  
  		for j in range(1,len(ans)):
  			s=set(ans[:j])
  			mi=min(s)
  			if len(s)==k:
  				ind=bisect.bisect_right(final,ans[j])
  				if ind!=l1:
  					cur=ans[:j]+[final[ind]]
  				else:
  					cur=[]
  					for kk in range(j+1):
  						if ans[kk]==final[-1]:
  							cur.append(ans[kk]+1)
  						else:
  							cur.append(ans[kk])
  
  				cur.extend([mi]*(l-j-1))
  
  			else:
  				cur=ans[:j]+[ans[j]+1]
  				s.add(ans[j]+1)
  				if len(s)==k:
  					cur.extend([min(s)]*(l-j-1))
  				else:
  					cur.extend([0]*(l-j-1))
  
  
  			# print(cur)
  			ans1=min(ans1,int(''.join(map(str,cur))))
  
  
  		print(ans1)
  