#include <bits/stdc++.h>
int number(std::string arg) {
  if (arg == "") {
    return 0;
  }
  if (arg == "T") {
    return 1;
  }
  if (arg == "int") {
    return 2;
  }
  if (arg == "string") {
    return 3;
  }
  if (arg == "double") {
    return 4;
  }
}
void h5(int* Mat_5, std::string arg5) {
  if (arg5 == "T") {
    for (int i = 1; i < 5; i++) {
      Mat_5[i]++;
    }
  } else {
    Mat_5[number(arg5)]++;
  }
}
void h4(int** Mat_4, std::string arg4, std::string arg5) {
  if (arg4 == "T") {
    for (int i = 1; i < 5; i++) {
      h5(Mat_4[i], arg5);
    }
  } else {
    h5(Mat_4[number(arg4)], arg5);
  }
}
void h3(int*** Mat_3, std::string arg3, std::string arg4, std::string arg5) {
  if (arg3 == "T") {
    for (int i = 1; i < 5; i++) {
      h4(Mat_3[i], arg4, arg5);
    }
  } else {
    h4(Mat_3[number(arg3)], arg4, arg5);
  }
}
void h2(int**** Mat_2, std::string arg2, std::string arg3, std::string arg4,
        std::string arg5) {
  if (arg2 == "T") {
    for (int i = 1; i < 5; i++) {
      h3(Mat_2[i], arg3, arg4, arg5);
    }
  } else {
    h3(Mat_2[number(arg2)], arg3, arg4, arg5);
  }
}
void handle_arguments(std::string fooname,
                      std::map<std::string, int*****>* fooParamsDict,
                      std::string arg1, std::string arg2 = "no",
                      std::string arg3 = "no", std::string arg4 = "no",
                      std::string arg5 = "no") {
  std::map<std::string, int*****>::iterator it = (*fooParamsDict).find(fooname);
  int***** Mat;
  if (it == (*fooParamsDict).end()) {
    const int T = 5;
    Mat = new int****[T];
    int**** Mat_2 = new int***[T * T];
    int*** Mat_3 = new int**[T * T * T];
    int** Mat_4 = new int*[T * T * T * T];
    int* Mat_5 = new int[T * T * T * T * T];
    for (int i1 = 0; i1 < T; i1++) {
      Mat[i1] = &Mat_2[T * i1];
      for (int i2 = 0; i2 < T; i2++) {
        Mat[i1][i2] = &Mat_3[T * T * i1 + T * i2];
        for (int i3 = 0; i3 < T; i3++) {
          Mat[i1][i2][i3] = &Mat_4[T * T * T * i1 + T * T * i2 + T * i3];
          for (int i4 = 0; i4 < T; i4++) {
            Mat[i1][i2][i3][i4] = &Mat_5[T * T * T * T * i1 + T * T * T * i2 +
                                         T * T * i3 + T * i4];
            for (int i5 = 0; i5 < T; i5++) {
              Mat[i1][i2][i3][i4][i5] = 0;
            }
          }
        }
      }
    }
    (*fooParamsDict).insert(std::pair<std::string, int*****>(fooname, Mat));
  } else {
    Mat = it->second;
  }
  if (arg1 == "T") {
    for (int i = 1; i < 5; i++) {
      h2(Mat[i], arg2, arg3, arg4, arg5);
    }
  } else {
    h2(*(Mat + number(arg1)), arg2, arg3, arg4, arg5);
  }
}
int main() {
  std::map<std::string, int*****> fooParamsDict;
  std::map<std::string, int> dict;
  dict.insert(std::pair<std::string, int>("", 0));
  dict.insert(std::pair<std::string, int>("T", 1));
  dict.insert(std::pair<std::string, int>("int", 2));
  dict.insert(std::pair<std::string, int>("string", 3));
  dict.insert(std::pair<std::string, int>("double", 4));
  int n;
  std::cin >> n;
  char ch = 'a';
  std::string fooname = "";
  std::string arg1 = "";
  std::string arg2 = "";
  std::string arg3 = "";
  std::string arg4 = "";
  std::string arg5 = "";
  bool nextline = false;
  bool firsttime = true;
  for (int temp = 0; temp < n; temp++) {
    if (!firsttime) {
      handle_arguments(fooname, &fooParamsDict, arg1, arg2, arg3, arg4, arg5);
    } else {
      firsttime = false;
    }
    nextline = false;
    arg1 = "";
    arg2 = "";
    arg3 = "";
    arg4 = "";
    arg5 = "";
    std::cin >> fooname;
    fooname = "";
    while (true) {
      ch = getchar();
      if (ch == '(') {
        break;
      }
      if ((ch != ' ') && (ch != '\n')) {
        fooname += ch;
      }
    }
    while (true) {
      ch = getchar();
      if (ch == ')') {
        nextline = true;
        break;
      }
      if (ch == ',') {
        break;
      }
      if (ch != ' ') {
        arg1 += ch;
      }
    }
    if (nextline) {
      continue;
    }
    while (true) {
      ch = getchar();
      if (ch == ')') {
        nextline = true;
        break;
      }
      if (ch == ',') {
        break;
      }
      if (ch != ' ') {
        arg2 += ch;
      }
    }
    if (nextline) {
      continue;
    }
    while (true) {
      ch = getchar();
      if (ch == ')') {
        nextline = true;
        break;
      }
      if (ch == ',') {
        break;
      }
      if (ch != ' ') {
        arg3 += ch;
      }
    }
    if (nextline) {
      continue;
    }
    while (true) {
      ch = getchar();
      if (ch == ')') {
        nextline = true;
        break;
      }
      if (ch == ',') {
        break;
      }
      if (ch != ' ') {
        arg4 += ch;
      }
    }
    if (nextline) {
      continue;
    }
    while (true) {
      ch = getchar();
      if (ch == ')') {
        nextline = true;
        break;
      }
      if (ch == ',') {
        break;
      }
      if (ch != ' ') {
        arg5 += ch;
      }
    }
    if (nextline) {
      continue;
    }
  }
  handle_arguments(fooname, &fooParamsDict, arg1, arg2, arg3, arg4, arg5);
  int m;
  std::cin >> m;
  std::map<std::string, std::string> variable_dict;
  std::string var_name;
  std::string var_type;
  for (int i = 0; i < m; i++) {
    std::cin >> var_type >> var_name;
    variable_dict.insert(
        std::pair<std::string, std::string>(var_name, var_type));
  }
  int k;
  std::cin >> k;
  firsttime = true;
  std::map<std::string, int*****>::iterator it;
  for (int temp = 0; temp < k; temp++) {
    if (!firsttime) {
      it = fooParamsDict.find(fooname);
      if (it == fooParamsDict.end()) {
        std::cout << 0 << std::endl;
      } else {
        std::cout << (it->second)[number(variable_dict[arg1])][number(
                         variable_dict[arg2])][number(variable_dict[arg3])]
                                 [number(variable_dict[arg4])]
                                 [number(variable_dict[arg5])]
                  << std::endl;
      }
    } else {
      firsttime = false;
    }
    nextline = false;
    arg1 = "";
    arg2 = "";
    arg3 = "";
    arg4 = "";
    arg5 = "";
    fooname = "";
    while (true) {
      ch = getchar();
      if (ch == '(') {
        break;
      }
      if ((ch != ' ') && (ch != '\n')) {
        fooname += ch;
      }
    }
    while (true) {
      ch = getchar();
      if (ch == ')') {
        nextline = true;
        break;
      }
      if (ch == ',') {
        break;
      }
      if (ch != ' ') {
        arg1 += ch;
      }
    }
    if (nextline) {
      continue;
    }
    while (true) {
      ch = getchar();
      if (ch == ')') {
        nextline = true;
        break;
      }
      if (ch == ',') {
        break;
      }
      if (ch != ' ') {
        arg2 += ch;
      }
    }
    if (nextline) {
      continue;
    }
    while (true) {
      ch = getchar();
      if (ch == ')') {
        nextline = true;
        break;
      }
      if (ch == ',') {
        break;
      }
      if (ch != ' ') {
        arg3 += ch;
      }
    }
    if (nextline) {
      continue;
    }
    while (true) {
      ch = getchar();
      if (ch == ')') {
        nextline = true;
        break;
      }
      if (ch == ',') {
        break;
      }
      if (ch != ' ') {
        arg4 += ch;
      }
    }
    if (nextline) {
      continue;
    }
    while (true) {
      ch = getchar();
      if (ch == ')') {
        nextline = true;
        break;
      }
      if (ch == ',') {
        break;
      }
      if (ch != ' ') {
        arg5 += ch;
      }
    }
    if (nextline) {
      continue;
    }
  }
  it = fooParamsDict.find(fooname);
  if (it == fooParamsDict.end()) {
    std::cout << 0 << std::endl;
  } else {
    std::cout << (it->second)[number(variable_dict[arg1])][number(
                     variable_dict[arg2])][number(variable_dict[arg3])]
                             [number(variable_dict[arg4])]
                             [number(variable_dict[arg5])]
              << std::endl;
  }
}
