#include <bits/stdc++.h>
using namespace std;
const int NN = 1009;
string types[] = {"int", "string", "double", "T"};
vector<string> f_name;
vector<int> G[NN];
void go_is_space(int &index, string &A) {
  while (A[index] == ' ') index++;
}
int find_type(string &A) {
  for (int i = 0; i < 4; i++)
    if (A == types[i]) return i;
  return -1;
}
void fun_1(int index, string &A, int I) {
  string name;
  go_is_space(index, A);
  while (A[index] != ' ' && A[index] != '(') {
    name += A[index];
    index++;
  }
  f_name.push_back(name);
  go_is_space(index, A);
  index++;
  while (A[index] != ')') {
    go_is_space(index, A);
    name.clear();
    while (A[index] != ' ' && A[index] != ',' && A[index] != ')') {
      name += A[index];
      index++;
    }
    go_is_space(index, A);
    G[I].push_back(find_type(name));
    if (A[index] == ',') index++;
    go_is_space(index, A);
  }
}
string p_name;
vector<int> t_aux;
map<string, string> Map;
void fun_2(int index, string &A) {
  p_name;
  p_name = "";
  t_aux.clear();
  go_is_space(index, A);
  while (A[index] != ' ' && A[index] != '(') {
    p_name += A[index];
    index++;
  }
  go_is_space(index, A);
  index++;
  string name;
  while (A[index] != ')') {
    go_is_space(index, A);
    name.clear();
    while (A[index] != ' ' && A[index] != ',' && A[index] != ')') {
      name += A[index];
      index++;
    }
    go_is_space(index, A);
    t_aux.push_back(find_type(Map[name]));
    if (A[index] == ',') index++;
    go_is_space(index, A);
  }
}
int main() {
  ios::sync_with_stdio(false);
  int N;
  string s;
  cin >> N;
  getline(cin, s);
  for (int i = 0; i < N; i++) {
    getline(cin, s);
    int fi = s.find("void");
    fi += 4;
    fun_1(fi, s, i);
  }
  int M;
  cin >> M;
  getline(cin, s);
  for (int i = 0; i < M; i++) {
    getline(cin, s);
    stringstream ss;
    ss << s;
    string A, B;
    ss >> A >> B;
    Map[B] = A;
  }
  int K;
  cin >> K;
  getline(cin, s);
  for (int i = 0; i < K; i++) {
    getline(cin, s);
    fun_2(0, s);
    int cnt = 0;
    for (int j = 0; j < N; j++) {
      if (p_name == f_name[j] && ((int)G[j].size()) == ((int)t_aux.size())) {
        bool f = true;
        for (int r = 0; r < ((int)G[j].size()); r++) {
          if (t_aux[r] == 3) continue;
          if (G[j][r] == 3) continue;
          if (t_aux[r] != G[j][r]) {
            f = false;
            break;
          }
        }
        if (f) cnt++;
      }
    }
    printf("%d\n", cnt);
  }
}
