#include <bits/stdc++.h>
int parse1(char *s, char *s_, char ss[][16]) {
  int h, hl, hr, j, l, r, c;
  l = 0;
  while (s[l] != '(') l++;
  r = l;
  while (s[r] != ')') r++;
  hr = l - 1;
  while (s[hr] <= ' ') hr--;
  hl = hr;
  while (hl >= 0 && s[hl] > ' ') hl--;
  hl++;
  memcpy(s_, s + hl, hr - hl + 1);
  s_[hr - hl + 1] = '\0';
  c = 0;
  for (j = l + 1; j < r; j++)
    if (s[j] == ',') c++;
  l++;
  for (h = 0; h <= c; h++) {
    r = l;
    while (s[r] != ',' && s[r] != ')') r++;
    hl = l, hr = r - 1;
    while (s[hl] <= ' ') hl++;
    while (s[hr] <= ' ') hr--;
    memcpy(ss[h], s + hl, hr - hl + 1);
    ss[h][hr - hl + 1] = '\0';
    l = r + 1;
  }
  return c + 1;
}
int main() {
  static char s[128];
  static char procedures[1000][16], parameters[1000 + 1][5][16], tt[5][16];
  static int cnt[1000];
  static char types[1000][16], names[1000][16];
  int n, m, k, h, i, j;
  fgets(s, 128, stdin);
  n = atoi(s);
  for (i = 0; i < n; i++) {
    fgets(s, 128, stdin);
    cnt[i] = parse1(s, procedures[i], parameters[i]);
  }
  fgets(s, 128, stdin);
  m = atoi(s);
  for (j = 0; j < m; j++) {
    fgets(s, 128, stdin);
    sscanf(s, "%s%s", types[j], names[j]);
  }
  fgets(s, 128, stdin);
  k = atoi(s);
  while (k--) {
    static char name[16];
    int c, ans, yes;
    fgets(s, 128, stdin);
    c = parse1(s, name, parameters[n]);
    for (h = 0; h < c; h++)
      for (j = 0; j < m; j++)
        if (strcmp(names[j], parameters[n][h]) == 0) {
          strcpy(tt[h], types[j]);
          break;
        }
    ans = 0;
    for (i = 0; i < n; i++) {
      if (cnt[i] != c || strcmp(procedures[i], name) != 0) continue;
      yes = 1;
      for (h = 0; h < c; h++)
        if (strcmp(parameters[i][h], "T") != 0 &&
            strcmp(parameters[i][h], tt[h]) != 0) {
          yes = 0;
          break;
        }
      if (yes) ans++;
    }
    printf("%d\n", ans);
  }
  return 0;
}
