#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
inline ll bit(ll x, ll y) { return (x & (1ll << y)); }
const ll mod = 998244353;
const ll N = 2e5 + 5;
const ll base = 31;
ll dd[20];
ll kq[20];
ll n, cnt = 0;
string st;
ll k, cur = 0;
void down(ll i) {
  if (dd[kq[i]] == 1) cnt--;
  dd[kq[i]]--;
  if (kq[i] == 9) {
    down(i - 1);
  } else {
    st[i] = char(ll((st[i] - '0') + 1) + '0');
    for (ll j = i + 1; j <= n; j++) st[j] = '0';
    cur = i - 1;
  }
  return;
}
void check(ll i) {
  ll x = st[i] - '0';
  if (dd[x]) {
    kq[i] = x;
    dd[x]++;
    return;
  }
  if (cnt < k) {
    kq[i] = st[i] - '0';
    if (dd[kq[i]] == 0) cnt++;
    dd[kq[i]]++;
    return;
  }
  bool kt = false;
  for (ll j = x; j <= 9; j++)
    if (dd[j]) {
      kt = true;
      kq[i] = j;
      dd[j]++;
      for (ll k = i + 1; k <= n; k++) st[k] = '0';
      return;
    }
  if (kt == false) {
    down(i - 1);
    return;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(0);
  ll t;
  cin >> t;
  while (t--) {
    cin >> st >> k;
    for (ll i = 0; i <= 9; i++) {
      dd[i] = 0;
      kq[i] = 0;
    }
    cnt = 0;
    cur = 1;
    n = st.size();
    st = ' ' + st;
    while (cur <= n) {
      check(cur);
      cur++;
    }
    for (ll i = 1; i <= n; i++) cout << kq[i];
    cout << endl;
  }
}

### Prompt

Your challenge is to write a cpp solution 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;
using ll = long long;
using ull = unsigned long long;
inline ll bit(ll x, ll y) { return (x & (1ll << y)); }
const ll mod = 998244353;
const ll N = 2e5 + 5;
const ll base = 31;
ll dd[20];
ll kq[20];
ll n, cnt = 0;
string st;
ll k, cur = 0;
void down(ll i) {
  if (dd[kq[i]] == 1) cnt--;
  dd[kq[i]]--;
  if (kq[i] == 9) {
    down(i - 1);
  } else {
    st[i] = char(ll((st[i] - '0') + 1) + '0');
    for (ll j = i + 1; j <= n; j++) st[j] = '0';
    cur = i - 1;
  }
  return;
}
void check(ll i) {
  ll x = st[i] - '0';
  if (dd[x]) {
    kq[i] = x;
    dd[x]++;
    return;
  }
  if (cnt < k) {
    kq[i] = st[i] - '0';
    if (dd[kq[i]] == 0) cnt++;
    dd[kq[i]]++;
    return;
  }
  bool kt = false;
  for (ll j = x; j <= 9; j++)
    if (dd[j]) {
      kt = true;
      kq[i] = j;
      dd[j]++;
      for (ll k = i + 1; k <= n; k++) st[k] = '0';
      return;
    }
  if (kt == false) {
    down(i - 1);
    return;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(0);
  ll t;
  cin >> t;
  while (t--) {
    cin >> st >> k;
    for (ll i = 0; i <= 9; i++) {
      dd[i] = 0;
      kq[i] = 0;
    }
    cnt = 0;
    cur = 1;
    n = st.size();
    st = ' ' + st;
    while (cur <= n) {
      check(cur);
      cur++;
    }
    for (ll i = 1; i <= n; i++) cout << kq[i];
    cout << endl;
  }
}

```