#include <bits/stdc++.h>
using namespace std;
struct Function {
  string name;
  vector<string> args;
  string str() {
    string s = name;
    s += "(";
    for (int i = 0; i < args.size(); i++) {
      s += args[i] + " ";
    }
    s += ")";
    return s;
  }
};
struct Varible {
  string name;
  string type;
};
vector<Function> funs;
vector<Varible> vars;
map<string, string> tp;
bool debug;
inline Function parseFunction(string s) {
  if (debug) cout << "parseFunction:" << s << endl;
  Function op;
  int spaces = 0;
  while (s[spaces] == ' ') spaces++;
  string name = "";
  int i;
  for (i = 5 + spaces; i < s.size(); i++) {
    if (s[i] == ' ') continue;
    if (s[i] == '(') break;
    name += s[i];
  }
  if (debug) cout << "Name is:" << name << endl;
  op.name = name;
  string tmp;
  for (i++; i < s.size(); i++) {
    if (s[i] == ' ') continue;
    if (s[i] == ',' || s[i] == ')') {
      if (debug) cout << "Found args:" << tmp << endl;
      op.args.push_back(tmp);
      tmp = "";
      continue;
    }
    tmp += s[i];
  }
  return op;
}
inline Varible parseVarible(string s) {
  Varible op;
  string type = "", name = "";
  int i = 0;
  while (s[i] == ' ') i++;
  for (; i < s.size(); i++) {
    if (s[i] == ' ') break;
    type += s[i];
  }
  for (i++; i < s.size(); i++) {
    if (s[i] == ' ') continue;
    name += s[i];
  }
  op.name = name;
  op.type = type;
  if (debug) cout << "FOUND type&name=" << type << "&" << name << endl;
  tp[name] = type;
  return op;
}
char tmp[10005];
inline bool match(Function f1, Function f2) {
  if (debug) {
    cout << "Matching:" << f1.str() << " and " << f2.str() << endl;
  }
  if (f1.name != f2.name) return false;
  if (f1.args.size() != f2.args.size()) return false;
  for (int i = 0; i < f1.args.size(); i++) {
    if (f2.args[i] == "T") continue;
    if (f1.args[i] != f2.args[i]) return false;
  }
  return true;
}
inline int matchCount(Function f) {
  int cnt = 0;
  for (int i = 0; i < funs.size(); i++) {
    if (match(f, funs[i])) {
      cnt++;
      if (debug) cout << "ok Right answer" << endl;
    } else {
      if (debug) cout << "wrong answer Bad Compare" << endl;
    }
  }
  return cnt;
}
inline Function parseEnterFunction(string s) {
  if (debug) cout << "parseEnterFunction:" << s << endl;
  int p = 0;
  while (s[p] == ' ') p++;
  Function op;
  for (int i = p; i < s.size(); i++) {
    p = i;
    if (s[i] == '(') break;
    if (s[i] == ' ') continue;
    op.name += s[i];
  }
  if (debug) cout << "Found name=" << op.name << endl;
  p++;
  string var;
  for (int i = p; i < s.size(); i++) {
    if (s[i] == ' ') continue;
    if (s[i] == ',' || s[i] == ')') {
      op.args.push_back(tp[var]);
      if (debug) cout << "ARGS:" << var << "->" << tp[var] << endl;
      var = "";
      continue;
    }
    var += s[i];
  }
  return op;
}
int main(int argc, char* argv[]) {
  int n, m, t;
  cin >> n;
  debug = false;
  gets(tmp);
  for (int i = 0; i < n; i++) {
    string s;
    gets(tmp);
    s = tmp;
    funs.push_back(parseFunction(s));
  }
  cin >> m;
  gets(tmp);
  for (int i = 0; i < m; i++) {
    string s;
    gets(tmp);
    s = tmp;
    vars.push_back(parseVarible(s));
  }
  cin >> t;
  gets(tmp);
  for (int i = 0; i < t; i++) {
    string s;
    gets(tmp);
    s = tmp;
    Function f = parseEnterFunction(s);
    cout << matchCount(f) << endl;
  }
  return 0;
}
