#include <bits/stdc++.h>
enum CTypeId { tpT, tpInt, tpStr, tpDouble };
struct CFunc {
  std::string name_;
  std::vector<CTypeId> params_;
};
std::vector<CFunc> fl;
std::string ParseName(char const* s, int& i) {
  while (s[i] && !(((s[i]) >= '0' && (s[i]) <= '9') ||
                   (((s[i]) >= 'a' && (s[i]) <= 'z') ||
                    ((s[i]) >= 'A' && (s[i]) <= 'Z')))) {
    ++i;
  }
  std::string res;
  while (s[i] && (((s[i]) >= '0' && (s[i]) <= '9') ||
                  (((s[i]) >= 'a' && (s[i]) <= 'z') ||
                   ((s[i]) >= 'A' && (s[i]) <= 'Z')))) {
    res.push_back(s[i]);
    ++i;
  }
  return res;
}
CTypeId GetTypeId(std::string const& s) {
  if (s == "T") return tpT;
  if (s == "int") return tpInt;
  if (s == "string") return tpStr;
  if (s == "double") return tpDouble;
  return tpT;
}
void ParseFunc(char const* s) {
  int pos = 0;
  std::string name;
  name = ParseName(s, pos);
  name = ParseName(s, pos);
  CFunc f;
  f.name_ = name;
  name = ParseName(s, pos);
  while (!name.empty()) {
    f.params_.push_back(GetTypeId(name));
    name = ParseName(s, pos);
  }
  fl.push_back(f);
}
std::map<std::string, CTypeId> vm;
void ParseVar(char const* s) {
  int pos = 0;
  std::string typeName = ParseName(s, pos);
  std::string name = ParseName(s, pos);
  vm[name] = GetTypeId(typeName);
}
CFunc ParseCall(char const* s) {
  int pos = 0;
  std::string name;
  name = ParseName(s, pos);
  CFunc f;
  f.name_ = name;
  name = ParseName(s, pos);
  while (!name.empty()) {
    std::map<std::string, CTypeId>::const_iterator it = vm.find(name);
    f.params_.push_back(it->second);
    name = ParseName(s, pos);
  }
  return f;
}
bool Suitable(CFunc const& tmp, CFunc const& f) {
  if (tmp.name_ != f.name_) return false;
  if (tmp.params_.size() != f.params_.size()) return false;
  for (int i = 0; i < (int)f.params_.size(); ++i) {
    if (tmp.params_[i] == tpT) continue;
    if (tmp.params_[i] != f.params_[i]) return false;
  }
  return true;
}
int Solve(char const* s) {
  int cnt = 0;
  CFunc f = ParseCall(s);
  for (std::vector<CFunc>::const_iterator i = fl.begin(); i != fl.end(); ++i) {
    if (Suitable(*i, f)) ++cnt;
  }
  return cnt;
}
char buf[65000];
int main() {
  {
    gets(buf);
    int n;
    sscanf(buf, "%d", &n);
    for (int i = 0; i < n; ++i) {
      gets(buf);
      ParseFunc(buf);
    }
  }
  {
    gets(buf);
    int m;
    sscanf(buf, "%d", &m);
    for (int i = 0; i < m; ++i) {
      gets(buf);
      ParseVar(buf);
    }
  }
  {
    gets(buf);
    int k;
    sscanf(buf, "%d", &k);
    for (int i = 0; i < k; ++i) {
      gets(buf);
      int ans = Solve(buf);
      printf("%d\n", ans);
    }
  }
}
