#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int INF = 1e9;
const string tps[] = {"int", "double", "string", "T"};
string p, v[N], s;
map<string, int> mt;
int n, m, k;
pair<string, string> d[N];
string correct(string s, bool f = false) {
  string res = "";
  int n = s.size();
  if (f) {
    for (int i = 3; i < n; i++)
      if (s[i - 3] == 'v' && s[i - 2] == 'o' && s[i - 1] == 'i' &&
          s[i] == 'd') {
        s.erase(0, i + 1);
        n = s.size();
        break;
      }
  }
  for (int i = 0; i < n; i++)
    if (s[i] != ' ') res += s[i];
  return res;
}
int get_type_by_str(string s) {
  for (int i = 0; i < 4; i++)
    if (tps[i] == s) return i;
  return -1;
}
void get_type_of_v(string s) {
  int pos = 0, n = s.size();
  string tp = "", name = "";
  while (pos < n && s[pos] == ' ') pos++;
  while (pos < n && s[pos] != ' ') tp += s[pos], pos++;
  while (pos < n && s[pos] == ' ') pos++;
  while (pos < n && s[pos] != ' ') name += s[pos], pos++;
  mt[name] = get_type_by_str(tp);
}
pair<string, string> get_id(string s) {
  int pos = 0, n = s.size();
  pair<string, string> r;
  while (pos < n && s[pos] != '(') r.first += s[pos], pos++;
  pos++;
  while (pos < n) {
    string tmp = "";
    while (pos < n && s[pos] != ',' && s[pos] != ')') tmp += s[pos], pos++;
    r.second += char(get_type_by_str(tmp) + '0');
    pos++;
  }
  return r;
}
pair<string, string> get_vid(string s) {
  int pos = 0, n = s.size();
  pair<string, string> r;
  while (pos < n && s[pos] == ' ') pos++;
  while (pos < n && s[pos] != '(') r.first += s[pos], pos++;
  pos++;
  while (pos < n) {
    string tmp = "";
    while (pos < n && s[pos] != ',' && s[pos] != ')') tmp += s[pos], pos++;
    r.second += char(mt[tmp] + '0');
    pos++;
  }
  return r;
}
bool is_eq(string a, string b) {
  if (a.size() == b.size()) {
    int n = a.size();
    for (int i = 0; i < n; i++)
      if (!(a[i] == b[i] || a[i] == '3' || b[i] == '3')) return 0;
    return 1;
  }
  return 0;
}
int main() {
  cin >> n;
  getline(cin, s);
  for (int i = 0; i < n; i++) {
    getline(cin, p);
    d[i] = get_id(correct(p, 1));
  }
  cin >> m;
  getline(cin, s);
  for (int i = 0; i < m; i++) {
    getline(cin, v[i]);
    get_type_of_v(v[i]);
  }
  cin >> k;
  getline(cin, s);
  for (int i = 0; i < k; i++) {
    getline(cin, s);
    pair<string, string> t = get_vid(correct(s));
    int cnt = 0;
    for (int j = 0; j < n; j++)
      if (d[j].first == t.first && is_eq(d[j].second, t.second)) cnt++;
    cout << cnt << endl;
  }
  return 0;
}
