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.io.*;
  import java.util.*;
   
  public class NearestBeautifulEasy {
  
      public static void main(String[] args) throws IOException {
          FastScanner in = new FastScanner();
          PrintWriter out = new PrintWriter(System.out);
          int t = in.nextInt();
          for (int i = 1; i <= t; i++)  {
          	long n = in.nextLong();
          	long k = in.nextLong();
          	if (k == 1)  {
          		String x = Long.toString(n);
          		int numDigits = x.length();
          		
          		char[] test1 = new char[numDigits];
          		for (int j = 0; j < numDigits; j++)  {
          			test1[j] = x.charAt(0);
          		}
          		String test1Str = new String(test1);
          		long test1Num = Long.parseLong(test1Str);
          		
          		if (test1Num >= n)  {
          			out.println(test1Num);
          		}
          		else  {
              		for (int j = 0; j < numDigits; j++)  {
              			test1[j] = (char) ((int) (x.charAt(0) - '0') + 1 + 48);
              		}
              		test1Str = new String(test1);
              		test1Num = Long.parseLong(test1Str);
              		
          			out.println(test1Num);
          		}
          	}
          	else if (k == 2)  {
          		Set<Integer> digits = new HashSet<Integer>();
          		int firstDigit = -1;
          		int secondDigit = -1;
          		int thirdDigit = -1;
          		
          		int firstSecondIndex = -1;
          		int lastFirstIndex = 0;
          		int lastSecondIndex = -1;
          		int thirdIndex = -1;
          		
          		int numFirstDigitsBeforeThirdDigit = 0;
          		int numSecondDigitsBeforeThirdDigit = 0;
          		
          		String x = Long.toString(n);
          		char[] temp = x.toCharArray();
          		for (int j = 0; j < temp.length; j++)  {
          			int myDigit = (int) (temp[j] - '0');
          			
          			digits.add(myDigit);
          			if (firstDigit == -1)  {
          				firstDigit = myDigit;
          				lastFirstIndex = j;
          			}
          			else if ((secondDigit == -1) && (myDigit != firstDigit))  {
          				secondDigit = myDigit;
          				firstSecondIndex = j;
          				lastSecondIndex = j;
          			}
          			else if ((thirdDigit == -1)  && (myDigit != firstDigit) && (myDigit != secondDigit)) {
          				thirdDigit = (int) (temp[j] - '0');
          				thirdIndex = j;
          			}
          			
          			if ((myDigit == firstDigit) && (thirdDigit == -1))  {
          				lastFirstIndex = j;
          				numFirstDigitsBeforeThirdDigit++;
          			}
          			if ((myDigit == secondDigit) && (thirdDigit == -1))  {
          				lastSecondIndex = j;
          				numSecondDigitsBeforeThirdDigit++;
          			}
          			
          		}
          		
          		if (digits.size() <= 2)  {
          			out.println(n);
          		}
          		else  {
              		char[] output = new char[temp.length];
              		int smallDigit = Math.min(firstDigit, secondDigit);
              		int bigDigit = Math.max(firstDigit, secondDigit);
      				if (thirdDigit < smallDigit)  {
                  		for (int j = 0; j < thirdIndex; j++)  {
                  			output[j] = temp[j];
                  		}
                  		for (int j = thirdIndex; j < temp.length; j++)  {
                  			output[j] = (char) (smallDigit + 48);
                  		}
      				}
      				else if ((thirdDigit > smallDigit) && (thirdDigit < bigDigit))  {
                  		for (int j = 0; j < thirdIndex; j++)  {
                  			output[j] = temp[j];
                  		}
              			output[thirdIndex] = (char) (bigDigit + 48);
                  		for (int j = thirdIndex + 1; j < temp.length; j++)  {
                  			output[j] = (char) (smallDigit + 48);
                  		}
      				}
      				else if (thirdDigit > bigDigit)  {
                  		if (firstDigit < secondDigit)  {
                  			// If secondDigits are all together at the back, set the first secondDigit as secondDigit + 1 and the rest as firstDigit
              				if (lastFirstIndex == firstSecondIndex - 1)  {
                          		for (int j = 0; j < temp.length; j++)  {
                          			output[j] = (char) (firstDigit + 48);
                          		}
              					output[firstSecondIndex] = (char) (secondDigit + 1 + 48);
              				}
                  			// Else set the last firstDigit as secondDigit and the rest as firstDigit
              				else  {
                          		for (int j = 0; j < lastFirstIndex; j++)  {
                          			output[j] = temp[j];
                          		}
              					output[lastFirstIndex] = (char) (secondDigit + 48);
                          		for (int j = lastFirstIndex + 1; j < temp.length; j++)  {
                          			output[j] = (char) (firstDigit + 48);
                          		}
              				}
              				
                  		}
                  		else  {
                      		for (int j = 0; j < firstSecondIndex; j++)  {
                      			output[j] = temp[j];
                      		}
                  			if (lastSecondIndex > lastFirstIndex) {
                          		for (int j = 0; j < lastSecondIndex; j++)  {
                          			output[j] = temp[j];
                          		}
                          		if (numSecondDigitsBeforeThirdDigit > 1)  {
                      				output[lastSecondIndex] = (char) (firstDigit + 48);
                              		for (int j = lastSecondIndex + 1; j < temp.length; j++)  {
                              			output[j] = (char) (secondDigit + 48);
                              		}
                          		}
                          		else  {
                      				output[lastSecondIndex] = (char) (secondDigit + 1 + 48);
                      				if (firstDigit == secondDigit + 1)  {
                                  		for (int j = lastSecondIndex + 1; j < temp.length; j++)  {
                                  			output[j] = (char) (0 + 48);
                                  		}
                      				}
                      				else  {
                                  		for (int j = lastSecondIndex + 1; j < temp.length; j++)  {
                                  			output[j] = (char) (secondDigit + 1 + 48);
                                  		}
                      				}
                          		}
                  			}
                  			else {
                          		if (numSecondDigitsBeforeThirdDigit > 1)  {
                              		for (int j = 0; j < lastSecondIndex; j++)  {
                              			output[j] = temp[j];
                              		}
                      				output[lastSecondIndex] = (char) (firstDigit + 48);
                              		for (int j = lastSecondIndex + 1; j < temp.length; j++)  {
                              			output[j] = (char) (secondDigit + 48);
                              		}
                          		}
                          		else  {
                      				output[firstSecondIndex] = (char) (secondDigit + 1 + 48);
                      				if (firstDigit == secondDigit + 1)  {
                                  		for (int j = firstSecondIndex + 1; j < temp.length; j++)  {
                                  			output[j] = (char) (0 + 48);
                                  		}
                      				}
                      				else  {
                                  		for (int j = firstSecondIndex + 1; j < temp.length; j++)  {
                                  			output[j] = (char) (secondDigit + 1 + 48);
                                  		}
                      				}
                          		}
                  			}
                  		}
      				}
          			out.println(new String(output));
          		}
          	}
          }
          out.close();
      }
      
      static class FastScanner {
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          StringTokenizer st=new StringTokenizer("");
          String next() {
              while (!st.hasMoreTokens())
                  try { 
                      st = new StringTokenizer(br.readLine());                               
                  } catch (IOException e) {
                      // noop
                  }
              return st.nextToken();
          }
          
          int nextInt() {
              return Integer.parseInt(next());
          }
          long nextLong() {
              return Long.parseLong(next());
          }
          double nextDouble()  {
              return Double.parseDouble(next());
          }
      }
  
  }