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;
  int dp[10];
  int find_greater(int a) {
    int index = -1;
    for (int i = a + 1; i < 10; i++) {
      if (dp[i]) {
        index = i;
        break;
      }
    }
    return index;
  }
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
      int n, k;
      cin >> n >> k;
      memset(dp, 0, sizeof(dp));
      string ans = "";
      int mnm = 10, mxm = -1;
      vector<int> v;
      int temp = n, cnt = 0;
      while (temp != 0) {
        v.push_back(temp % 10);
        temp /= 10;
        cnt++;
      }
      reverse(v.begin(), v.end());
      int x = 0;
      for (int i = 0; i < cnt; i++) {
        if (x == 1) {
          if (k)
            mnm = 0;
          else {
            mnm = 10;
            for (int j = 0; j < 10; j++) {
              if (dp[j]) {
                mnm = j;
                break;
              }
            }
          }
          ans += char(mnm + '0');
          continue;
        }
        if (dp[v[i]] == 0) {
          if (k == 0) {
            int j;
            int temp = find_greater(v[i]);
            if (temp != -1) {
              ans += char(temp + '0');
              x = 1;
            } else {
              int j = v[i];
              while (i >= 0) {
                i--;
                temp = find_greater(v[i]);
                ans.pop_back();
                dp[v[i]]--;
                if (dp[v[i]] == 0) k++;
                if (temp == -1 && k || (k && temp > v[i] + 1)) {
                  k--;
                  dp[v[i] + 1] = 1;
                  ans += char(v[i] + 1 + '0');
                  x = 1;
                  break;
                } else if (temp != -1) {
                  dp[temp]++;
                  ans += char(temp + '0');
                  x = 1;
                  break;
                }
              }
            }
          } else {
            ans += char(v[i] + '0');
            dp[v[i]] = 1;
            k--;
            mnm = min(mnm, v[i]);
          }
        } else {
          ans += char(v[i] + '0');
          dp[v[i]]++;
        }
      }
      cout << ans << endl;
    }
  }
  