#include <bits/stdc++.h>
using namespace std;
struct proc {
  string name;
  vector<int> t;
};
struct var {
  string name;
  int t;
};
proc p[1001], f[1001];
var v[1001];
int n, m, k;
bool cmp1(proc p1, proc p2) { return (p1.name < p2.name); }
bool cmp2(var v1, var v2) { return (v1.name < v2.name); }
int gett(string t) {
  if (t == "T") return 0;
  if (t == "int") return 1;
  if (t == "string") return 2;
  if (t == "double") return 3;
}
int findt(string vname) {
  int l = 0, r = m - 1;
  while (l < r) {
    int sr = (l + r) / 2;
    if (v[sr].name < vname)
      l = sr + 1;
    else
      r = sr;
  }
  return v[l].t;
}
int bins(proc q) {
  int l = 0, r = n - 1;
  while (l < r) {
    int sr = (l + r) / 2;
    if (p[sr].name < q.name)
      l = sr + 1;
    else
      r = sr;
  }
  if (p[l].name != q.name) return 0;
  while (l > 0 && p[l - 1].name == p[l].name) l--;
  while (r < n - 1 && p[r + 1].name == p[r].name) r++;
  int ans = 0;
  for (int i = l; i <= r; i++)
    if (q.t.size() == p[i].t.size()) {
      bool x = true;
      for (int j = 0; j < q.t.size(); j++)
        if (p[i].t[j] != 0 && p[i].t[j] != q.t[j]) x = false;
      if (x) ans++;
    }
  return ans;
}
int main() {
  string g;
  cin >> n;
  getline(cin, g);
  for (int i = 0; i < n; i++) {
    string s, x = "";
    int num = 0;
    getline(cin, s);
    s += ' ';
    for (int j = 0; j < s.size(); j++)
      if (s[j] == ' ' || s[j] == '(' || s[j] == ',' || s[j] == ')') {
        if (x != "") {
          if (num == 1)
            p[i].name = x;
          else if (num >= 2)
            p[i].t.push_back(gett(x));
          num++;
        }
        x = "";
      } else
        x += s[j];
  }
  sort(p, p + n, &cmp1);
  cin >> m;
  getline(cin, g);
  for (int i = 0; i < m; i++) {
    string s, x;
    int num = 0;
    getline(cin, s);
    s += ' ';
    for (int j = 0; j < s.size(); j++)
      if (s[j] == ' ' || s[j] == '(' || s[j] == ',' || s[j] == ')') {
        if (x != "") {
          if (num == 0)
            v[i].t = gett(x);
          else
            v[i].name = x;
          num++;
        }
        x = "";
      } else
        x += s[j];
  }
  sort(v, v + m, &cmp2);
  cin >> k;
  getline(cin, g);
  for (int i = 0; i < k; i++) {
    string s, x = "";
    int num = 0;
    getline(cin, s);
    s += ' ';
    for (int j = 0; j < s.size(); j++)
      if (s[j] == ' ' || s[j] == '(' || s[j] == ',' || s[j] == ')') {
        if (x != "") {
          if (num == 0)
            f[i].name = x;
          else if (num >= 1)
            f[i].t.push_back(findt(x));
          num++;
        }
        x = "";
      } else
        x += s[j];
  }
  for (int i = 0; i < k; i++) cout << bins(f[i]) << endl;
}
