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 sys import stdin
  raw_input = lambda: stdin.readline().rstrip()
  input = lambda: int(raw_input())
  I=lambda: map(int, raw_input().split())
  t = input()
  for _ in xrange(t):
  	n,k = I()
  	q = [int(x) for x in str(n)]
  	l = len(q)
  	k1 = k
  	wLst = []
  	s = set()
  	j = -1
  	for i in xrange(l):
  		x = q[i]
  		if x not in s:
  			if k1==0:
  				j = i
  				xJ = x
  				break
  			s.add(x)
  			wLst.append(x)
  			k1 -= 1
  	if j==-1:
  		print n
  		continue
  	wLst.sort()
  	mW = wLst[0]
  	MW = wLst[-1]
  	if k==1:
  		if q[0]>xJ:
  			r = str(q[0])*l
  		else:
  			r = str(q[0]+1)*l
  	else:
  		if xJ<MW:
  			for x in wLst:
  				if x > xJ:
  					break
  			rLst = q[:j] + [x] + [mW]*(l-j-1)
  		else:
  			j1 = j-1
  			xJ1 = q[j1]
  			if xJ1!=MW:
  				for i in xrange(l):
  					if q[i]==xJ1:
  						leftJ = i
  						break
  				if leftJ==j1:
  					if xJ1==mW:
  						mW += 1
  					if xJ1+1 in wLst:
  						rLst = q[:j1] + [xJ1+1] + [0]*(l-j1-1)
  					else:
  						rLst = q[:j1] + [xJ1+1] + [mW]*(l-j1-1)
  				else:
  					for x in wLst:
  						if x > xJ1:
  							break
  					rLst = q[:j1] + [x] + [mW]*(l-j1-1)
  			else:
  				for i in xrange(l):
  					if q[i]==xJ1:
  						leftJ = i
  						break
  				if leftJ==j1 and xJ1<9:
  					rLst = q[:j1] + [xJ1+1] + [mW]*(l-j1-1)
  				else:
  					while q[j1] == MW:
  						j1 -= 1
  					j1 += 1
  					xJ1 = q[j1]
  					for i in xrange(l):
  						if q[i]==xJ1:
  							leftJ = i
  							break
  					if leftJ==j1 and xJ1<9:
  						rLst = q[:j1] + [xJ1+1] + [mW]*(l-j1-1)
  					else:
  						j1 -= 1
  						xJ1 = q[j1]
  						for i in xrange(l):
  							if q[i]==xJ1:
  								leftJ = i
  								break
  						if leftJ==j1:
  							if xJ1==mW:
  								mW += 1
  							if xJ1+1 in wLst:
  								rLst = q[:j1] + [xJ1+1] + [0]*(l-j1-1)
  							else:
  								rLst = q[:j1] + [xJ1+1] + [mW]*(l-j1-1)
  						else:
  							for x in wLst:
  								if x > xJ1:
  									break
  							rLst = q[:j1] + [x] + [mW]*(l-j1-1)
  		r = ''.join([str(x) for x in rLst])
  	print r
  		