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: // package Self;
  
  import java.util.Scanner;
  import java.util.Set;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Stack;
  import java.util.StringTokenizer;
  import java.util.TreeMap;
  import java.util.Queue;
  import java.util.LinkedList;
  import java.util.List;
  import java.util.Map;
  import java.util.PriorityQueue;
  
  public class Solution {
  
  	static FastReader scn = new FastReader();
  
  	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 int maxN = (int) 2e5 + 10;
  	static StringBuilder sb = new StringBuilder();
  
  	public static void main(String[] args) {
  
  		int t = 1;
  		t = scn.nextInt();
  
  		while (t-- > 0) {
  
  			int n = scn.nextInt();
  			int k = scn.nextInt();
  
  			String str = n + "";
  
  			int[] freq = new int[10];
  
  			for (int i = 0; i < str.length(); i++) {
  				freq[str.charAt(i) - '0']++;
  			}
  
  			int count = 0;
  
  			for (int val : freq) {
  
  				if (val != 0)
  					count++;
  
  			}
  
  			if (count <= k) {
  				sb.append(n);
  				sb.append("\n");
  				continue;
  			}
  
  			ArrayList<Long> res = new ArrayList<>();
  
  			int num0 = str.charAt(0) - '0';
  
  			if (k == 1) {
  				res.add(find(num0, str.length()));
  
  				if (num0 != 0)
  					res.add(find(num0 + 1, str.length()));
  
  			} else {
  
  				for (int sec = 0; sec <= 9; sec++)
  					fill(num0, str.length() - 1, num0, sec, res);
  			}
  
  //			res.add(find(1, str.length() + 1));
  
  			Long ans = Long.MAX_VALUE;
  
  			for (long val : res) {
  				if (val >= n && val < ans)
  					ans = val;
  			}
  
  			sb.append(ans);
  			sb.append("\n");
  		}
  
  		System.out.println(sb);
  
  	}
  
  	private static Long find(int digit, int count) {
  
  		long n = 0;
  
  		while (count-- > 0) {
  			n = n * 10 + digit;
  		}
  
  		return n;
  
  	}
  
  	private static void fill(long cur, int digits, int a, int b, ArrayList<Long> res) {
  
  		if (digits == 0) {
  			res.add(cur);
  			return;
  		}
  
  		fill(cur * 10 + a, digits - 1, a, b, res);
  		fill(cur * 10 + b, digits - 1, a, b, res);
  
  	}
  
  	public static void yes() {
  		sb.append("YES\n");
  	}
  
  	public static void no() {
  		sb.append("NO\n");
  	}
  
  	public static void swap(int[] arr, int i, int j) {
  		int temp = arr[i];
  		arr[i] = arr[j];
  		arr[j] = temp;
  	}
  
  	public static long[] longInput(int n) {
  
  		long[] arr = new long[n];
  
  		for (int i = 0; i < n; i++) {
  			arr[i] = scn.nextLong();
  		}
  
  		return arr;
  
  	}
  
  	public static int[] input(int n) {
  
  		int[] arr = new int[n];
  
  		for (int i = 0; i < n; i++) {
  			arr[i] = scn.nextInt();
  		}
  
  		return arr;
  
  	}
  
  }