description: It is a complicated version of problem F1. 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 ≤ 10).
  
  Output
  
  For each test case output on a separate line x — the minimum k-beautiful integer such that x ≥ n.
  
  Example
  
  Input
  
  
  6
  2021 3
  177890 2
  34512 3
  724533 4
  998244353 1
  12345678 10
  
  
  Output
  
  
  2021
  181111
  34533
  724542
  999999999
  12345678
solution: import java.io.*;
  import java.util.*;
  import java.math.BigInteger;
  import java.util.InputMismatchException;
  
  public class Main {
  	static PrintWriter out;
  	static Reader in;
  	public static void main(String[] args) throws IOException {
  		input_output();
  		Main solver = new Main();
  		solver.solve();
  		out.close();
  
  		out.flush();
  	} 
  
  	static long INF = (long)1e18;
  	static int MAXN = (int)1e5 + 5;
  	static int MOD = (int)1e9 + 7;
  	static int q, t, n, m, k;
  	static double pi = Math.PI;
  
  	void solve() throws IOException {
  		t = in.nextInt();
  		
  
  		while (t --> 0) {
  			n = in.nextInt();
  			k = in.nextInt();
  
  			String ans = solve1(n+"", k);
  			out.println(ans);
  		}	
  	}
  
  	static String solve1(String s, int k) {
  		int len = s.length();
  		
  		int ptr = 0, size = 0;
  		int[] cnt = new int[10];
  		StringBuilder newN = new StringBuilder("");
  		while (true) { // deal with the case where ptr == len
  
  			if (cnt[s.charAt(ptr)-'0'] == 0 && size == k) {
  
  				for (int i = s.charAt(ptr)-'0'+1; i <= 9; i++) {
  					if (cnt[i] != 0) {
  						for (int j = 0; j <= ptr-1; j++) {
  							newN.append(s.charAt(j));
  						}
  						newN.append(i);
  						while (newN.length() != len) newN.append('0');
  						return solve1(newN.toString(), k);
  					}
  				}
  
  				for (int j = 0; j <= ptr-2; j++) newN.append(s.charAt(j));
  				newN.append((char)(s.charAt(ptr-1)+1));
  				while (newN.length() != len) newN.append('0');
  				return solve1(newN.toString(), k);
  			} else {
  				if (++cnt[s.charAt(ptr)-'0'] == 1) size++;
  				ptr++;
  			}
  
  			if (ptr == len) break;
  		}
  
  
  		return s;
  	}
  
  	static class Reader {
  		private InputStream mIs;
  		private byte[] buf = new byte[1024];
  		private int curChar;
  		private int numChars;
  
  		public Reader() {
  			this(System.in);
  		}
  
  		public Reader(InputStream is) {
  			mIs = is;
  		}
  
  		public int read() {
  			if (numChars == -1) {
  				throw new InputMismatchException();
  
  			}
  			if (curChar >= numChars) {
  				curChar = 0;
  				try {
  					numChars = mIs.read(buf);
  				} catch (IOException e) {
  					throw new InputMismatchException();
  				}
  				if (numChars <= 0) {
  					return -1;
  				}
  			}
  			return buf[curChar++];
  		}
  
  		public String nextLine() {
  			int c = read();
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			StringBuilder res = new StringBuilder();
  			do {
  				res.appendCodePoint(c);
  				c = read();
  			} while (!isEndOfLine(c));
  			return res.toString();
  		}
  
  		public String next() {
  			int c = read();
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			StringBuilder res = new StringBuilder();
  			do {
  				res.appendCodePoint(c);
  				c = read();
  			} while (!isSpaceChar(c));
  			return res.toString();
  		}
  
  		double nextDouble() {
  			return Double.parseDouble(next());
  		}
  
  		public long nextLong() {
  			int c = read();
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			int sgn = 1;
  			if (c == '-') {
  				sgn = -1;
  				c = read();
  			}
  			long res = 0;
  			do {
  				if (c < '0' || c > '9') {
  					throw new InputMismatchException();
  				}
  				res *= 10;
  				res += c - '0';
  				c = read();
  			} while (!isSpaceChar(c));
  			return res * sgn;
  		}
  
  		public int nextInt() {
  			int c = read();
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			int sgn = 1;
  			if (c == '-') {
  				sgn = -1;
  				c = read();
  			}
  			int res = 0;
  			do {
  				if (c < '0' || c > '9') {
  					throw new InputMismatchException();
  				}
  				res *= 10;
  				res += c - '0';
  				c = read();
  			} while (!isSpaceChar(c));
  			return res * sgn;
  		}
  
  		public boolean isSpaceChar(int c) {
  			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  		}
  
  		public boolean isEndOfLine(int c) {
  			return c == '\n' || c == '\r' || c == -1;
  		}
  
  	}
  	static void input_output() throws IOException {
  		File f = new File("in.txt");
  		if (f.exists() && !f.isDirectory()) {
  			in = new Reader(new FileInputStream("in.txt"));
  		} else in = new Reader();
  		f = new File("out.txt");
  		if (f.exists() && !f.isDirectory()) {
  			out = new PrintWriter(new File("out.txt"));
  		} else out = new PrintWriter(System.out);
  	}
  }