1602. GPLT 2021 Problem 2-1 (包装机)

You are given an automatic packaging machine:

There are N tracks in the machine and some items are placed. There is a basket under the track. When someone press the button of a track, the corresponding piston (活塞) pushes the leftmost item into the basket. When button 0 is pressed, the manipulator (机械手) will grab an item at the top of the basket and put it on the assembly line. The following fugure shows the state of the packaging machine after pressing buttons 3, 2, 3, 0, 1, 2 and 0 sequentially.

In a special case, due to limited capacity of the basket, when the basket is full, and the button of a track is still pressed, the system will first grab an item at the top of the basket and put it on the assembly line, then push the item of the corresponding track down. In addition, if a track is empty, nothing will happen by pressing the corresponding button (note that even if the basket is full, we will not grab items from the basket in this case). Similarly, if the basket is empty, nothing will happen by pressing button 0.

Now a series of button pressed are given. Please list the items on the assembly line.

输入

The first line contains 3 integers N \ (1 \leq N \leq 100), M \ (1 \leq M \leq 1000), S_{max} \ (1 \leq S_{max} \leq 100), the number of tracks, the initial number of items placed on each track and the capacity of the basket.

For the next N lines, each line contains a string which consists of M uppercase letters, the ith letter on the jth line (of the overall input) describes the name of the ith item on track j-1.

The last line contains a sequence of integers o_1,o_2,... \ (0 \leq o_i \leq N), terminated by the input of an integer -1 (which you don't need to process) denoting the button pressed. The total number of integers in this line will not exceed 10^5.

输出

Output the items on the assembly line in order, see sample output for format.

样例

标准输入 复制文本
3 4 4
GPLT
PATA
OMSA
3 2 3 0 1 2 0 2 2 0 -1
标准输出 复制文本
MATA

提示

All testdatas are randomly generated.

登录以提交代码。
单点时限 1 秒
内存限制 128 MB
提交 92
通过 33