Please help me to find bug in my code

here is submission link

Looks like your solution calls top() on an empty stack with the sample test input:

Compiling prince_000-CHFQUEUE.cpp
+ g++ -std=c++14 prince_000-CHFQUEUE.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
^[[A^[[A+ set +x
[~/devel/hackerrank/otherpeoples]>echo "4 2
1 2 1 2" | ./a.out
/usr/include/c++/7/bits/stl_stack.h:200: std::stack<_Tp, _Sequence>::reference std::stack<_Tp, _Sequence>::top() [with _Tp = int; _Sequence = std::__debug::deque<int, std::allocator<int> >; std::stack<_Tp, _Sequence>::reference = int&]: Assertion '__builtin_expect(!this->empty(), true)' failed.
Aborted (core dumped)

please, can you explain how it can be improved.
I still unable to find mistake