#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long cl(long double a) {
  if (a > (long long)a) {
    return (long long)a + 1;
  } else {
    return (long long)a;
  }
}
long long flr(long double a) {
  if (a < 0.0) {
    return (long long)a - 1;
  }
  return (long long)a;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long pw(long long n, long long k) {
  if (k == 0) return 1;
  long long t = pw(n, k / 2);
  long long ans = (t * t) % mod;
  if (k % 2)
    return (ans * n) % mod;
  else
    return ans;
}
long long ans = 1e18;
long long n, k;
void mn(string s) {
  set<char> st;
  for (auto ch : s) st.insert(ch);
  if (s.length() < n || st.size() > k) return;
  long long num = stoll(s);
  ans = min(ans, num);
}
void solve() {
  string s;
  cin >> s;
  cin >> k;
  set<char> st;
  n = s.length();
  ans = 1e18;
  string cur = "";
  for (long long i = 0; i < n; i++) {
    if (st.size() > k) break;
    string nw = cur;
    if (s[i] == '9') {
      if (i == 0) {
        if (k == 1) {
          for (long long i = 0; i < n + 1; i++) nw += '1';
        } else {
          nw += '1';
          for (long long i = 0; i < n; i++) nw += '0';
        }
        mn(nw);
      }
      st.insert('9');
      cur += '9';
      continue;
    } else if (i == n - 1) {
      char ch = s[i];
      if (st.size() < k || st.find(ch) != st.end()) {
        cur += ch;
        mn(cur);
      } else {
        auto it = st.upper_bound(s[i]);
        if (it != st.end()) {
          cur += *it;
          mn(cur);
        }
      }
      break;
    }
    char ch = s[i] + 1;
    if (st.size() == k) {
      auto it = st.lower_bound(ch);
      if (it != st.end()) {
        nw += *it;
        while (nw.length() < s.length()) {
          nw += *st.begin();
        }
        mn(nw);
      }
    } else {
      long long op = 0;
      ch = s[i] + 1;
      if (st.find(ch) == st.end()) {
        op++;
      }
      nw += ch;
      char nxt;
      if (st.find('0') != st.end() || st.size() + op < k) {
        nxt = '0';
      } else {
        nxt = ch;
        if (st.size() > 0) nxt = min(nxt, *st.begin());
      }
      while (nw.length() < s.length()) {
        nw += nxt;
      }
      mn(nw);
    }
    st.insert(s[i]);
    cur += s[i];
  }
  if (cur.length() == s.length()) mn(cur);
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  t = 1;
  cin >> t;
  while (t--) solve();
}

### Prompt

Construct a Cpp code solution to the problem outlined:

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;
const long long mod = 1e9 + 7;
long long cl(long double a) {
  if (a > (long long)a) {
    return (long long)a + 1;
  } else {
    return (long long)a;
  }
}
long long flr(long double a) {
  if (a < 0.0) {
    return (long long)a - 1;
  }
  return (long long)a;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long pw(long long n, long long k) {
  if (k == 0) return 1;
  long long t = pw(n, k / 2);
  long long ans = (t * t) % mod;
  if (k % 2)
    return (ans * n) % mod;
  else
    return ans;
}
long long ans = 1e18;
long long n, k;
void mn(string s) {
  set<char> st;
  for (auto ch : s) st.insert(ch);
  if (s.length() < n || st.size() > k) return;
  long long num = stoll(s);
  ans = min(ans, num);
}
void solve() {
  string s;
  cin >> s;
  cin >> k;
  set<char> st;
  n = s.length();
  ans = 1e18;
  string cur = "";
  for (long long i = 0; i < n; i++) {
    if (st.size() > k) break;
    string nw = cur;
    if (s[i] == '9') {
      if (i == 0) {
        if (k == 1) {
          for (long long i = 0; i < n + 1; i++) nw += '1';
        } else {
          nw += '1';
          for (long long i = 0; i < n; i++) nw += '0';
        }
        mn(nw);
      }
      st.insert('9');
      cur += '9';
      continue;
    } else if (i == n - 1) {
      char ch = s[i];
      if (st.size() < k || st.find(ch) != st.end()) {
        cur += ch;
        mn(cur);
      } else {
        auto it = st.upper_bound(s[i]);
        if (it != st.end()) {
          cur += *it;
          mn(cur);
        }
      }
      break;
    }
    char ch = s[i] + 1;
    if (st.size() == k) {
      auto it = st.lower_bound(ch);
      if (it != st.end()) {
        nw += *it;
        while (nw.length() < s.length()) {
          nw += *st.begin();
        }
        mn(nw);
      }
    } else {
      long long op = 0;
      ch = s[i] + 1;
      if (st.find(ch) == st.end()) {
        op++;
      }
      nw += ch;
      char nxt;
      if (st.find('0') != st.end() || st.size() + op < k) {
        nxt = '0';
      } else {
        nxt = ch;
        if (st.size() > 0) nxt = min(nxt, *st.begin());
      }
      while (nw.length() < s.length()) {
        nw += nxt;
      }
      mn(nw);
    }
    st.insert(s[i]);
    cur += s[i];
  }
  if (cur.length() == s.length()) mn(cur);
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  t = 1;
  cin >> t;
  while (t--) solve();
}

```