Under construction

Still being built. Things may change

Magic Cards

Interview

Time limit3sMemory limit2048 MB

Summary
Given K cards that each list numbers from 1 to N, and each friend's yes/no answers, find the number each friend picked, or 0 if it is not unique.
Level

Medium4 of 10

Topics
Hash map, Bit manipulation, Array
Solved
No attempts yet

Problem

Chansu and Junsu are friends at International College of Programming Convergence. One day, Chansu met Junsu and said that "I'll do a magic trick for you. Pick any number between 11 and 1212, and don't tell me your number. Just keep it in your mind." Junsu chose 1111 in mind. Chansu then showed Junsu the following four cards one by one, asking "Is there your number in this card?" at each time. Junsu answered "Yes, yes, no, yes" in this order. After Chansu made some theatrical gestures with his arms and legs for a while, he finally shouted, "I've got your number. It is 1111." Junsu was surprised because it was exactly the number he had kept in mind. Chansu did not reveal the secret of the trick. He only said, "These cards have a great magic power, so they can read your mind and tell me something only in a magical language, which only I can understand." How does this work? Can you figure out the secret? Now, you are to write a program that answers the number in your friends' minds. The trick can be generalized as follows. You have KK magic cards, and each card has exactly MM integers between 11 and NN, possibly with some repeats. You perform the trick on FF friends. From the yes/no sequences of the FF friends, you pick out the correct numbers.

Input

The first line contains four integers NN, KK, MM, and FF (1≤N≤500 0001 \le N \le 500\,000, 1≤K≤1001 \le K \le 100, 1≤M≤5 0001 \le M \le 5\,000, 1≤F≤50 0001 \le F \le 50\,000). Each of the following KK lines contains MM integers between 11 and NN, which are the numbers written on one magic card. Each of the following FF lines contains a string of length KK over Y and N. A Y means "yes" and an N means "no". All answers are correctly given according to the numbers chosen in mind.

Output

Print exactly FF lines. The ii-th line contains the number in the ii-th friend's mind. If the number of the ii-th friend cannot be identified uniquely, print 0 on that line.

Examples3

  1. Example 1

    Input
    12 4 6 3
    1 9 7 11 3 5
    2 10 3 6 7 11
    4 5 6 7 6 12
    8 11 10 9 12 9
    YYNY
    NNNY
    YNNN
    
    Expected output
    11
    8
    1
    
  2. Example 2

    Input
    13 4 6 4
    1 9 7 11 3 5
    2 10 3 6 7 11
    4 5 6 7 6 12
    8 11 10 9 12 9
    YYNY
    NNNY
    YNNN
    NNNN
    
    Expected output
    11
    8
    1
    13
    
  3. Example 3

    Input
    14 4 6 4
    1 9 7 11 3 5
    2 10 3 6 7 11
    4 5 6 7 6 12
    8 11 10 9 12 9
    YYNY
    NNNY
    YNNN
    NNNN
    
    Expected output
    11
    8
    1
    0