#include <bits/stdc++.h>
using namespace std;
string nsp(string s) {
  string t = "";
  for (int i = 0; i < s.size(); i++)
    if (s[i] != ' ') t = t + s[i];
  return t;
}
string gfbu(string s, char c, int a) {
  string t = "";
  for (int i = a; i < s.size() - 1; i++) {
    if (s[i] == c) return t;
    t += s[i];
  }
  return t;
}
int in(string s) {
  return (s == "double" ? 2 : (s == "int" ? 3 : (s == "T" ? 1 : 5)));
}
vector<int> ext(string s) {
  int cnt = gfbu(s, '(', 0).size() + 1;
  vector<int> v;
  while (cnt < s.size())
    v.push_back(in(gfbu(s, ',', cnt))), cnt += gfbu(s, ',', cnt).size() + 1;
  return v;
}
int main() {
  int n, m, k;
  map<string, int> ints;
  map<string, vector<vector<int> > > voids;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string trash, s, vo;
    if (i == 0) cin.ignore();
    getline(cin, s), s = nsp(s), s.erase(0, 4);
    vo = gfbu(s, '(', 0), voids[vo].push_back(ext(s));
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    string s, t;
    cin >> s >> t, ints[t] = in(s);
  }
  cin >> k;
  for (int i = 0; i < k; i++) {
    if (i == 0) cin.ignore();
    string s, vo;
    getline(cin, s), s = nsp(s), vo = gfbu(s, '(', 0);
    int ans = 0, cnt = gfbu(s, '(', 0).size() + 1;
    vector<int> vi;
    while (cnt < s.size())
      vi.push_back(ints[gfbu(s, ',', cnt)]),
          cnt += gfbu(s, ',', cnt).size() + 1;
    if (voids[vo].size() == 0) {
      cout << "0\n";
      continue;
    }
    for (int j = 0; j < voids[vo].size(); j++) {
      if (voids[vo][j].size() != vi.size()) continue;
      for (int k = 0; k < vi.size(); k++)
        if (vi[k] % voids[vo][j][k]) {
          ans--;
          break;
        }
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
