#include <bits/stdc++.h>
#pragma warning(disable : 4996)
int T, n, k, a, b, c;
char care[100];
char str[100];
int dif(char* str) {
  int is = 0;
  int n = strlen(str);
  int ans = 0;
  int bulk[10];
  for (int i = 0; i < 10; i++) {
    bulk[i] = 0;
  }
  for (is; is < n; is++) {
    if (bulk[str[is] - '0'] == 0) ans++;
    bulk[str[is] - '0']++;
  }
  return ans;
}
bool hasbig(int* a, char b) {
  for (int i = b + 1; i < 10; i++) {
    if (a[i]) {
      return true;
    }
  }
  return false;
}
char nextbig(int* a, char b) {
  for (int i = b + 1; i < 10; i++) {
    if (a[i]) {
      return i;
    }
  }
  return 10;
}
char least(int* a) {
  for (int i = 0; i < 10; i++) {
    if (a[i]) {
      return i;
    }
  }
  return 10;
}
bool full(char* a, char* b, int k) {
  int is = 0;
  int n = strlen(a);
  b[n] = '\0';
  int bulk[10];
  for (int i = 0; i < 10; i++) {
    bulk[i] = 0;
  }
  int omit = 0;
  for (is; is < n; is++) {
    b[is] = a[is];
    if (bulk[a[is] - '0'] == 0) omit++;
    bulk[a[is] - '0']++;
    if (omit == k) break;
  }
  for (is; is < n; is++) {
    if (bulk[a[is] - '0']) {
      b[is] = a[is];
    } else {
      break;
    }
  }
  if (is == n) {
    return true;
  }
  if (hasbig(bulk, a[is] - '0')) {
    b[is] = nextbig(bulk, a[is] - '0') + '0';
    for (int i = is + 1; i < n; i++) {
      b[i] = least(bulk) + '0';
    }
  } else {
    return false;
  }
}
void take(char* a, char* b, int c) {
  int is = 0;
  int n = strlen(a);
  b[n] = '\0';
  int bulk[10];
  for (int i = 0; i < 10; i++) {
    bulk[i] = 0;
  }
  int omit = 0;
  for (is; is < n; is++) {
    b[is] = a[is];
    if (bulk[a[is] - '0'] == 0) omit++;
    bulk[a[is] - '0']++;
    if (omit == k + 1) break;
  }
  bulk[b[is] - '0'] = 0;
  is--;
  while (bulk[b[is] - '0'] != 1) {
    if (hasbig(bulk, b[is] - '0')) {
      b[is] = nextbig(bulk, b[is] - '0') + '0';
      for (is++; is < n; is++) {
        b[is] = least(bulk) + '0';
      }
      return;
    } else {
      bulk[b[is] - '0']--;
      is--;
    }
  }
  if (b[is] == '9') {
    if (bulk[b[is - 1] - '0'] == 1) {
      bulk[b[is - 1] - '0']--;
      b[is - 1]++;
      bulk[b[is - 1] - '0']++;
      for (is; is < n; is++) {
        b[is] = '0';
      }
      return;
    } else {
      bulk[b[is - 1] - '0']--;
      b[is - 1]++;
      bulk[b[is - 1] - '0']++;
      for (is; is < n; is++) {
        b[is] = least(bulk) + '0';
      }
      return;
    }
  } else {
    bulk[b[is] - '0']--;
    b[is]++;
    bulk[b[is] - '0']++;
    if (bulk[b[is] - '0'] == 1) {
      for (is++; is < n; is++) {
        b[is] = least(bulk) + '0';
      }
    } else {
      for (is++; is < n; is++) {
        b[is] = '0';
      }
    }
  }
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &k);
    sprintf(care, "%d", n);
    a = dif(care);
    if (a <= k) {
      printf("%d\n", n);
      continue;
    }
    if (full(care, str, k)) {
      printf("%s\n", str);
      continue;
    } else {
      take(care, str, k);
      printf("%s\n", str);
    }
  }
}

