#include <bits/stdc++.h>
using namespace std;
set<int> se;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    se.clear();
    scanf("%d%d", &n, &k);
    int mark = n;
    int b[20];
    int tcnt = 0;
    while (n) {
      b[++tcnt] = n % 10;
      n /= 10;
    }
    int vis[10] = {0};
    int nowcnt = 0;
    int i;
    int markstart;
    for (i = tcnt; i >= 1; i--) {
      if (vis[b[i]] == 0) {
        nowcnt++;
        vis[b[i]] = 1;
        if (nowcnt <= k) se.insert(b[i]);
        if (nowcnt == k) markstart = i;
      }
      if (nowcnt > k) {
        if (b[i] > (*(--se.end()))) {
          for (int j = i + 1; j < markstart; j++) {
            if (b[j] < (*(--se.end()))) {
              b[j] = (*se.upper_bound(b[j]));
              i = j - 1;
              goto endd2;
            }
          }
          goto endd;
        } else {
          b[i] = (*se.lower_bound(b[i]));
          i--;
          break;
        }
      }
    }
  endd2:;
    for (; i >= 1; i--) b[i] = (*se.begin());
    if (nowcnt <= k)
      printf("%d\n", mark);
    else {
      for (int i = tcnt; i >= 1; i--) printf("%d", b[i]);
      cout << endl;
    }
    continue;
  endd:;
    se.clear();
    memset(vis, 0, sizeof(vis));
    nowcnt = 0;
    for (i = tcnt; i >= 1; i--) {
      if (vis[b[i]] == 0) {
        nowcnt++;
        vis[b[i]] = 1;
        if (nowcnt < k) se.insert(b[i]);
      }
      if (nowcnt == k) {
        se.insert(b[i] + 1);
        if (se.size() < k) se.insert(0);
        b[i]++;
        i--;
        break;
      }
    }
    for (; i >= 1; i--) b[i] = (*se.begin());
    for (int i = tcnt; i >= 1; i--) printf("%d", b[i]);
    cout << endl;
  }
  return 0;
}

### Prompt

Generate a CPP solution to the following problem:

It is a simplified version of problem F2. 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 ≤ 2).

Output

For each test case output on a separate line x — the minimum k-beautiful integer such that x ≥ n.

Example

Input


4
1 1
221 2
177890 2
998244353 1


Output


1
221
181111
999999999

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
set<int> se;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    se.clear();
    scanf("%d%d", &n, &k);
    int mark = n;
    int b[20];
    int tcnt = 0;
    while (n) {
      b[++tcnt] = n % 10;
      n /= 10;
    }
    int vis[10] = {0};
    int nowcnt = 0;
    int i;
    int markstart;
    for (i = tcnt; i >= 1; i--) {
      if (vis[b[i]] == 0) {
        nowcnt++;
        vis[b[i]] = 1;
        if (nowcnt <= k) se.insert(b[i]);
        if (nowcnt == k) markstart = i;
      }
      if (nowcnt > k) {
        if (b[i] > (*(--se.end()))) {
          for (int j = i + 1; j < markstart; j++) {
            if (b[j] < (*(--se.end()))) {
              b[j] = (*se.upper_bound(b[j]));
              i = j - 1;
              goto endd2;
            }
          }
          goto endd;
        } else {
          b[i] = (*se.lower_bound(b[i]));
          i--;
          break;
        }
      }
    }
  endd2:;
    for (; i >= 1; i--) b[i] = (*se.begin());
    if (nowcnt <= k)
      printf("%d\n", mark);
    else {
      for (int i = tcnt; i >= 1; i--) printf("%d", b[i]);
      cout << endl;
    }
    continue;
  endd:;
    se.clear();
    memset(vis, 0, sizeof(vis));
    nowcnt = 0;
    for (i = tcnt; i >= 1; i--) {
      if (vis[b[i]] == 0) {
        nowcnt++;
        vis[b[i]] = 1;
        if (nowcnt < k) se.insert(b[i]);
      }
      if (nowcnt == k) {
        se.insert(b[i] + 1);
        if (se.size() < k) se.insert(0);
        b[i]++;
        i--;
        break;
      }
    }
    for (; i >= 1; i--) b[i] = (*se.begin());
    for (int i = tcnt; i >= 1; i--) printf("%d", b[i]);
    cout << endl;
  }
  return 0;
}

```