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: #include <bits/stdc++.h>
  using namespace std;
  #pragma gcc optimize("O3,unroll-loop")
  #pragma gcc target( \
      "sse,sse2,sse3,ssse3,sse4,avx,avx2,fma,abm,mmx,popcnt,tune=native")
  const int INF = 1111111112;
  template <typename T>
  void amin(T &a, T b) {
    if (a > b) a = b;
  }
  bool f(int x, int s) {
    while (s) {
      if (s % 10 == x) return true;
      s /= 10;
    }
    return false;
  }
  void solve() {
    int n, k;
    cin >> n >> k;
    if (n == 1000000000) {
      if (k == 1)
        cout << 1111111111 << '\n';
      else
        cout << n << '\n';
      return;
    }
    vector<int> v;
    while (n) {
      v.emplace_back(n % 10);
      n /= 10;
    }
    v.emplace_back(0);
    reverse(v.begin(), v.end());
    int m = ((int)v.size()) - 1;
    int dp[11][11][2] = {};
    for (int i = 0; i < (11); ++i)
      for (int j = 0; j < (11); ++j) dp[i][j][0] = dp[i][j][1] = INF;
    dp[0][0][0] = 0;
    for (int i = 1; i <= m; ++i) {
      for (int j = 1; j <= k; ++j) {
        for (int u = 0; u < 10; ++u) {
          if (i == 1 && u == 0) continue;
          if (f(u, dp[i - 1][j][1]) && dp[i - 1][j][1] < INF)
            amin(dp[i][j][1], dp[i - 1][j][1] * 10 + u);
          if (f(u, dp[i - 1][j][0]) && u > v[i] && dp[i - 1][j][0] < INF)
            amin(dp[i][j][1], dp[i - 1][j][0] * 10 + u);
          if (f(u, dp[i - 1][j][0]) && u >= v[i] && dp[i - 1][j][0] < INF)
            amin(dp[i][j][0], dp[i - 1][j][0] * 10 + u);
          if (dp[i - 1][j - 1][1] < INF)
            amin(dp[i][j][1], dp[i - 1][j - 1][1] * 10 + u);
          if (u > v[i] && dp[i - 1][j - 1][0] < INF)
            amin(dp[i][j][1], dp[i - 1][j - 1][0] * 10 + u);
          if (u >= v[i] && dp[i - 1][j - 1][0] < INF)
            amin(dp[i][j][0], dp[i - 1][j - 1][0] * 10 + u);
        }
      }
    }
    int ans = INF;
    for (int i = 0; i <= k; ++i) amin(ans, min(dp[m][i][0], dp[m][i][1]));
    cout << ans << '\n';
  }
  int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T = 1;
    cin >> T;
    for (int i = 1; i <= (T); ++i) {
      solve();
    }
  }
  