F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Acesrc and Good Numbers

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 713    Accepted Submission(s): 381


Problem Description
Acesrc is a famous mathematician at Nanjing University second to none. Playing with interesting numbers is his favorite. Today, he finds a manuscript when cleaning his room, which reads

... Let $f(d, n)$ denote the number of occurrences of digit $d$ in decimal representations of integers $1, 2, 3, \cdots, n$. The function has some fantastic properties ...

... Obviously, there exist some nonnegative integers $k$, such that $f(d, k) = k$, and I decide to call them $d$-good numbers ...

... I have found all $d$-good numbers not exceeding $10^{1000}$, but the paper is too small to write all these numbers ...

Acesrc quickly recollects all $d$-good numbers he found, and he tells Redsun a question about $d$-good numbers: what is the maximum $d$-good number no greater than $x$? However, Redsun is not good at mathematics, so he wants you to help him solve this problem.
 

Input
The first line of input consists of a single integer $q$ $(1 \leq q \leq 1500)$, denoting the number of test cases. Each test case is a single line of two integers $d$ $(1 \leq d \leq 9)$ and $x$ $(0 \leq x \leq 10^{18})$.
 

Output
For each test case, print the answer as a single integer in one line. Note that $0$ is trivially a $d$-good number for arbitrary $d$.
 

Sample Input
3 1 1 1 199999 3 0
 

Sample Output
1 199990 0
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 08:12:56, Gzip enabled