#include <bits/stdc++.h>
using namespace std;
vector<int> get_stacked_value(int n) {
  vector<int> result;
  while (n) {
    result.push_back(n % 10);
    n /= 10;
  }
  reverse(result.begin(), result.end());
  return result;
}
int get_int(vector<int> s) {
  int res = 0;
  for (int i : s) {
    s.pop_back();
    res *= 10;
    res += i;
  }
  return res;
}
bool is_valid(vector<int> &vec, int n, int k) {
  set<int> s;
  for (int i : vec) s.insert(i);
  int val = get_int(vec);
  return (s.size() <= k) && (val >= n);
}
int solve(int n, int k) {
  vector<int> n_vec = get_stacked_value(n);
  if (is_valid(n_vec, n, k)) return n;
  for (int i = n_vec.size() - 1; i >= 0; i--) {
    for (int add = 1; add + n_vec[i] < 10; add++) {
      vector<int> tmp_vec;
      tmp_vec = n_vec;
      tmp_vec[i] = (tmp_vec[i] + add);
      for (int j = i + 1; j < n_vec.size(); j++) tmp_vec[j] = 0;
      set<int> s(tmp_vec.begin(), tmp_vec.begin() + i + 1);
      if (s.size() > k)
        continue;
      else if (s.size() < k)
        for (int j = i + 1; j < n_vec.size(); j++) tmp_vec[j] = 0;
      else {
        int val = 9;
        for (int j = 0; j <= i; j++) val = min(val, tmp_vec[j]);
        for (int j = i + 1; j < n_vec.size(); j++) {
          tmp_vec[j] = val;
        }
      }
      const bool valid = is_valid(tmp_vec, n, k);
      if (valid) return get_int(tmp_vec);
    }
  }
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, k;
    cin >> n >> k;
    cout << solve(n, k) << endl;
  }
  return 0;
}

### 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;
vector<int> get_stacked_value(int n) {
  vector<int> result;
  while (n) {
    result.push_back(n % 10);
    n /= 10;
  }
  reverse(result.begin(), result.end());
  return result;
}
int get_int(vector<int> s) {
  int res = 0;
  for (int i : s) {
    s.pop_back();
    res *= 10;
    res += i;
  }
  return res;
}
bool is_valid(vector<int> &vec, int n, int k) {
  set<int> s;
  for (int i : vec) s.insert(i);
  int val = get_int(vec);
  return (s.size() <= k) && (val >= n);
}
int solve(int n, int k) {
  vector<int> n_vec = get_stacked_value(n);
  if (is_valid(n_vec, n, k)) return n;
  for (int i = n_vec.size() - 1; i >= 0; i--) {
    for (int add = 1; add + n_vec[i] < 10; add++) {
      vector<int> tmp_vec;
      tmp_vec = n_vec;
      tmp_vec[i] = (tmp_vec[i] + add);
      for (int j = i + 1; j < n_vec.size(); j++) tmp_vec[j] = 0;
      set<int> s(tmp_vec.begin(), tmp_vec.begin() + i + 1);
      if (s.size() > k)
        continue;
      else if (s.size() < k)
        for (int j = i + 1; j < n_vec.size(); j++) tmp_vec[j] = 0;
      else {
        int val = 9;
        for (int j = 0; j <= i; j++) val = min(val, tmp_vec[j]);
        for (int j = i + 1; j < n_vec.size(); j++) {
          tmp_vec[j] = val;
        }
      }
      const bool valid = is_valid(tmp_vec, n, k);
      if (valid) return get_int(tmp_vec);
    }
  }
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, k;
    cin >> n >> k;
    cout << solve(n, k) << endl;
  }
  return 0;
}

```