Under construction

Still being built. Things may change

queuestack

Interview

Time limit1sMemory limit1024 MB

Summary
Each of N positions is a queue or stack holding one value; insert M values one at a time and print the value that exits the end of the line.
Level

Medium5 of 10

Topics
Queue, Stack, Implementation, Simulation
Solved
No attempts yet

Problem

Dohyeon, idly enjoying his vacation, suddenly came up with an interesting data structure. He named it queuestack.

The structure of queuestack is as follows. Data structures numbered 11, 22, ..., NN (each a queue or a stack) are lined up, and each data structure holds one element.

queuestack works as follows.

  • Read x0x_0.
  • Insert x0x_0 into data structure 11, then pop an element from data structure 11. Call the popped element x1x_1.
  • Insert x1x_1 into data structure 22, then pop an element from data structure 22. Call the popped element x2x_2.
  • ...
  • Insert xN−1x_{N-1} into data structure NN, then pop an element from data structure NN. Call the popped element xNx_N.
  • Return xNx_N.

Dohyeon takes a sequence CC of length MM and inserts its elements into queuestack one by one from front to back. The results of previous insertions remain. (See Sample 11.)

Given the elements to insert into queuestack, write a program that outputs the return value for each insertion.

Input

The first line gives the number of data structures NN that make up queuestack. (1≤N≤100 0001 \leq N \leq 100\,000)

The second line gives a sequence AA of length NN. If data structure ii is a queue then Ai=0A_i = 0, and if it is a stack then Ai=1A_i = 1.

The third line gives a sequence BB of length NN. BiB_i is the element held in data structure ii. (1≤Bi≤1 000 000 0001 \leq B_i \leq 1\,000\,000\,000)

The fourth line gives the length MM of the sequence to insert. (1≤M≤100 0001 \leq M \leq 100\,000)

The fifth line gives a sequence CC of length MM containing the elements to insert into queuestack. (1≤Ci≤1 000 000 0001 \leq C_i \leq 1\,000\,000\,000)

All numbers in the input are integers.

Output

For each element of sequence CC inserted into queuestack in order, output the return value, separated by spaces.

Examples2

  1. Example 1

    Input
    4
    0 1 1 0
    1 2 3 4
    3
    2 4 7
    
    Expected output
    4 1 2
    
  2. Example 2

    Input
    5
    1 1 1 1 1
    1 2 3 4 5
    3
    1 3 5
    
    Expected output
    1 3 5