Source: Codeforces 832D
How many cases arise from each query?
Analyse the time complexity of your algorithm and estimate its running time.