#include <bits/stdc++.h>
using namespace std;
const int N = (int)1000 + 5;
const int INF = (int)1e9 + 9;
const double eps = 1e-9;
const double pi = 3.1415926535897932384626433832795;
class procedure {
 public:
  string name;
  vector<string> types;
  procedure() {}
};
map<string, string> M;
int n, m, k;
procedure a[N];
string s, second;
char c[1000];
set<string> names;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  getline(cin, s);
  for (int q = 0; q < n; q++) {
    getline(cin, s);
    procedure neww;
    int i = 3;
    while (i < s.size() && s[i] != 'd' && s[i - 1] != 'i' && s[i - 2] != 'o' &&
           s[i - 3] != 'v')
      i++;
    i++;
    string name = "";
    for (int j = i; j < s.size(); j++) {
      if (s[j] == '(') break;
      if (s[j] == ' ') continue;
      while ((s[j] >= 'a' && s[j] <= 'z') || (s[j] >= '0' && s[j] <= '9')) {
        name += s[j];
        j++;
      }
      if (name != "") {
        i = j;
        break;
      }
    }
    neww.name = name;
    names.insert(name);
    vector<string> types;
    while (s[i] != '(') i++;
    i++;
    for (int j = i; j < s.size(); j++) {
      string need = "";
      if (s[j] == 'T') {
        need = "T";
        types.push_back(need);
        continue;
      }
      while (s[j] >= 'a' && s[j] <= 'z') {
        need += s[j];
        j++;
      }
      if (need != "") types.push_back(need);
    }
    neww.types = types;
    a[q] = neww;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> second;
    M[second] = s;
  }
  vector<int> anss;
  cin >> k;
  getline(cin, s);
  for (int q = 0; q < k; q++) {
    getline(cin, s);
    string name = "";
    int j;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == ' ') continue;
      if (s[i] == '(') break;
      while ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9')) {
        name += s[i];
        i++;
      }
      if (name != "") {
        j = i;
        break;
      }
    }
    if (names.find(name) == names.end()) {
      anss.push_back(0);
      continue;
    }
    vector<string> types, perems;
    while (s[j] != '(') j++;
    j++;
    for (int i = j; i < s.size(); i++) {
      string perem = "";
      while ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9')) {
        perem += s[i];
        i++;
      }
      if (perem != "") perems.push_back(perem);
    }
    types.resize(perems.size());
    for (int i = 0; i < perems.size(); i++) types[i] = M[perems[i]];
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (a[i].name != name) continue;
      if (a[i].types.size() != types.size()) continue;
      bool f = 1;
      for (int j = 0; j < types.size(); j++) {
        if (types[j] == a[i].types[j] || a[i].types[j] == "T")
          f = 1;
        else {
          f = 0;
          break;
        }
      }
      if (!f) continue;
      ans++;
    }
    anss.push_back(ans);
  }
  for (int i = 0; i < anss.size() - 1; i++) cout << anss[i] << endl;
  cout << anss[anss.size() - 1];
  return 0;
}
