Under construction

Still being built. Things may change

Book Search Program

Time limit3sMemory limit1024 MB

Summary
For each nested AND, OR, NOT search expression, count the books in the DB whose name, author, and tags satisfy every condition.
Level

Medium4 of 10

Topics
Recursion, String, Implementation
Solved
No attempts yet

Problem

The PIMM game development club has a custom of graduates leaving their major textbooks in the club room. In the past, PIMM ran a book lending system so that new members could freely use used textbooks. However, last year, when Minkyu was club president, too many books piled up in the club room, and the lending system became hard to manage. Hearing this, Jonghyun started building a book search program that helps people find the books they need right away.

The book search program stores books and search requests as strings. The DB stores a string <book> that represents a book. <book> can be written as follows in Backus-Naur Form (BNF). In the table, the left side of "::=" is a symbol and the right side is an expression.

<book> ::= <name> " " <author> | <name> " " <author> " " <tags>
<tags> ::= <tag> | <tag> " " <tags>
<name> ::= <str>
<author> ::= <str>
<tag> ::= <str>

The symbols and expressions in this table have the following properties.

  • <str> is a string of at most 20 characters made of letters, digits, '-', and '_'.
  • <name> is the name of the book.
  • <author> is the author of the book.
  • <tag> is a feature of the book.
  • No <tag> is repeated within a single <tags>.

For example, if the string "1984 George_Orwell novel SF" is in the DB, it means a book named 1984 by George_Orwell has two features, novel and SF.

No two books in the DB can have the same <name> and <author> together. Note that several books can share the same <name> or the same <author>.

A search request <search> is also given as a string.

<method> ::= <AND> | <OR> | <NOT> | <nameCorrect> | <nameInclude> | <authorCorrect> | <tagCorrect>
<methods> ::= <method> | <method> ", " <methods>

<AND> ::= "A(" <methods> ")"
<OR> ::= "O(" <methods> ")"
<NOT> ::= "N(" <method> ")"

<nameCorrect> ::= "n:" <str>
<nameInclude> ::= "ni:" <str>
<authorCorrect> ::= "a:" <str>
<tagCorrect> ::= "t:" <str>

<search> ::= <methods>

The symbols and expressions in this table have the following properties.

  • <str> is a string of at most 20 characters made of letters, digits, '-', and '_'.
  • <nameCorrect> returns true if the expression <str> equals the <name> of the given <book>, and false otherwise.
  • <nameInclude> returns true if the expression <str> is contained in the <name> of the given <book>, and false otherwise.
  • <authorCorrect> returns true if the expression <str> equals the <author> of the given <book>, and false otherwise.
  • <tagCorrect> returns true if at least one <tag> in the <tags> of the given <book> equals the expression <str>, and false otherwise.
  • <AND> returns true if every <method> in <methods> returns true, and false otherwise.
  • <OR> returns true if at least one <method> in <methods> returns true, and false otherwise.
  • <NOT> returns false if the <method> returns true, and true otherwise.
  • <search> returns the number of <book>s in the DB for which every <method> in <methods> returns true.

Input

The first line contains NN, the number of <book> strings in the DB. The next NN lines each contain one <book>.

The next line contains MM, the number of search strings <search>. The next MM lines each contain one <search>.

Invalid strings are never given as input.

Output

For each <search>, print the result on its own line.

Constraints

1leN,Mle2001 \\le N, M \\le 200

Each <book> in the input is at most 200200 characters long.

Each <search> in the input is at most 1,5001\\,500 characters long.

All numbers in the input are integers.

Examples1

  1. Example 1

    Input
    4
    Gullivers_Travels Jonathan_Swift novel fairytale
    1984 George_Orwell novel SF
    Toji Pak_Kyong-ni novel korean
    Cosmos Carl_Sagan science cosmology
    5
    n:Gulliver
    ni:Gulliver
    N(ni:Gulliver)
    a:Pak_Kyong-ni, t:novel
    O(A(t:novel, N(t:fairytale)), A(t:fairytale, N(t:novel)))
    
    Expected output
    0
    1
    3
    1
    2