you are getting TLE due to for loop of low base function because-
let input number is 10^10 then you called function and this value is copied into z then z is not less than 6 ( of course ) hence value if lim is (10^10)/2 i.e 5 * 10^9 and now you are iterating from b=2 to 5 * 10^9 which is large and also number of test cases are 10^3 hence total complexity is 5*10^12 which is much large to run within 3 sec. and also there are some bug in your code but first fix it.
try to optimize you algorithm and replace this loop.
at last you wrote printf(“0”) here you will never get input zero so no need to write it.