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.util.*;
  import java.io.*;
  import java.math.*;
  
  public class cf {
  	static PrintWriter pw = new PrintWriter(System.out);
  
  	public static void main(String[] args) throws IOException, InterruptedException {
  		Scanner sc = new Scanner(System.in);
  		ArrayList<TreeSet<Integer>>[] arr = new ArrayList[11];
  		for (int i = 1; i < 11; i++) {
  			gen = new ArrayList<TreeSet<Integer>>();
  			gen(new TreeSet<>(), i);
  			arr[i] = gen;
  		}
  		HashMap<pair, Long> hm = new HashMap<>();
  		int t = sc.nextInt();
  		while (t-- > 0) {
  			int z = sc.nextInt();
  			char[] n = (z + "").toCharArray();
  			int k = sc.nextInt();
  			if (hm.containsKey(new pair(z, k))) {
  				pw.println(hm.get(new pair(z, k)));
  				continue;
  			}
  			long min = Integer.MAX_VALUE;
  			int[] a = new int[n.length];
  			for (int i = 0; i < a.length; i++) {
  				a[i] = n[i] - '0';
  			}
  			for (TreeSet<Integer> ts : arr[k]) {
  				min = Math.min(min, min(a, 0, false, ts));
  			}
  			hm.put(new pair(z, k), min);
  			pw.println(min);
  		}
  		pw.close();
  	}
  
  	static long pow(long a, long e) {
  		long res = 1;
  		while (e > 0) {
  			if ((e & 1) == 1)
  				res *= a;
  			a *= a;
  			e >>= 1;
  		}
  		return res;
  	}
  
  	static long min(int[] n, int i, boolean f, TreeSet<Integer> ts) {
  		if (i == n.length) {
  			return 0;
  		}
  		if (f) {
  			return min(n, i + 1, true, ts) + pow(10, n.length - i - 1) * ts.first();
  		} else {
  			if (ts.ceiling(n[i]) == null) {
  				return (int) 2e9;
  			}
  			int a = ts.ceiling(n[i]);
  			if (a == n[i]) {
  				if (ts.ceiling(a + 1) == null) {
  					return min(n, i + 1, false, ts) + pow(10, n.length - i - 1) * a;
  				} else {
  					return Math.min(min(n, i + 1, true, ts) + pow(10, n.length - i - 1) * ts.ceiling(a + 1),
  							min(n, i + 1, false, ts) + pow(10, n.length - i - 1) * a);
  				}
  			} else {
  				return min(n, i + 1, true, ts) + pow(10, n.length - i - 1) * a;
  			}
  		}
  	}
  
  	static ArrayList<TreeSet<Integer>> gen;
  
  	static void gen(TreeSet<Integer> ts, int n) {
  		if (ts.size() == n) {
  			gen.add(ts);
  			return;
  		}
  		int start;
  		if (ts.isEmpty())
  			start = 0;
  		else
  			start = ts.last() + 1;
  		for (int i = start; i < 10; i++) {
  			TreeSet<Integer> ts2 = (TreeSet<Integer>) ts.clone();
  			ts2.add(i);
  			gen(ts2, n);
  		}
  	}
  
  	public static class tuble implements Comparable<tuble> {
  		int x;
  		int y;
  		int z;
  
  		public tuble(int x, int y, int z) {
  			this.x = x;
  			this.y = y;
  			this.z = z;
  		}
  
  		public String toString() {
  			return x + " " + y + " " + z;
  		}
  
  		public int compareTo(tuble other) {
  			if (this.x == other.x) {
  				if (this.y == other.y)
  					return this.z - other.z;
  				return this.y - other.y;
  			} else {
  				return this.x - other.x;
  			}
  		}
  	}
  
  	public static class pair implements Comparable<pair> {
  		int x;
  		int y;
  
  		public pair(int x, int y) {
  			this.x = x;
  			this.y = y;
  		}
  
  		public String toString() {
  			return x + " " + y;
  		}
  
  		public boolean equals(Object o) {
  			if (o instanceof pair) {
  				pair p = (pair) o;
  				return p.x == x && p.y == y;
  			}
  			return false;
  		}
  
  		public int hashCode() {
  			return new Integer(x).hashCode() * 31 + new Integer(y).hashCode();
  		}
  
  		public int compareTo(pair other) {
  			if (this.x == other.x) {
  				return Long.compare(this.y, other.y);
  			}
  			return Long.compare(this.x, other.x);
  		}
  	}
  
  	static class Scanner {
  		StringTokenizer st;
  		BufferedReader br;
  
  		public Scanner(InputStream s) {
  			br = new BufferedReader(new InputStreamReader(s));
  		}
  
  		public Scanner(FileReader r) {
  			br = new BufferedReader(r);
  		}
  
  		public String next() throws IOException {
  			while (st == null || !st.hasMoreTokens())
  				st = new StringTokenizer(br.readLine());
  			return st.nextToken();
  		}
  
  		public int nextInt() throws IOException {
  			return Integer.parseInt(next());
  		}
  
  		public long nextLong() throws IOException {
  			return Long.parseLong(next());
  		}
  
  		public String nextLine() throws IOException {
  			return br.readLine();
  		}
  
  		public double nextDouble() throws IOException {
  			return Double.parseDouble(next());
  		}
  
  		public boolean ready() throws IOException {
  			return br.ready();
  		}
  
  	}
  
  }