#include <bits/stdc++.h>
enum var_type {
  TYPE_DOUBLE,
  TYPE_STRING,
  TYPE_INT,
  TYPE_T,
};
using namespace std;
struct proc {
  string name;
  vector<var_type> types;
};
vector<proc> procedures;
map<string, var_type> vars;
vector<int> results;
var_type get_type(string type_name) {
  if (type_name == "double") return TYPE_DOUBLE;
  if (type_name == "int") return TYPE_INT;
  if (type_name == "string") return TYPE_STRING;
  if (type_name == "T") return TYPE_T;
}
void proc_procedures() {
  int n;
  cin >> n;
  cin.ignore(1, '\n');
  for (int i = 0; i < n; ++i) {
    char buf[200];
    gets(buf);
    string str(buf);
    int j = 0;
    while (str[j] == ' ') ++j;
    j += 4;
    while (str[j] == ' ') ++j;
    string name;
    while (str[j] != ' ' && str[j] != '(') {
      name += str[j];
      ++j;
    }
    while (str[j] != '(') ++j;
    ++j;
    vector<var_type> var_types;
    while (str[j] != ')') {
      if (str[j] == ' ') {
        ++j;
        continue;
      }
      string cur_type;
      while (str[j] != ' ' && str[j] != ')' && str[j] != ',') {
        cur_type += str[j];
        ++j;
      }
      var_types.push_back(get_type(cur_type));
      while (str[j] != ')' && str[j] != ',') ++j;
      if (str[j] == ',') ++j;
    }
    proc temp;
    temp.name = name;
    temp.types = var_types;
    procedures.push_back(temp);
  }
}
void proc_variables() {
  int n;
  cin >> n;
  cin.ignore(1, '\n');
  for (int i = 0; i < n; ++i) {
    char buf[200];
    gets(buf);
    string str(buf);
    int j = 0;
    while (str[j] == ' ') ++j;
    string cur_type;
    while (str[j] != ' ') {
      cur_type += str[j];
      ++j;
    }
    while (str[j] == ' ') ++j;
    string name;
    while (j < str.size() && str[j] != ' ') {
      name += str[j];
      ++j;
    }
    vars.insert(pair<string, var_type>(name, get_type(cur_type)));
  }
}
void proc_calls() {
  int n;
  cin >> n;
  cin.ignore(1, '\n');
  for (int i = 0; i < n; ++i) {
    char buf[200];
    gets(buf);
    string str(buf);
    int j = 0;
    while (str[j] == ' ') ++j;
    string name;
    while (str[j] != ' ' && str[j] != '(') {
      name += str[j];
      ++j;
    }
    while (str[j] != '(') ++j;
    ++j;
    vector<var_type> var_types;
    while (str[j] != ')') {
      if (str[j] == ' ') {
        ++j;
        continue;
      }
      string cur_type;
      while (str[j] != ' ' && str[j] != ')' && str[j] != ',') {
        cur_type += str[j];
        ++j;
      }
      var_types.push_back(vars[cur_type]);
      while (str[j] != ')' && str[j] != ',') ++j;
      if (str[j] == ',') ++j;
    }
    int res = 0;
    for (int j = 0; j < procedures.size(); ++j) {
      if (procedures[j].name != name) continue;
      if (var_types.size() != procedures[j].types.size()) continue;
      bool is_ok = true;
      for (int k = 0; k < var_types.size(); ++k) {
        if (procedures[j].types[k] == TYPE_T) continue;
        if (procedures[j].types[k] != var_types[k]) is_ok = false;
      }
      if (is_ok) ++res;
    }
    results.push_back(res);
  }
}
int main() {
  proc_procedures();
  proc_variables();
  proc_calls();
  for (int i = 0; i < results.size(); ++i) cout << results[i] << endl;
  return 0;
}
