[Time limit exceeded] Lucky lucky number

can anyone please tell me why my program is exceeding it’s time limit.
here’s the link to the solution.
http://www.codechef.com/viewsolution/3117046

n <= 10^9. You cannot use a loop upto 10^9 to find the ans it takes a lot of time much more then 1 sec.
Also there are t <= 1000 testcases
So in total 10^12. Its impossible to finish in time if you simply iterate upto n.

to keep number smallest, maximize digit 4, which means maximize n’s divisibility by 7.