#include <bits/stdc++.h>
using namespace std;
const int length = 1111;
char str[length / 10], str2[length / 10];
void input(vector<string> &ans) {
  if (ans.size()) ans.clear();
  int i = 0;
  char c = getchar();
  while (c != '\n' && c != -1) {
    if (c == '(' || c == ')' || c == ',' || c == '\t')
      str[i++] = ' ';
    else
      str[i++] = c;
    c = getchar();
  }
  str[i] = '\0';
  char *p = str;
  while (sscanf(p, "%s", str2) != EOF) {
    ans.push_back((string)str2);
    while (*p == ' ') p++;
    p += strlen(str2);
  }
}
vector<string> fun[length];
map<string, string> mp_type;
bool is_appeared(int id, vector<string> compare_with) {
  if (fun[id].size() != compare_with.size()) return false;
  for (int i = 0; i < compare_with.size(); ++i) {
    if (fun[id][i] != "T" && fun[id][i] != compare_with[i]) return false;
  }
  return true;
}
int main() {
  vector<string> in;
  int n, m, i, j, cnt;
  scanf("%d", &n);
  getchar();
  for (i = 0; i < n; ++i) {
    input(in);
    for (j = 1; j < in.size(); ++j) fun[i].push_back(in[j]);
  }
  scanf("%d", &m);
  getchar();
  while (m--) {
    input(in);
    mp_type[in[1]] = in[0];
  }
  scanf("%d", &m);
  getchar();
  for (i = 0; i < m; ++i) {
    input(in);
    for (j = 1; j < in.size(); ++j) in[j] = mp_type[in[j]];
    cnt = 0;
    for (j = 0; j < n; ++j)
      if (is_appeared(j, in)) cnt++;
    printf("%d\n", cnt);
  }
  return 0;
}
