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 java.util.*;
  import java.io.*;
  
  public class _1560_F2 {
  	public static void main(String[] args) throws IOException {
  		Reader in = new Reader();
  		PrintWriter out = new PrintWriter(System.out);
  		int t = in.nextInt();
  		while(t-- > 0) {	
              int n = in.nextInt();
              String strn = Integer.toString(n);
              int k = in.nextInt();
              TreeSet<Long> candidates = new TreeSet<Long>();
              for(int i = 0; i < strn.length(); i++) {
                  TreeSet<Integer> digits = new TreeSet<Integer>();
                  for(int j = 0; j < i; j++) {
                      digits.add(Character.getNumericValue(strn.charAt(j)));
                  }
                  int digit = Character.getNumericValue(strn.charAt(i));
                  if(digit == 9) {
                      continue;
                  }
                  int size = digits.size();
                  if(size > k) {
                      break;
                  }
                  if(!digits.contains(digit + 1)) {
                      size++;
                  }
                  if(size > k) {
                      StringBuilder cur = new StringBuilder(strn.substring(0, i));
                      Integer higher = digits.higher(digit);
                      boolean possible = true;
                      int ind = i + 1;
                      while(higher == null) {
                          if(!digits.contains(digit)) {
                              possible = false;
                              break;
                          }
                          cur.append(digit);
                          if(ind >= strn.length()) {
                              break;
                          }
                          digit = Character.getNumericValue(strn.charAt(ind));
                          higher = digits.higher(digit);
                          ind++;
                      }
                      if(!possible) {
                          continue;
                      }
                      if(higher != null) {
                          cur.append(higher);
                          for(int j = ind; j < strn.length(); j++) {
                              cur.append(digits.first());
                          }
                      }
                      candidates.add(Long.parseLong(cur.toString()));
                  }else if(size == k) {
                      digits.add(digit + 1);
                      StringBuilder cur = new StringBuilder(strn.substring(0, i));
                      cur.append(digit + 1);
                      for(int j = i + 1; j < strn.length(); j++) {
                          cur.append(digits.first());
                      }
                      candidates.add(Long.parseLong(cur.toString()));
                  }else {
                      StringBuilder cur = new StringBuilder(strn.substring(0, i));
                      cur.append(digit + 1);
                      for(int j = i + 1; j < strn.length(); j++) {
                          cur.append(0);
                      }
                      candidates.add(Long.parseLong(cur.toString()));
                  }
              }
              TreeSet<Integer> digits = new TreeSet<Integer>();
              for(int j = 0; j < strn.length(); j++) {
                  digits.add(Character.getNumericValue(strn.charAt(j)));
              }
              if(digits.size() <= k) {
                  candidates.add((long)n);
              }
              out.println(candidates.first());
  		}
  		in.close();
  		out.close();
  	}
  	static class Reader {
  		final private int BUFFER_SIZE = 1 << 16;
  		private DataInputStream din;
  		private byte[] buffer;
  		private int bufferPointer, bytesRead;
  		public Reader() {
  			din = new DataInputStream(System.in);
  			buffer = new byte[BUFFER_SIZE];
  			bufferPointer = bytesRead = 0;
  		}
  		public Reader(String file_name) throws IOException {
  			din = new DataInputStream(new FileInputStream(file_name));
  			buffer = new byte[BUFFER_SIZE];
  			bufferPointer = bytesRead = 0;
  		}
  		public String readLine() throws IOException {
  			byte[] buf = new byte[64]; // line length
  			int cnt = 0, c;
  			while((c = read()) != -1) {
  				if(c == '\n') {
  					if(cnt != 0) {
  						break;
  					}else {
  						continue;
  					}
  				}
  				buf[cnt++] = (byte) c;
  			}
  			return new String(buf, 0, cnt);
  		}
  		public int nextInt() throws IOException {
  			int ret = 0;
  			byte c = read();
  			while(c <= ' ') {
  				c = read();
  			}
  			boolean neg = (c == '-');
  			if(neg) {
  				c = read();
  			}
  			do{
  				ret = ret * 10 + c - '0';
  			}while((c = read()) >= '0' && c <= '9');
  			if(neg) {
  				return -ret;
  			}
  			return ret;
  		}
  		public long nextLong() throws IOException {
  			long ret = 0;
  			byte c = read();
  			while(c <= ' ') {
  				c = read();
  			}
  			boolean neg = (c == '-');
  			if(neg) {
  				c = read();
  			}
  			do{
  				ret = ret * 10 + c - '0';
  			}while((c = read()) >= '0' && c <= '9');
  			if(neg) {
  				return -ret;
  			}
  			return ret;
  		}
  		public double nextDouble() throws IOException {
  			double ret = 0, div = 1;
  			byte c = read();
  			while(c <= ' ') {
  				c = read();
  			}
  			boolean neg = (c == '-');
  			if(neg) {
  				c = read();
  			}
  			do{
  				ret = ret * 10 + c - '0';
  			}while((c = read()) >= '0' && c <= '9');
  			if(c == '.') {
  				while((c = read()) >= '0' && c <= '9') {
  					ret += (c - '0') / (div *= 10);
  				}
  			}
  			if(neg) {
  				return -ret;
  			}
  			return ret;
  		}
  		private void fillBuffer() throws IOException {
  			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  			if(bytesRead == -1) {
  				buffer[0] = -1;
  			}
  		}
  		private byte read() throws IOException {
  			if(bufferPointer == bytesRead) {
  				fillBuffer();
  			}
  			return buffer[bufferPointer++];
  		}
  		public void close() throws IOException {
  			if(din == null) {
  				return;
  			}
  			din.close();
  		}
  	}
  }
  