Just use faster IO, and it should be OK, please remove your code.
For faster IO:
look at
import sys
for line in sys.stdin:
...
Dont use print for each case, but rather a unique print on a "\n".join object.
Make some tests on all that, it is very helpfull.