#include <bits/stdc++.h>
using namespace std;
string funcname[1111];
int paramtype[1111][5];
int paramcnt[1111];
map<string, int> vartype;
int main(void) {
  int n = 0;
  scanf("%d\n", &n);
  for (int i = 0; i < n; i++) {
    char funcstr[1111] = {0};
    gets(funcstr);
    int len = strlen(funcstr);
    for (int j = 0; j < len; j++) {
      if (funcstr[j] == '(' || funcstr[j] == ')' || funcstr[j] == ',')
        funcstr[j] = ' ';
    }
    char* token = strtok(funcstr, " ");
    int cnt = 0;
    while (token) {
      if (cnt == 1) {
        funcname[i] = token;
      } else if (cnt > 1) {
        int type = 0;
        if (strcmp(token, "int") == 0) type = 1;
        if (strcmp(token, "double") == 0) type = 2;
        if (strcmp(token, "string") == 0) type = 3;
        if (strcmp(token, "T") == 0) type = 4;
        paramtype[i][cnt - 2] = type;
      }
      cnt++;
      token = strtok(NULL, " ");
    }
    paramcnt[i] = cnt - 2;
  }
  int m = 0;
  scanf("%d\n", &m);
  for (int i = 0; i < m; i++) {
    char varstr[1111] = {0};
    gets(varstr);
    char* token = strtok(varstr, " ");
    int cnt = 0;
    int type = 0;
    while (token) {
      if (cnt == 0) {
        if (strcmp(token, "int") == 0) type = 1;
        if (strcmp(token, "double") == 0) type = 2;
        if (strcmp(token, "string") == 0) type = 3;
        if (strcmp(token, "T") == 0) type = 4;
      } else {
        vartype[string(token)] = type;
      }
      cnt++;
      token = strtok(NULL, " ");
    }
  }
  int qcnt = 0;
  scanf("%d\n", &qcnt);
  for (int i = 0; i < qcnt; i++) {
    char callstr[1111] = {0};
    gets(callstr);
    int len = strlen(callstr);
    for (int j = 0; j < len; j++) {
      if (callstr[j] == '(' || callstr[j] == ')' || callstr[j] == ',')
        callstr[j] = ' ';
    }
    char* token = strtok(callstr, " ");
    int cnt = 0;
    string callname;
    int ptype[5];
    while (token) {
      if (cnt == 0)
        callname = token;
      else
        ptype[cnt - 1] = vartype[string(token)];
      cnt++;
      token = strtok(NULL, " ");
    }
    int ans = 0;
    for (int j = 0; j < n; j++) {
      if (cnt - 1 != paramcnt[j] || funcname[j] != callname) continue;
      bool fail = false;
      for (int k = 0; k < cnt - 1; k++) {
        if (ptype[k] == 0 ||
            (ptype[k] != paramtype[j][k] && paramtype[j][k] != 4)) {
          fail = true;
          break;
        }
      }
      if (!fail) ans++;
    }
    printf("%d\n", ans);
  }
  while (getchar() != EOF)
    ;
  return 0;
}
