#include <bits/stdc++.h>
using namespace std;
map<string, vector<string> > mp;
map<string, char> tp;
bool get(int x) {
  while (x--) fgetc(stdin);
  return 1;
}
int main() {
  int n, m, k;
  char c;
  scanf("%d", &n);
  fgetc(stdin);
  for (int i = 1; i <= n; i++) {
    string proname, protype;
    while ((c = fgetc(stdin)) == ' ')
      ;
    for (int j = 1; j <= 4; j++) c = fgetc(stdin);
    while (c == ' ') c = fgetc(stdin);
    while (c != ' ' && c != '(') proname += c, c = fgetc(stdin);
    while (c == ' ') c = fgetc(stdin);
    while (c != ')') {
      while ((c = fgetc(stdin)) == ' ' || c == ',')
        ;
      if (c == 'T') protype += 'T';
      if (c == 'i') protype += 'i', get(2);
      if (c == 's') protype += 's', get(5);
      if (c == 'd') protype += 'd', get(5);
    }
    mp[proname].push_back(protype);
    while ((c = fgetc(stdin)) != '\n')
      ;
  }
  scanf("%d", &m);
  fgetc(stdin);
  for (int i = 1; i <= m; i++) {
    string valname;
    char valtype;
    while ((c = fgetc(stdin)) == ' ')
      ;
    valtype = c;
    while ((c = fgetc(stdin)) != ' ')
      ;
    while ((c = fgetc(stdin)) == ' ')
      ;
    while (c != '\n' && c != ' ') valname += c, c = fgetc(stdin);
    tp[valname] = valtype;
    while (c != '\n') c = fgetc(stdin);
  }
  scanf("%d", &k);
  fgetc(stdin);
  while (k--) {
    string proname, protype;
    while ((c = fgetc(stdin)) == ' ')
      ;
    while (c != ' ' && c != '(') proname += c, c = fgetc(stdin);
    while (c != '(') c = fgetc(stdin);
    for (c = fgetc(stdin); c != ')';) {
      while (c == ' ' || c == ',') c = fgetc(stdin);
      string valname;
      while (c != ' ' && c != ',' && c != ')') valname += c, c = fgetc(stdin);
      if (valname.size() == 0) break;
      protype += tp[valname];
      if (c == ')') break;
    }
    vector<string> v = mp[proname];
    int cnt = 0;
    for (int i = 0; i < (int)v.size(); i++) {
      if (v[i].size() != protype.size()) continue;
      bool f = true;
      for (int j = 0; j < (int)v[i].size(); j++) {
        if (v[i][j] != 'T' && v[i][j] != protype[j]) {
          f = false;
          break;
        }
      }
      if (f) cnt++;
    }
    printf("%d\n", cnt);
    while ((c = fgetc(stdin)) != '\n')
      ;
  }
  return 0;
}
