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: /*Everything is Hard 
   * Before Easy 
   * Jai Mata Dii 
   */ 
   
  import java.util.*;
  import java.io.*; 
    
  public class Main {
  	static class FastReader{ BufferedReader br;StringTokenizer st;public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}String next(){while (st == null || !st.hasMoreElements()){try{st = new StringTokenizer(br.readLine());}catch (IOException  e){e.printStackTrace();}}return st.nextToken();}int nextInt(){ return Integer.parseInt(next());}long nextLong(){return Long.parseLong(next());}double nextDouble(){return Double.parseDouble(next());}String nextLine(){String str = ""; try{str = br.readLine(); } catch (IOException e) {e.printStackTrace();} return str; }} 
  	static long mod = (long)(1e9+7); 
  	// static long mod = 998244353; 
  //	 static Scanner sc = new Scanner(System.in); 
  	static FastReader sc = new FastReader(); 
  	static PrintWriter out = new PrintWriter(System.out);
  	public static void main (String[] args) { 
  		int ttt = 1;
  		ttt = sc.nextInt();
  		long xyz = -1;
  		z :for(int tc=1;tc<=ttt;tc++){
  			long n = sc.nextLong();
  			int k = sc.nextInt();
  //			if(tc == 1) xyz = n;
  //			if(tc == 220 && xyz == 999999001) {
  //				System.out.println(n+" "+k);
  //				continue;
  //			}
  			ArrayList<Long> a = new ArrayList<>();
  			HashSet<Long> hs = new HashSet<>();
  			long res = n;
  			while(n>0) {
  				a.add(n%10);
  				hs.add(n%10);
  				n /= 10;
  			}
  			if(res<10 || hs.size() <= k) {
  				out.write(res+"\n");
  				continue;
  			}
  			Collections.reverse(a);
  			if(k == 1) {
  				boolean is = false;
  				for(int i=1;i<a.size();i++) {
  					if(a.get(i)<a.get(0)) {
  						is = true;
  						break;
  					}
  					if(a.get(i)>a.get(0)) {
  						break;
  					}
  				}
  				if(is) {
  					for(int i=0;i<a.size();i++) {
  						out.write(a.get(0)+"");
  					}
  				}
  				else {
  					if(a.get(0)==9) {
  						out.write(1+"");
  						for(int i=0;i<a.size();i++) {
  							out.write(1+"");
  						}
  					}
  					else {
  //						out.write((a.get(0)+1)+"");
  						for(int i=0;i<a.size();i++) {
  							out.write((a.get(0)+1)+"");
  						}
  					}
  				}
  			}
  			else {
  				long fir = a.get(0), sec = -1;
  				int idx = -1;
  				for(int i=1;i<a.size();i++) {
  					if(fir != a.get(i)) {
  						sec = a.get(i);
  						idx = i+1;
  						break;
  					}
  				}
  				if(sec == -1) {
  					if(a.get(a.size()-1)==9) {
  						out.write(1+"");
  						for(int i=0;i<a.size();i++) {
  							out.write(1+"");
  						}
  					}
  					else {
  						for(int i=0;i<a.size()-1;i++) {
  							out.write(a.get(0)+"");
  						}
  						out.write((a.get(a.size()-1)+1)+"");
  					}
  				}
  				else {
  					boolean is = false;
  					boolean first = false;
  					int value = -1;
  //					System.out.println(idx+" "+fir+" "+sec);
  					while(idx<a.size()) {
  						if(a.get(idx)<sec || a.get(idx)<fir) {
  							is = true;
  							if(a.get(idx)<fir && a.get(idx)<sec) {
  								first = false;
  								if(fir<sec) first = true;
  							}
  							else if(a.get(idx)<fir) first = true;
  							value = idx;
  							if(a.get(idx)!=fir && a.get(idx)!=sec)
  								break;
  						}
  						if(a.get(idx)>sec && a.get(idx)>fir) {
  							break;
  						}
  						idx++;
  					}
  					if(is) {
  						int i = 0;
  						for(i=0;i<value;i++) {
  							out.write(a.get(i)+"");
  						}
  						if(first) out.write(fir+"");
  						else out.write(sec+"");
  						i++;
  						while(i<a.size()) {
  							if(fir<sec) out.write(fir+"");
  							else out.write(sec+"");
  							i++;
  						}
  					}
  					else {
  						int i = 0;
  						for(i=0;i<a.size();i++) {
  							if(a.get(i) == fir) out.write(fir+"");
  							else break;
  						}
  						out.write((sec+1)+"");
  						long nxt = Math.min(sec+1, fir);
  						if(sec+1==fir) nxt = 0;
  						i++;
  						while(i<a.size()) {
  							out.write(nxt+"");
  							i++;
  						}
  					}
  				}
  			}
  			out.write("\n");
  		}
  		out.close();
  	}
  	static long pow(long a, long b){long ret = 1;while(b>0){if(b%2 == 0){a = (a*a)%mod;b /= 2;}else{ret = (ret*a)%mod;b--;}}return ret%mod;}
  	static long gcd(long a,long b){if(b==0) return  a; return gcd(b,a%b); } 
  	private static void sort(double[] a) {List<Double> k = new ArrayList<>();for(double val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);} 
  	private static void ini(List<Integer>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}} 
  	private static void init(List<int[]>[] tre2){for(int i=0;i<tre2.length;i++){tre2[i] = new ArrayList<>();}} 
  	private static void sort(long[] a) {List<Long> k = new ArrayList<>();for(long val : a) k.add(val);Collections.sort(k);for(int i=0;i<a.length;i++) a[i] = k.get(i);} 
  }