#include <bits/stdc++.h>
int _cer;
using namespace std;
char ch[11 + 2];
bool isok(char c) {
  return (c >= 'a' && c <= 'z') || (c == 'T') || (c >= '0' && c <= '9');
}
int read() {
  ch[0] = '\0';
  char c = getchar();
  while (!isok(c) && c != '\n') c = getchar();
  if (c == '\n') return -1;
  int k = 0;
  while (isok(c)) {
    ch[k++] = c;
    c = getchar();
  }
  ch[k++] = '\0';
  if (c == '\n') return -1;
  return 1;
}
int fnd() {
  if (ch[0] == 'i') return 1;
  if (ch[0] == 'T') return 0;
  if (ch[0] == 'd') return 2;
  if (ch[0] == 's') return 3;
  return -1;
}
int n, m, q;
char nam[1003][11 + 2];
vector<int> tp[1003];
map<string, int> mp;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    while (read() == -1)
      ;
    read();
    strcpy(nam[i], ch);
    while (read() == 1) {
      tp[i].push_back(fnd());
    }
    if (ch[0] != '\0') tp[i].push_back(fnd());
  }
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    while (read() == -1)
      ;
    int tpe = fnd();
    read();
    mp[string(ch)] = tpe;
  }
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    while (read() == -1)
      ;
    char cmp[11 + 2];
    strcpy(cmp, ch);
    vector<int> now;
    while (read() == 1) {
      now.push_back(mp[string(ch)]);
    }
    if (ch[0] != '\0') now.push_back(mp[string(ch)]);
    int cnt = 0;
    for (int j = 0; j < n; ++j) {
      if (strcmp(cmp, nam[j])) continue;
      if (tp[j].size() != now.size()) continue;
      bool flag = true;
      for (int l = 0; l < now.size(); ++l) {
        if (tp[j][l]) {
          flag &= (tp[j][l] == now[l]);
        }
      }
      if (flag) ++cnt;
    }
    printf("%d\n", cnt);
  }
  return 0;
}
