Help required in SPOJ JNEXT (using stack and queues)

can someone help me how to do this problem using stacks and queues in cpp?? thank you in advance