#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  cin.ignore();
  vector<pair<string, vector<string>>> func(n);
  for (int i = 0; i < n; i++) {
    string s, t = "";
    getline(cin, s, '\n');
    int c = 0;
    for (int j = 0; j < s.size(); j++) {
      if (isalpha(s[j]) || isdigit(s[j]))
        t += s[j];
      else {
        if (!t.empty()) {
          if (c == 1)
            func[i].first = t;
          else if (c > 1)
            func[i].second.push_back(t);
          t = "", c++;
        }
      }
    }
  }
  int m;
  cin >> m;
  map<string, string> var;
  for (int i = 0; i < m; i++) {
    string s, t;
    cin >> s >> t;
    var[t] = s;
  }
  int k;
  cin >> k;
  cin.ignore();
  for (int i = 0; i < k; i++) {
    string funcName;
    vector<string> variables;
    string s, t = "";
    getline(cin, s, '\n');
    int c = 0, ans = 0;
    for (int j = 0; j < s.size(); j++) {
      if (isalpha(s[j]) || isdigit(s[j]))
        t += s[j];
      else {
        if (!t.empty()) {
          if (c == 0)
            funcName = t;
          else
            variables.push_back(t);
          t = "", c++;
        }
      }
    }
    for (int j = 0; j < n; j++) {
      if (func[j].first == funcName) {
        if (func[j].second.size() != variables.size()) continue;
        bool ok = true;
        for (int h = 0; h < variables.size(); h++) {
          if (var[variables[h]] != func[j].second[h] &&
              func[j].second[h] != "T") {
            ok = false;
            break;
          }
        }
        if (ok) ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
