#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n, k, k_c, i, left_max, smallest_uniq;
  vector<int> v, uniq;
  cin >> t;
  while (t) {
    cin >> n >> k;
    k_c = k;
    v.clear();
    uniq.clear();
    while (n > 0) {
      v.push_back(n % 10);
      n = n / 10;
    }
    i = v.size() - 1;
    for (; - 1 < i && k > 0; i--) {
      if (find(uniq.begin(), uniq.end(), v[i]) == uniq.end()) {
        uniq.push_back(v[i]);
        k -= 1;
      }
    }
    if (i > -1) {
      left_max = *max_element(v.begin(), v.begin() + i);
      smallest_uniq = *min_element(uniq.begin(), uniq.end());
    }
    int smallest_high = 10, flag = 0, target;
    while (i > -1) {
      if (flag)
        v[i] = smallest_uniq;
      else if (find(uniq.begin(), uniq.end(), v[i]) == uniq.end()) {
        for (int j = 0; j < uniq.size(); j++) {
          if (v[i] < uniq[j] && uniq[j] < smallest_high) {
            smallest_high = uniq[j];
            flag = 1;
          }
        }
        if (flag == 0) {
          target = v[i + 1];
          if (find(uniq.begin(), uniq.end(), target + 1) != uniq.end()) {
            i += 1;
            v[i] += 1;
            if (find(v.begin() + i, v.end(), target) == v.end())
              smallest_uniq = 0;
          } else {
            k = k_c + 1;
            for (int j = i + 1; k > k_c; j += 1) {
              k = k_c + 1;
              while (v[j] < 10 && k > k_c) {
                v[j] += 1;
                uniq.clear();
                k = 0;
                for (int z = j; z < v.size(); z++)
                  if (find(uniq.begin(), uniq.end(), v[z]) == uniq.end()) {
                    uniq.push_back(v[z]);
                    k += 1;
                  }
                smallest_uniq = *min_element(uniq.begin(), uniq.end());
                if (k < k_c) smallest_uniq = 0;
              }
              i = j;
            }
          }
          flag = 1;
        } else
          v[i] = smallest_high;
      }
      i -= 1;
    }
    for (i = v.size() - 1; - 1 < i; i--) cout << v[i];
    cout << endl;
    t--;
  }
  return 0;
}

### Prompt

Create a solution in cpp for 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;
int main() {
  int t, n, k, k_c, i, left_max, smallest_uniq;
  vector<int> v, uniq;
  cin >> t;
  while (t) {
    cin >> n >> k;
    k_c = k;
    v.clear();
    uniq.clear();
    while (n > 0) {
      v.push_back(n % 10);
      n = n / 10;
    }
    i = v.size() - 1;
    for (; - 1 < i && k > 0; i--) {
      if (find(uniq.begin(), uniq.end(), v[i]) == uniq.end()) {
        uniq.push_back(v[i]);
        k -= 1;
      }
    }
    if (i > -1) {
      left_max = *max_element(v.begin(), v.begin() + i);
      smallest_uniq = *min_element(uniq.begin(), uniq.end());
    }
    int smallest_high = 10, flag = 0, target;
    while (i > -1) {
      if (flag)
        v[i] = smallest_uniq;
      else if (find(uniq.begin(), uniq.end(), v[i]) == uniq.end()) {
        for (int j = 0; j < uniq.size(); j++) {
          if (v[i] < uniq[j] && uniq[j] < smallest_high) {
            smallest_high = uniq[j];
            flag = 1;
          }
        }
        if (flag == 0) {
          target = v[i + 1];
          if (find(uniq.begin(), uniq.end(), target + 1) != uniq.end()) {
            i += 1;
            v[i] += 1;
            if (find(v.begin() + i, v.end(), target) == v.end())
              smallest_uniq = 0;
          } else {
            k = k_c + 1;
            for (int j = i + 1; k > k_c; j += 1) {
              k = k_c + 1;
              while (v[j] < 10 && k > k_c) {
                v[j] += 1;
                uniq.clear();
                k = 0;
                for (int z = j; z < v.size(); z++)
                  if (find(uniq.begin(), uniq.end(), v[z]) == uniq.end()) {
                    uniq.push_back(v[z]);
                    k += 1;
                  }
                smallest_uniq = *min_element(uniq.begin(), uniq.end());
                if (k < k_c) smallest_uniq = 0;
              }
              i = j;
            }
          }
          flag = 1;
        } else
          v[i] = smallest_high;
      }
      i -= 1;
    }
    for (i = v.size() - 1; - 1 < i; i--) cout << v[i];
    cout << endl;
    t--;
  }
  return 0;
}

```