#include <bits/stdc++.h>
const int maxn = 1e3 + 3;
int n, m, q;
char buf[200];
void parseWord(char *&s) {
  while (!isalnum(*s)) ++s;
  char *b = buf;
  for (; isalnum(*s); ++s) *(b++) = *s;
  *b = 0;
}
struct func {
  char name[16];
  int al, a[5];
  bool match(char *na, int *p, int pl) {
    int t = 0;
    if (strcmp(na, name) != 0 || pl != al) return false;
    for (int i = 0; i < pl; ++i) {
      if (a[i] && a[i] != p[i]) return false;
    }
    return true;
  }
} fp[maxn];
int fpl;
bool parseArg(char *&s, int *a, int &al) {
  while (*s != '(' && *s) ++s;
  if (!*s) return false;
  al = 0;
  for (++s; *s;) {
    if (isalnum(*s)) {
      parseWord(s);
      if (strcmp(buf, "T") == 0) {
        a[al++] = 0;
      } else if (strcmp(buf, "int") == 0) {
        a[al++] = 1;
      } else if (strcmp(buf, "string") == 0) {
        a[al++] = 2;
      } else if (strcmp(buf, "double") == 0) {
        a[al++] = 3;
      } else {
        return false;
      }
    } else if (*s == ')') {
      ++s;
      return true;
    } else {
      ++s;
    }
  }
  return false;
}
void parseFunc(char *s) {
  while (*s) {
    parseWord(s);
    if (strcmp(buf, "void") == 0) break;
  }
  if (!*s) return;
  parseWord(s);
  strcpy(fp[fpl].name, buf);
  if (parseArg(s, fp[fpl].a, fp[fpl].al)) ++fpl;
}
std::unordered_map<std::string, int> vl;
void parseVar(char *s) {
  parseWord(s);
  int t;
  if (strcmp(buf, "int") == 0) {
    t = 1;
  } else if (strcmp(buf, "string") == 0) {
    t = 2;
  } else if (strcmp(buf, "double") == 0) {
    t = 3;
  } else {
    return;
  }
  parseWord(s);
  vl[buf] = t;
}
bool parsePArg(char *&s, int *a, int &al) {
  while (*s != '(' && *s) ++s;
  if (!*s) return false;
  al = 0;
  for (++s; *s;) {
    if (isalnum(*s)) {
      parseWord(s);
      a[al++] = vl[buf];
    } else if (*s == ')') {
      ++s;
      return true;
    } else {
      ++s;
    }
  }
  return false;
}
char pn[200];
int pa[5], pl;
void parseP(char *s) {
  parseWord(s);
  if (!*s) {
    puts("0");
    return;
  }
  strcpy(pn, buf);
  if (!parsePArg(s, pa, pl)) {
    puts("0");
    return;
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (fp[i].match(pn, pa, pl)) ++ans;
  }
  printf("%d\n", ans);
}
char ln[512];
int main() {
  gets(ln);
  sscanf(ln, "%d", &n);
  for (int i = 0; i < n; ++i) {
    gets(ln);
    parseFunc(ln);
  }
  gets(ln);
  sscanf(ln, "%d", &m);
  for (int i = 0; i < m; ++i) {
    gets(ln);
    parseVar(ln);
  }
  gets(ln);
  sscanf(ln, "%d", &q);
  for (int i = 0; i < q; ++i) {
    gets(ln);
    parseP(ln);
  }
  return 0;
}
