Under construction

Still being built. Things may change

Multi-Item Preference Survey (Large)

Interview

Time limit3sMemory limit512 MB

Summary
Given n students' subject, fruit, and color choices, each query asks how many students match its pattern, where '-' matches any value.
Level

Easy3 of 10

Topics
Hash map, Implementation
Solved
No attempts yet

Problem

nn students were surveyed about their preferences as follows. Each student must choose exactly one option for each of the three survey items below.

  • Favorite subject: one of 'kor', 'eng', 'math'
  • Favorite fruit: one of 'apple', 'pear', 'orange'
  • Favorite color: one of 'red', 'blue', 'green'

Process mm queries in order on the preferences of the nn students. Each query is as follows.

  • The query format is 'subject fruit color'.
  • subject is one of 'kor', 'eng', 'math', or '-'.
  • fruit is one of 'apple', 'pear', 'orange', or '-'.
  • color is one of 'red', 'blue', 'green', or '-'.
  • '-' means that condition is not considered.
  • Print the number of students among the nn whose preferences match subject fruit color.

Given the preferences of the nn students and mm queries, process the queries in order. nn and mm are very large, so help the students who are stuck on them.

Input

The first line contains nn and mm, separated by a space.

Each of the next nn lines contains one student's preferences: favorite subject, fruit, and color, separated by spaces.

Each of the next mm lines contains one query: subject, fruit, and color, separated by spaces.

Output

Print the result of each query on its own line, in order, as an integer.

Constraints

1 ≤ nn, mm ≤ 100,000

Examples1

  1. Example 1

    Input
    5 4
    kor apple red
    kor pear blue
    eng apple red
    eng orange blue
    math apple green
    kor apple red
    kor - blue
    eng - -
    - - red
    
    Expected output
    1
    1
    2
    2