P21279 [CodeChef] CHDIGER
普及
题目描述

Chef Leonardo has a decimal integer $N$ and a non-zero decimal digit $d$. $N$ does not contain the digit zero; specifically, $N$ should always be treated as a decimal integer without leading zeroes.
Chef likes $d$ and does not like any other digit, so he decided to change $N$. He may apply the following operation any number of times (including zero): append the digit $d$ to the decimal representation of $N$ ($d$ becomes the least significant digit of $N$), then remove one occurrence of one digit from the decimal representation of $N$.
Chef has already changed a lot of numbers and he got bored. Now, he wants to know the smallest possible value of $N$ which can be obtained by performing the operation described above. Can you help him?

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first and only line of each test case contains two space-separated integers $N$ and $d$.

-----Output-----
For each test case, print a single line containing one integer - the minimum number Chef can obtain.

-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le N \le 10^{18}$
- $N$ does not contain the digit $0$
- $1 \le d \le 9$

-----Subtasks-----
Subtask #1 (40 points):
- $1 \le T \le 100$
- $1 \le N \le 10^9$
Subtask #2 (60 points): original constraints

-----Example Input-----
3
35 4
42 4
24 9

-----Example Output-----
34
24
24

-----Explanation-----
Example case 1: Chef can remove the digit $5$ and add $4$ to the end of $N$. Then, $N$ becomes $34$.
Example case 2: Chef can remove the digit $4$ from the beginning of $N$, add $4$ to the end of $N$ and get $N = 24$.
Example case 3: Chef cannot make his number any smaller.

输入格式

见题面

输出格式

见题面

样例输入
['3', '35 4', '42 4', '24 9']
样例输出
['34', '24', '24']