IPSC Logo

Internet Problem Solving Contest

IPSC 2003

Problem H – Hordes of Bacteria

There are really rush days in the Institute of Parasitic and Symbiotic Creatures (IPSC) this week. They have just discovered new species of bacteria and want to collect as much information about it as possible. Unfortunately the bacteria breed so quickly that one is hardly able to monitor their expansion. So far the institute staff knows only the following few facts about these bacteria:

Your task is to compute how many bacteria will live in each of the colonies after T seconds, provided there is exactly one bacterium in each of them at the beginning.

Input file specification

On the first line there are three integers N, M and T separated by one space. The second line contains one integer K. M lines follow, each of them describes one of the rules according to which the bacteria behave. The description consists of one string and exactly two integers separated by one space. Possible descriptions and their meanings follow.

Output file specification

For each colony print one line with the number of bacteria that will live in this colony after T seconds.

Example

Input file
8 6 11
7
reproduce 2 5 
copy 4 2
die 1 0
merry-go-round 0 0
teleport 5 3
swap 0 2
Output file
1
0
0
0
0
4
4
1

Note

The colonies evolved as follows:

Second Change Colonies
0th1st2nd3rd4th5th6th7th
beginning 11111111
1streproduce 2 5 11511111
2ndcopy 4 2 11516111
3rddie 1 0 10516111
4thmerry-go-round 0 0 11051611
5thteleport 5 3 11001411
6thswap 0 2 01101411
7threproduce 2 5 01501411
8thcopy 4 2 01506411
9thdie 1 0 00506411
10thmerry-go-round 0 0 10050641
11thteleport 5 3 10000441