GERALD07 help!

This problem:

asks for finding connected components over a range and queries, Here is what I tried:
It gives WA and I cant figure out the mistake, also the editorial suggests sqrt method and link cut tree despite this being a simple dfs problem. Came across this problem while reading this article: