#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000")
bool ascending(int i, int j) { return (i < j); }
bool descending(int i, int j) { return (i > j); }
using namespace std;
int n, m;
pair<string, vector<int> > FUNC[1000 + 10];
map<string, int> VAR;
string trim(string s) {
  int index = 0, it = s.length() - 1, len = s.length();
  while (s[index] == ' ') index++, len--;
  while (s[it] == ' ') len--, it--;
  return s.substr(index, len);
}
void createFunc(string s, int i) {
  int start = s.find("void") + 4;
  while (s[start] == ' ') start++;
  int len = 0;
  while (s[len + start] != '(') len++;
  string name = trim(s.substr(start, len));
  vector<int> V;
  bool flag = true;
  while (flag) {
    start = start + len + 1;
    len = 0;
    while (s[len + start] != ',') {
      if (s[start + len] == ')') {
        flag = false;
        break;
      }
      len++;
    }
    string type = trim(s.substr(start, len));
    if (type == "T") V.push_back(0);
    if (type == "int") V.push_back(1);
    if (type == "string") V.push_back(2);
    if (type == "double") V.push_back(3);
  }
  FUNC[i].first = name;
  FUNC[i].second = V;
}
void createVar(string s, int i) {
  s = trim(s);
  int len = 0;
  while (s[len] != ' ') len++;
  string type = s.substr(0, len);
  int start = len + 1;
  len = 0;
  while (s[start] == ' ') start++;
  string name = s.substr(start);
  if (type == "T") VAR[name] = 0;
  if (type == "int") VAR[name] = 1;
  if (type == "string") VAR[name] = 2;
  if (type == "double") VAR[name] = 3;
}
pair<string, vector<int> > parseFunc(string s) {
  s = trim(s);
  int start = 0;
  while (s[start] == ' ') start++;
  int len = 0;
  while (s[len + start] != '(') len++;
  string name = trim(s.substr(start, len));
  vector<int> V;
  bool flag = true;
  while (flag) {
    start = start + len + 1;
    len = 0;
    while (s[len + start] != ',') {
      if (s[start + len] == ')') {
        flag = false;
        break;
      }
      len++;
    }
    string names = trim(s.substr(start, len));
    V.push_back(VAR[names]);
  }
  return pair<string, vector<int> >(name, V);
}
int main() {
  cin >> n;
  for (int(i) = 0; (i) < (n); (i)++) {
    string s;
    getline(cin, s);
    if (s == "") {
      i--;
      continue;
    }
    createFunc(s, i);
  }
  cin >> m;
  for (int(i) = 0; (i) < (m); (i)++) {
    string s;
    getline(cin, s);
    if (s == "") {
      i--;
      continue;
    }
    createVar(s, i);
  }
  int k;
  cin >> k;
  for (int(i) = 0; (i) < (k); (i)++) {
    string s;
    getline(cin, s);
    if (s == "") {
      i--;
      continue;
    }
    pair<string, vector<int> > A = parseFunc(s);
    int ret = 0;
    for (int(j) = 0; (j) < (n); (j)++) {
      if (FUNC[j].first == A.first &&
          FUNC[j].second.size() == A.second.size()) {
        bool flag = true;
        for (int(t) = 0; (t) < (A.second.size()); (t)++) {
          if (!(FUNC[j].second[t] == 0 || A.second[t] == FUNC[j].second[t])) {
            flag = false;
            break;
          }
        }
        if (flag) ret++;
      }
    }
    cout << ret << endl;
  }
}
