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;
  namespace Fread {
  const int SIZE = 1 << 26;
  char buf[SIZE], *S, *T;
  inline char getchar() {
    if (S == T) {
      T = (S = buf) + fread(buf, 1, SIZE, stdin);
      if (S == T) return '\n';
    }
    return *S++;
  }
  }  // namespace Fread
  namespace Fwrite {
  const int SIZE = 1 << 26;
  char buf[SIZE], *S = buf, *T = buf + SIZE;
  inline void flush() {
    fwrite(buf, 1, S - buf, stdout);
    S = buf;
  }
  inline void putchar(char c) {
    *S++ = c;
    if (S == T) flush();
  }
  struct NTR {
    ~NTR() { flush(); }
  } ztr;
  }  // namespace Fwrite
  namespace Fastio {
  struct Reader {
    template <typename T>
    Reader& operator>>(T& x) {
      char c = Fread ::getchar();
      T f = 1;
      while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = Fread ::getchar();
      }
      x = 0;
      while (c >= '0' && c <= '9') {
        x = x * 10 + (c - '0');
        c = Fread ::getchar();
      }
      x *= f;
      return *this;
    }
    Reader& operator>>(char& c) {
      c = Fread ::getchar();
      while (c == '\n' || c == ' ') c = Fread ::getchar();
      return *this;
    }
    Reader& operator>>(char* str) {
      int len = 0;
      char c = Fread ::getchar();
      while (c == '\n' || c == ' ') c = Fread ::getchar();
      while (c != '\n' && c != ' ') {
        str[len++] = c;
        c = Fread ::getchar();
      }
      str[len] = '\0';
      return *this;
    }
    Reader() {}
  } cin;
  const char endl = '\n';
  struct Writer {
    template <typename T>
    Writer& operator<<(T x) {
      if (x == 0) {
        Fwrite ::putchar('0');
        return *this;
      }
      if (x < 0) {
        Fwrite ::putchar('-');
        x = -x;
      }
      static int sta[45];
      int top = 0;
      while (x) {
        sta[++top] = x % 10;
        x /= 10;
      }
      while (top) {
        Fwrite ::putchar(sta[top] + '0');
        --top;
      }
      return *this;
    }
    Writer& operator<<(char c) {
      Fwrite ::putchar(c);
      return *this;
    }
    Writer& operator<<(char* str) {
      int cur = 0;
      while (str[cur]) Fwrite ::putchar(str[cur++]);
      return *this;
    }
    Writer& operator<<(const char* str) {
      int cur = 0;
      while (str[cur]) Fwrite ::putchar(str[cur++]);
      return *this;
    }
    Writer() {}
  } cout;
  }  // namespace Fastio
  int n, T, k;
  bitset<10> b;
  bool B(int n) {
    b.reset();
    while (n) {
      b[n % 10] = 1;
      n /= 10;
    }
    return b.count() <= k;
  }
  signed main() {
    for (Fastio ::cin >> T; T; T--) {
      Fastio ::cin >> n >> k;
      while (!B(n)) {
        int l = 1, r = n;
        while (!B(r)) l *= 10, r /= 10;
        l /= 10;
        n = (n / l + 1) * l;
      }
      Fastio ::cout << n << Fastio ::endl;
    }
  }
  