#include <bits/stdc++.h>
using namespace std;
map<string, int> conv;
map<string, int> var;
map<string, int> tconv;
vector<vector<vector<int> > > req;
string alpha = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
void buildf(string str) {
  int pos = str.find("void") + 4, bck;
  pos = str.find_first_of(alpha, pos);
  bck = str.find_first_not_of(alpha, pos);
  string name = str.substr(pos, bck - pos);
  if (conv.count(name) == 0) {
    conv[name] = req.size();
    req.push_back(vector<vector<int> >());
  }
  vector<int> now;
  pos = str.find('(');
  while (true) {
    pos = str.find_first_of(alpha, pos);
    if (pos == string::npos) break;
    bck = str.find_first_not_of(alpha, pos) - 1;
    string type = str.substr(pos, bck - pos + 1);
    now.push_back(tconv[type]);
    pos = bck + 1;
  }
  req[conv[name]].push_back(now);
}
void buildvar(string str) {
  int pos = str.find_first_of(alpha);
  int bck = str.find_first_not_of(alpha, pos) - 1;
  int type = tconv[str.substr(pos, bck - pos + 1)];
  pos = str.find_first_of(alpha, bck + 1);
  bck = str.find_first_not_of(alpha, pos) - 1;
  var[str.substr(pos, bck - pos + 1)] = type;
}
int check(string str) {
  int pos, bck, res = 0;
  pos = str.find_first_of(alpha);
  bck = str.find_first_not_of(alpha, pos) - 1;
  string name = str.substr(pos, bck - pos + 1);
  if (conv.count(name)) {
    vector<int> now;
    pos = str.find('(');
    while (true) {
      pos = str.find_first_of(alpha, pos);
      if (pos == string::npos) break;
      bck = str.find_first_not_of(alpha, pos) - 1;
      now.push_back(var[str.substr(pos, bck - pos + 1)]);
      pos = bck + 1;
    }
    for (auto i : req[conv[name]]) {
      if (i.size() != now.size()) continue;
      bool f = true;
      for (int j = 0; j < now.size(); j++) {
        if ((now[j] & i[j]) == 0) f = false;
      }
      if (f) res++;
    }
  }
  return res;
}
int main() {
  vector<int> out;
  tconv["int"] = 1;
  tconv["double"] = (1 << 1);
  tconv["string"] = (1 << 2);
  tconv["T"] = (1 << 3) - 1;
  int n, m, k;
  cin >> n;
  cin.ignore();
  for (int i = 0; i < n; i++) {
    string str;
    getline(cin, str, '\n');
    buildf(str);
  }
  cin >> m;
  cin.ignore();
  for (int i = 0; i < m; i++) {
    string str;
    getline(cin, str, '\n');
    buildvar(str);
  }
  cin >> k;
  cin.ignore();
  for (int i = 0; i < k; i++) {
    string str;
    getline(cin, str, '\n');
    out.push_back(check(str));
  }
  for (auto x : out) cout << x << endl;
  return 0;
}
