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 mul[10];
  int bit(int mask, int i) { return (mask / mul[i]) % 10; }
  int t, t1;
  void solve() {
    int n, k;
    cin >> n >> k;
    if (n == 1e9) {
      if (k == 1) {
        cout << 1111111111;
      } else {
        cout << 1000000000;
      }
      cout << '\n';
      return;
    }
    bool c[10] = {0};
    int cnt = 0;
    int j = 8;
    while (bit(n, j) == 0) {
      j--;
    }
    int cur = 0;
    int mincs = 10;
    int res = 0;
    for (int i = 0; i <= j; i++) {
      res += mul[i] * (bit(n, j) + 1);
    }
    for (int i = j; i >= 0; i--) {
      if (!c[bit(n, i)]) {
        c[bit(n, i)] = 1;
        cnt++;
        mincs = min(mincs, bit(n, i));
      }
      cur *= 10;
      cur += bit(n, i);
      if (i > 0 && bit(n, i - 1) == 9) continue;
      if (cnt <= k) {
        if (i == 0) {
          res = n;
        } else {
          if (cnt < k) {
            if (cnt < k - 1) {
              res = min(res, cur * mul[i] + (bit(n, i - 1) + 1) * mul[i - 1]);
            } else {
              int tmp = cur * mul[i] + (bit(n, i - 1) + 1) * mul[i - 1];
              for (int z = 0; z < i - 1; z++) {
                tmp += mul[z] * min(mincs, bit(n, i - 1) + 1);
              }
              res = min(res, tmp);
            }
          }
          int z;
          for (z = bit(n, i - 1) + 1; z < 10; z++) {
            if (c[z]) {
              break;
            }
          }
          if (z < 10) {
            if (cnt < k) {
              res = min(res, cur * mul[i] + z * mul[i - 1]);
            } else {
              int tmp = cur * mul[i] + z * mul[i - 1];
              for (int y = 0; y < i - 1; y++) {
                tmp += mul[y] * mincs;
              }
              res = min(res, tmp);
            }
          }
        }
      }
    }
    cout << res << '\n';
  }
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    mul[0] = 1;
    for (int i = 1; i < 10; i++) {
      mul[i] = mul[i - 1] * 10;
    }
    cin >> t;
    for (t1 = 1; t1 <= t; t1++) {
      solve();
    }
  }
  