SPOJ - DQUERY TLE Python

I am getting TLE. What is the problem? Please check it.

Problem statement

Code