Hoodies at coding blocks, tle

I did Hoodies at coding blocks using a stack, it is taking o(n) time for both insertion and deletion and o(n) space too. Here is my solution, using linked list would have made the D o(1) but query E will still take O(n). so what should be the optimized way to do this question, I couldn’t think of any way of doing it with the queue?

Don’t use stack in this question use queue and see that queue is fifo or lilo last in last out and stack is lifo

Make a boolean function is same course than only use queue