#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:25600000")
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-9;
const int INF = 1000000000;
struct proc {
  string tp;
  string name;
  vector<string> param;
};
struct func {
  string name;
  vector<string> param;
};
vector<proc> p;
map<string, string> types;
bool ok(func& f, proc& pr) {
  if (f.name != pr.name) return false;
  if (f.param.size() != pr.param.size()) return false;
  for (long long i = 0; i < f.param.size(); ++i)
    if (types[f.param[i]] != pr.param[i] && pr.param[i] != "T") return false;
  return true;
}
int main() {
  int n;
  scanf("%d\n", &n);
  for (long long i = 0; i < n; ++i) {
    int k = 0;
    string s;
    getline(cin, s);
    while (isspace(s[0])) s.erase(0, 1);
    p.push_back(proc());
    for (long long i = 0; i < s.size(); ++i) {
      if (isspace(s[i])) {
        ++k;
        continue;
      }
      if (s[i] == ')') break;
      if (s[i] == ',' || s[i] == '(') {
        ++k;
        p.back().param.push_back("");
        continue;
      }
      if (k > 1 && p.back().name.empty()) k = 1;
      if (k == 1 && p.back().tp.empty()) k = 0;
      if (k == 0) p.back().tp.push_back(s[i]);
      if (k == 1) p.back().name.push_back(s[i]);
      if (k > 1) p.back().param.back().push_back(s[i]);
    }
  }
  scanf("%d\n", &n);
  for (long long i = 0; i < n; ++i) {
    string s;
    getline(cin, s);
    while (isspace(s[0])) s.erase(0, 1);
    string s1;
    string s2;
    int k = 0;
    for (long long i = 0; i < s.size(); ++i) {
      if (isspace(s[i])) {
        ++k;
        continue;
      }
      if (k == 0)
        s1.push_back(s[i]);
      else
        s2.push_back(s[i]);
    }
    types[s2] = s1;
  }
  scanf("%d\n", &n);
  for (long long i = 0; i < n; ++i) {
    string s;
    getline(cin, s);
    while (isspace(s[0])) s.erase(0, 1);
    int k = 0;
    func f;
    for (long long i = 0; i < s.size(); ++i) {
      if (isspace(s[i])) {
        ++k;
        continue;
      }
      if (s[i] == ')') break;
      if (s[i] == ',' || s[i] == '(') {
        ++k;
        f.param.push_back("");
        continue;
      }
      if (k == 0)
        f.name.push_back(s[i]);
      else
        f.param.back().push_back(s[i]);
    }
    int ans = 0;
    for (long long j = 0; j < p.size(); ++j)
      if (ok(f, p[j])) ++ans;
    cout << ans << endl;
  }
}