### 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>
#pragma warning(disable : 4996)
int T, n, k, a, b, c;
char care[100];
char str[100];
int dif(char* str) {
  int is = 0;
  int n = strlen(str);
  int ans = 0;
  int bulk[10];
  for (int i = 0; i < 10; i++) {
    bulk[i] = 0;
  }
  for (is; is < n; is++) {
    if (bulk[str[is] - '0'] == 0) ans++;
    bulk[str[is] - '0']++;
  }
  return ans;
}
bool hasbig(int* a, char b) {
  for (int i = b + 1; i < 10; i++) {
    if (a[i]) {
      return true;
    }
  }
  return false;
}
char nextbig(int* a, char b) {
  for (int i = b + 1; i < 10; i++) {
    if (a[i]) {
      return i;
    }
  }
  return 10;
}
char least(int* a) {
  for (int i = 0; i < 10; i++) {
    if (a[i]) {
      return i;
    }
  }
  return 10;
}
bool full(char* a, char* b, int k) {
  int is = 0;
  int n = strlen(a);
  b[n] = '\0';
  int bulk[10];
  for (int i = 0; i < 10; i++) {
    bulk[i] = 0;
  }
  int omit = 0;
  for (is; is < n; is++) {
    b[is] = a[is];
    if (bulk[a[is] - '0'] == 0) omit++;
    bulk[a[is] - '0']++;
    if (omit == k) break;
  }
  for (is; is < n; is++) {
    if (bulk[a[is] - '0']) {
      b[is] = a[is];
    } else {
      break;
    }
  }
  if (is == n) {
    return true;
  }
  if (hasbig(bulk, a[is] - '0')) {
    b[is] = nextbig(bulk, a[is] - '0') + '0';
    for (int i = is + 1; i < n; i++) {
      b[i] = least(bulk) + '0';
    }
  } else {
    return false;
  }
}
void take(char* a, char* b, int c) {
  int is = 0;
  int n = strlen(a);
  b[n] = '\0';
  int bulk[10];
  for (int i = 0; i < 10; i++) {
    bulk[i] = 0;
  }
  int omit = 0;
  for (is; is < n; is++) {
    b[is] = a[is];
    if (bulk[a[is] - '0'] == 0) omit++;
    bulk[a[is] - '0']++;
    if (omit == k + 1) break;
  }
  bulk[b[is] - '0'] = 0;
  is--;
  while (bulk[b[is] - '0'] != 1) {
    if (hasbig(bulk, b[is] - '0')) {
      b[is] = nextbig(bulk, b[is] - '0') + '0';
      for (is++; is < n; is++) {
        b[is] = least(bulk) + '0';
      }
      return;
    } else {
      bulk[b[is] - '0']--;
      is--;
    }
  }
  if (b[is] == '9') {
    if (bulk[b[is - 1] - '0'] == 1) {
      bulk[b[is - 1] - '0']--;
      b[is - 1]++;
      bulk[b[is - 1] - '0']++;
      for (is; is < n; is++) {
        b[is] = '0';
      }
      return;
    } else {
      bulk[b[is - 1] - '0']--;
      b[is - 1]++;
      bulk[b[is - 1] - '0']++;
      for (is; is < n; is++) {
        b[is] = least(bulk) + '0';
      }
      return;
    }
  } else {
    bulk[b[is] - '0']--;
    b[is]++;
    bulk[b[is] - '0']++;
    if (bulk[b[is] - '0'] == 1) {
      for (is++; is < n; is++) {
        b[is] = least(bulk) + '0';
      }
    } else {
      for (is++; is < n; is++) {
        b[is] = '0';
      }
    }
  }
}
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &k);
    sprintf(care, "%d", n);
    a = dif(care);
    if (a <= k) {
      printf("%d\n", n);
      continue;
    }
    if (full(care, str, k)) {
      printf("%s\n", str);
      continue;
    } else {
      take(care, str, k);
      printf("%s\n", str);
    }
  }
}

```