#include <bits/stdc++.h>
using namespace std;
struct var {
  char name[20], type[20];
} v[1010];
int vcnt;
struct func {
  char name[20];
  char para[50][20];
  int paracnt;
} f[1010];
int fcnt;
int n;
char str[110], name[110], para[110][110], cnt;
int main() {
  scanf("%d", &fcnt);
  gets(str);
  for (int i = 1; i <= fcnt; ++i) {
    gets(str);
    int j = 0, l = 0, len = strlen(str);
    while (str[j] != 'd') ++j;
    ++j;
    while (!isalpha(str[j]) && !isdigit(str[j])) ++j;
    while (isalpha(str[j]) || isdigit(str[j])) f[i].name[l++] = str[j++];
    f[i].name[l] = '\0';
    j++;
    while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j;
    while (j < len) {
      l = 0;
      f[i].paracnt++;
      while (isalpha(str[j]) || isdigit(str[j]))
        f[i].para[f[i].paracnt][l++] = str[j++];
      f[i].para[f[i].paracnt][l] = '\0';
      while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j;
    }
  }
  scanf("%d", &vcnt);
  gets(str);
  for (int i = 1; i <= vcnt; ++i) {
    gets(str);
    int j = 0, len = strlen(str), l = 0;
    while (!isalpha(str[j]) && !isdigit(str[j])) ++j;
    while (isdigit(str[j]) || isalpha(str[j])) v[i].type[l++] = str[j++];
    v[i].type[l] = '\0';
    while (!isalpha(str[j]) && !isdigit(str[j])) ++j;
    l = 0;
    while (isdigit(str[j]) || isalpha(str[j])) v[i].name[l++] = str[j++];
    v[i].name[l] = '\0';
  }
  scanf("%d", &n);
  gets(str);
  while (n--) {
    gets(str);
    int j = 0, l = 0, len = strlen(str);
    while (!isalpha(str[j]) && !isdigit(str[j])) ++j;
    while (isalpha(str[j]) || isdigit(str[j])) name[l++] = str[j++];
    name[l] = '\0';
    j++;
    cnt = 0;
    while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j;
    while (j < len) {
      l = 0;
      cnt++;
      while (isalpha(str[j]) || isdigit(str[j])) para[cnt][l++] = str[j++];
      para[cnt][l] = '\0';
      while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j;
    }
    bool flag = true;
    for (int i = 1; i <= cnt; ++i) {
      bool find = false;
      for (int j = 1; j <= vcnt; ++j)
        if (!strcmp(para[i], v[j].name)) {
          strcpy(para[i], v[j].type);
          find = true;
          break;
        }
      if (!find) {
        flag = false;
        break;
      }
    }
    if (!flag) {
      printf("0\n");
      continue;
    }
    int res = 0;
    for (int i = 1; i <= fcnt; ++i)
      if ((strcmp(f[i].name, name) == 0) && (cnt == f[i].paracnt)) {
        bool okay = true;
        for (int j = 1; j <= cnt; ++j)
          if (strcmp(f[i].para[j], "T") && strcmp(f[i].para[j], para[j])) {
            okay = false;
            break;
          }
        res += okay;
      }
    printf("%d\n", res);
  }
  return 0;
}
