#include <bits/stdc++.h>
using namespace std;
typedef struct proceduce_st {
  string name;
  vector<string> parameterTypes;
  proceduce_st() {}
  proceduce_st(char line[]) {
    string s = "";
    for (int i = 0; line[i]; i++) {
      if (line[i] != ' ') s += line[i];
    }
    if (s.find("void") != string::npos)
      name = s.substr(4, s.find("(") - 4);
    else
      name = s.substr(0, s.find("("));
    s = s.substr(s.find("(") + 1, s.size() - s.find("(") - 2);
    while (s.find(",") != string::npos) {
      parameterTypes.push_back(s.substr(0, s.find(",")));
      s = s.substr(s.find(",") + 1);
    }
    parameterTypes.push_back(s);
  }
  void print() {
    cout << "Name: " << name << endl;
    for (string s : parameterTypes) {
      cout << s << endl;
    }
    cout << endl;
  }
} Proceduce;
int N, M, K;
Proceduce functions[1005];
char line[1000];
map<string, string> var;
bool match(Proceduce &a, Proceduce &template_) {
  if (a.name != template_.name) return false;
  if (a.parameterTypes.size() != template_.parameterTypes.size()) return false;
  for (int i = 0; i < template_.parameterTypes.size(); i++) {
    if (template_.parameterTypes[i] == "T") continue;
    if (template_.parameterTypes[i] != var[a.parameterTypes[i]]) return false;
  }
  return true;
}
int main() {
  scanf("%d", &N);
  gets(line);
  for (int i = 0; i < N; i++) {
    gets(line);
    functions[i] = Proceduce(line);
  }
  scanf("%d", &M);
  string v, t;
  for (int i = 0; i < M; i++) {
    cin >> t >> v;
    var[v] = t;
  }
  scanf("%d", &K);
  gets(line);
  for (int i = 0; i < K; i++) {
    gets(line);
    Proceduce f = Proceduce(line);
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      if (match(f, functions[j])) {
        cnt++;
      }
    }
    printf("%d\n", cnt);
  }
  return 0;
}
