#include <bits/stdc++.h>
using namespace std;
template <typename T, size_t sz>
using ar = array<T, sz>;
template <typename TOStream, typename T1, typename T2>
inline TOStream &operator<<(TOStream &s, const pair<T1, T2> &p) {
  s << "[" << p.first << ", " << p.second << "]";
  return s;
}
template <typename TOStream, typename TContainer>
inline TOStream &operator<<(TOStream &s, const TContainer &container) {
  s << "[";
  bool first = true;
  for (const auto &val : container) {
    if (!first) {
      s << ", ";
    } else {
      first = false;
    }
    s << val;
  }
  s << "]";
  return s;
}
template <typename TIStream, typename T1, typename T2>
inline TIStream &operator>>(TIStream &s, pair<T1, T2> &p) {
  s >> p.first >> p.second;
  return s;
}
template <typename TOStream>
inline TOStream &operator<<(TOStream &s, const pair<int, int> &p) {
  s << "(" << p.first << ", " << p.second << ")";
  return s;
}
bool search(int k, int i, const string &s, const set<char> &digs, string &r,
            set<char> &taken) {
  if (i >= int(s.size())) {
    return true;
  }
  if (k == taken.size()) {
    auto iter = taken.lower_bound(s[i]);
    if (*iter == s[i]) {
      r.push_back(*iter);
      if (search(k, i + 1, s, digs, r, taken)) {
        return true;
      }
      r.pop_back();
      iter++;
    }
    if (iter != taken.end()) {
      r.push_back(*iter);
      for (int j = i + 1; j < int(s.size()); j++) {
        r.push_back(*taken.begin());
      }
      return true;
    }
    return false;
  }
  r.push_back(s[i]);
  taken.insert(s[i]);
  if (search(k, i + 1, s, digs, r, taken)) {
    return true;
  }
  r.pop_back();
  taken.erase(s[i]);
  if (s[i] < '9') {
    r.push_back(s[i] + 1);
    taken.insert(s[i] + 1);
    for (int j = i + 1; j < int(s.size()); j++) {
      r.push_back(taken.size() == k ? *taken.begin() : '0');
    }
    return true;
  }
  return false;
}
void solve() {
  string s;
  int k;
  cin >> s >> k;
  string r;
  set<char> digs(s.begin(), s.end());
  set<char> taken;
  search(k, 0, s, digs, r, taken);
  cout << r << endl;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}

### Prompt

Please create a solution in cpp to the following problem:

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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, size_t sz>
using ar = array<T, sz>;
template <typename TOStream, typename T1, typename T2>
inline TOStream &operator<<(TOStream &s, const pair<T1, T2> &p) {
  s << "[" << p.first << ", " << p.second << "]";
  return s;
}
template <typename TOStream, typename TContainer>
inline TOStream &operator<<(TOStream &s, const TContainer &container) {
  s << "[";
  bool first = true;
  for (const auto &val : container) {
    if (!first) {
      s << ", ";
    } else {
      first = false;
    }
    s << val;
  }
  s << "]";
  return s;
}
template <typename TIStream, typename T1, typename T2>
inline TIStream &operator>>(TIStream &s, pair<T1, T2> &p) {
  s >> p.first >> p.second;
  return s;
}
template <typename TOStream>
inline TOStream &operator<<(TOStream &s, const pair<int, int> &p) {
  s << "(" << p.first << ", " << p.second << ")";
  return s;
}
bool search(int k, int i, const string &s, const set<char> &digs, string &r,
            set<char> &taken) {
  if (i >= int(s.size())) {
    return true;
  }
  if (k == taken.size()) {
    auto iter = taken.lower_bound(s[i]);
    if (*iter == s[i]) {
      r.push_back(*iter);
      if (search(k, i + 1, s, digs, r, taken)) {
        return true;
      }
      r.pop_back();
      iter++;
    }
    if (iter != taken.end()) {
      r.push_back(*iter);
      for (int j = i + 1; j < int(s.size()); j++) {
        r.push_back(*taken.begin());
      }
      return true;
    }
    return false;
  }
  r.push_back(s[i]);
  taken.insert(s[i]);
  if (search(k, i + 1, s, digs, r, taken)) {
    return true;
  }
  r.pop_back();
  taken.erase(s[i]);
  if (s[i] < '9') {
    r.push_back(s[i] + 1);
    taken.insert(s[i] + 1);
    for (int j = i + 1; j < int(s.size()); j++) {
      r.push_back(taken.size() == k ? *taken.begin() : '0');
    }
    return true;
  }
  return false;
}
void solve() {
  string s;
  int k;
  cin >> s >> k;
  string r;
  set<char> digs(s.begin(), s.end());
  set<char> taken;
  search(k, 0, s, digs, r, taken);
  cout << r << endl;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}

```