Ciel Numbers III

All submissions for this problem are available.
Tomya is a girl. She loves Chef Ciel very much.
Tomya also loves Ciel numbers.
The definition of Ciel numbers are following.
Ciel numbers are defined as the positive integers k
such that d(k, 8) ≥ d(k, 5) ≥ d(k, 3) and d(k, i) = 0 for all i = 0, 1, 2, 4, 6, 7, 9,
where d(k, i) denotes the number of the digit i in the decimal representation of the integer k.
For example, the first few Ciel numbers are 8, 58, 85, 88, 358, 385, 538, 583, 588, 835, 853, 858, 885, 888, ....
Now Tomya defines the xpseudo Ciel numbers as follows for nonnegative integers x:
 A positive integer k is a 0pseudo Ciel numbers if and only if k is a Ciel number.

For x ≥ 1, a positive integer k is a xpseudo Ciel numbers if and only if
k is not a ypseudo Ciel number for all 0 ≤ y < x
and, there exists a (x1)pseudo Ciel number s such that s have the same number of digits as k
and exactly one digit differ from k in their decimal notations.
For example,
583, 58588, 355388, and 5358388 are 0pseudo Ciel numbers, and
523, 58558, 355308, and 5354388 are 1pseudo Ciel numbers, and
123, 55558, 353308, and 5314388 are 2pseudo Ciel numbers, and
124, 55555, 333308, and 1314388 are 3pseudo Ciel numbers.
In this problem, your task is to find the largest xpseudo Ciel number less than or equal to N.
Input
The first line contains an integer T, the number of test cases.
Then T test cases follow.
Each test case contains 2 integers x and N in one line.
Output
For each test case, print the largest xpseudo Ciel number less than or equal to N.
If there are no such numbers, print "1" (excluding quotes).
Constraints
1 ≤ T ≤ 10000 (10^{4})
0 ≤ x ≤ 1000000000 (10^{9})
1 ≤ N < 10^{100000}
The sum of the length of N in one judge input file does not exceed 1000000 (10^{6}).
Sample Input
5 0 58600 2 128 1 5354389 1 1 2 1
Sample Output
58588 128 5354388 1 1
Author:  laycurse 
Editorial  http://discuss.codechef.com/problems/CIELNUM3 
Tags  cook24, laycurse, medium 
Date Added:  4072012 
Time Limit:  0.386777 sec 
Source Limit:  50000 Bytes 
Languages:  C, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions