#include <bits/stdc++.h>
using namespace std;
typedef struct procedure {
  string name;
  int num;
  string var[5];
} P;
P proc[1000 + 5];
map<string, string> var_map;
int take_input(char in[]) {
  char c;
  int len = 0;
  while ((c = getchar()) != '\n') in[len++] = c;
  in[len] = 0;
  return len;
}
int main() {
  char c, in[1000];
  int i, j, k, len, N, M, K;
  scanf("%d", &N);
  getchar();
  for (i = 0; i < N; i++) {
    len = take_input(in);
    char *token = strtok(in, " ,()");
    token = strtok(NULL, " ,()");
    proc[i].name = string(token);
    proc[i].num = 0;
    while ((token = strtok(NULL, " ,()")) != NULL)
      proc[i].var[proc[i].num++] = string(token);
  }
  scanf("%d", &M);
  getchar();
  for (i = 0; i < M; i++) {
    len = take_input(in);
    char *token = strtok(in, " ");
    string type = string(token);
    token = strtok(NULL, " ");
    string name = string(token);
    var_map.insert(make_pair(name, type));
  }
  P cur;
  scanf("%d", &K);
  getchar();
  for (i = 0; i < K; i++) {
    len = take_input(in);
    char *token = strtok(in, " ,()");
    cur.name = string(token);
    cur.num = 0;
    while ((token = strtok(NULL, " ,()")) != NULL)
      cur.var[cur.num++] = var_map.find(string(token))->second;
    int equal = 0;
    for (j = 0; j < N; j++)
      if (cur.name.compare(proc[j].name) == 0 && cur.num == proc[j].num) {
        for (k = 0; k < cur.num; k++)
          if (!(proc[j].var[k].compare(string("T")) == 0 ||
                cur.var[k].compare(proc[j].var[k]) == 0))
            break;
        if (k == cur.num) equal++;
      }
    printf("%d\n", equal);
  }
  return 0;
}
