This problem is a bit tricky.
The key point is that A can go to B in two ways.
The first way is to send it directly.
The second way is to put it in the stack.
So simply put, just simulate and see how it goes.
When you find it's impossible, then it's NO.