I can’t see anything that would make it O(N \times Q) or anything like that, so I suspect it’s only just timing out by a small amount, most likely due to your unnecessary use of a std::set.
(I have a testcase here that takes your solution 3.83 seconds on my laptop, but my laptop is slower than Codechef’s servers, so I’m not sure if that counts … )
Without using sets there may be a chance of putting duplicate values which eventually gives 0 everytime. Therefore to avoid that i am using set. set operations usually takes O(1) right?
Think it through - I’ve already suggested one think that is probably causing it to TLE!
A hint, to get you on the right track - what is my_set doing? Is there a way of doing it without using something as slow and expensive as a std::set? The Editorial solution doesn’t use one!