Question about Time Complexity of Program

I was working through MooTube, and couldn’t really figure out how to do it under the time limit. So I just submitted what I thought was the naive solution to the problem, which is to iterate through all values of Q, run a dfs, and just add it to ans. Somehow it got full credit, and I was wondering how this happened?

The max for Q is 5000, and the max number of nodes is 5000 (Which means 4999 edges). So wouldn’t the time complexity for this program be O(Q * (V + E)), and give me a TLE? Or am I missing something with this program? My solution is the exact same as the one linked in the USACO Guide Module

Q*(V+E) is not that large.

Oh I’m sorry I’m so stupid lol. For whatever reason I thought it would be 5e8.