Lottery Number

5

1 votes
Implementation, 2D dynamic programming, Recursion
Problem

One day Mr. Seth went to a shop to play the game of Lottery. So he scratched the Lottery card and found a number N. Lottery number K is defined as the number which only consists of digits 3 or 5 or both.

The Lottery number is always least greater (i.e. nearest to N) than the number N.

Help Mr. Seth to find the Lottery Number.

 

Input:
First line of input contains number of test cases T. Each test case contains a single number N.

 

Output:
For each test case, print the Lottery Number in a separate line.

 

Constraints:
1<=T<=1000
1<=N<=10100

Time Limit: 0.2
Memory Limit: 256
Source Limit:
Editor Image

?