Alice is a very brilliant student. He considers '4' and '7' as Magic numbers. The numbers containing only magic numbers are also magical. Given a magic number N ,he wants to know what could be the next magical number greater than the given number.
-----Input-----
First line of input contains number of test cases T. For each test case, there is exits only one line containing a magic number N. 
-----Output-----
For each test case,  output a single line containing the next greater magical number.
-----Constraints-----
1<=T<=1000
4<= N<=10^100
-----Example-----
Input:
2
4
47
Output:
7
74
#!/usr/bin/env python

def process(N):
    if  not '4' in N:
        return '4' * (len(N) + 1)
    i = N.rindex('4')
    return N[:i] + '7' + '4' * len(N[i+1:])

def main():
    T = int(input().strip())
    for t in range(T):
        N = input().strip()
        print(process(N))

main()


#!/usr/bin/env python

def process(N):
    if  not '4' in N:
        return '4' * (len(N) + 1)
    i = N.rindex('4')
    return N[:i] + '7' + '4' * len(N[i+1:])

def main():
    T = int(input().strip())
    for t in range(T):
        N = input().strip()
        print(process(N))

main()

