#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, ans, j;
string in, rname, rtypes;
bool hd, hk;
struct funcinfo {
  string name;
  vector<int> types;
};
vector<funcinfo> v;
vector<int> qtypes;
map<string, int> mp;
int getint() {
  string bb;
  getline(cin, bb);
  return atoi(bb.c_str());
}
int getnum(string intype) {
  if (intype[0] == 'i') return 1;
  if (intype[0] == 's') return 2;
  if (intype[0] == 'd') return 3;
  return 4;
}
void readfunc() {
  n = getint();
  while ((n--) > 0) {
    rname = "";
    rtypes = "";
    hd = false;
    hk = false;
    funcinfo pos;
    getline(cin, in);
    for (int i = 0; i < in.size(); i++) {
      if (hk) {
        if (in[i] == ',' || in[i] == ')') {
          pos.types.push_back(getnum(rtypes));
          rtypes = "";
        } else if (in[i] != ' ')
          rtypes.push_back(in[i]);
        continue;
      }
      if (in[i] == '(') hk = true;
      if (hd && !hk && in[i] != ' ') rname.push_back(in[i]);
      if (in[i] == 'd') hd = true;
    }
    pos.name = rname;
    v.push_back(pos);
  }
}
void readvar() {
  n = getint();
  while ((n--) > 0) {
    cin >> rtypes >> rname;
    mp[rname] = getnum(rtypes);
  }
}
void solve() {
  cin >> n;
  getline(cin, in);
  while ((n--) > 0) {
    ans = 0;
    qtypes.clear();
    rname = "";
    rtypes = "";
    hk = false;
    getline(cin, in);
    for (int i = 0; i < in.size(); i++) {
      if (hk) {
        if (in[i] == ',' || in[i] == ')') {
          qtypes.push_back(mp[rtypes]);
          rtypes = "";
        } else if (in[i] != ' ')
          rtypes.push_back(in[i]);
      }
      if (in[i] == '(') hk = true;
      if (!hk && in[i] != ' ') rname.push_back(in[i]);
    }
    for (int i = 0; i < v.size(); i++) {
      if (rname != v[i].name) continue;
      if (qtypes.size() != v[i].types.size()) continue;
      for (j = 0; j < qtypes.size(); j++) {
        if (qtypes[j] != v[i].types[j] && v[i].types[j] != 4) break;
      }
      if (j < qtypes.size()) continue;
      ans++;
    }
    printf("%d\n", ans);
  }
}
int main() {
  readfunc();
  readvar();
  solve();
  return 0;
}
