#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
  cerr << '{';
  __print(x.first);
  cerr << ", ";
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T& x) {
  int first = 0;
  cerr << '{';
  for (auto& i : x) cerr << (first++ ? ", " : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;
int K;
vector<int> digs;
int pre;
set<int> used;
int go(int rem, bool match) {
  if (rem == 0) {
    return pre;
  }
  int p = (int)(digs).size() - rem;
  int numTried = 0;
  for (int i = 0; i < (10); i++) {
    if (match && i < digs[p]) continue;
    if ((int)(used).size() == K && used.count(i) == 0) {
      continue;
    }
    if (numTried >= 2) continue;
    numTried++;
    pre *= 10;
    pre += i;
    bool curIn = used.count(i);
    if (!curIn) used.insert(i);
    int res = go(rem - 1, match && i == digs[p]);
    if (!curIn) used.erase(i);
    pre /= 10;
    if (res != -1) {
      return res;
    }
  }
  return -1;
}
void solve() {
  int N;
  cin >> N >> K;
  digs.clear();
  while (N) {
    digs.push_back(N % 10);
    N /= 10;
  }
  reverse(digs.begin(), digs.end());
  used.clear();
  pre = 0;
  int res = go((int)(digs).size(), true);
  if (res == -1) {
    cout << 1;
    for (int i = 0; i < ((int)(digs).size()); i++) cout << 0;
    cout << nl;
  } else
    cout << res << nl;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}

### 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>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
  cerr << '{';
  __print(x.first);
  cerr << ", ";
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T& x) {
  int first = 0;
  cerr << '{';
  for (auto& i : x) cerr << (first++ ? ", " : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;
int K;
vector<int> digs;
int pre;
set<int> used;
int go(int rem, bool match) {
  if (rem == 0) {
    return pre;
  }
  int p = (int)(digs).size() - rem;
  int numTried = 0;
  for (int i = 0; i < (10); i++) {
    if (match && i < digs[p]) continue;
    if ((int)(used).size() == K && used.count(i) == 0) {
      continue;
    }
    if (numTried >= 2) continue;
    numTried++;
    pre *= 10;
    pre += i;
    bool curIn = used.count(i);
    if (!curIn) used.insert(i);
    int res = go(rem - 1, match && i == digs[p]);
    if (!curIn) used.erase(i);
    pre /= 10;
    if (res != -1) {
      return res;
    }
  }
  return -1;
}
void solve() {
  int N;
  cin >> N >> K;
  digs.clear();
  while (N) {
    digs.push_back(N % 10);
    N /= 10;
  }
  reverse(digs.begin(), digs.end());
  used.clear();
  pre = 0;
  int res = go((int)(digs).size(), true);
  if (res == -1) {
    cout << 1;
    for (int i = 0; i < ((int)(digs).size()); i++) cout << 0;
    cout << nl;
  } else
    cout << res << nl;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}

```