#P10697. [SNCPC2024] 消失的数字

[SNCPC2024] 消失的数字

Problem Description

uuku is learning digit DP.

But one day, he found that among the nine digits 1,2,,91,2,\ldots,9, the digit xx disappeared. That is, all numbers that contain the digit xx disappeared.

This made him very nervous, because it has a big impact on counting how many numbers there are.

Now, he wants you to help him find the rank of the number nn among the natural numbers that did not disappear, when ordered from small to large.

Input Format

This problem has multiple test cases. The first line contains an integer TT (1T1051 \leq T \leq 10^5), which indicates the number of test cases.

For each test case:

There is one line with two integers n,xn, x (0n1018,1x90 \leq n \leq 10^{18}, 1 \leq x \leq 9), separated by a space, with meanings as described in the statement.

The testdata guarantees that nn does not contain the digit xx.

Output Format

For each test case, output one integer per line, which is the rank of nn.

5
9 4
99 7
12345678 9
9475632111234123 8
998244353114514 7

9
81
6053445
1758041005111510
205404686678741

Hint

For the first test case in the sample, the sequence of natural numbers becomes 0,1,2,3,5,6,7,8,90,1,2,3,5,6,7,8,9, where 99 is the 99-th.

Translated by ChatGPT 5