#include <bits/stdc++.h>
using namespace std;
struct fun {
  string ret;
  string name;
  vector<string> ff;
  int int_, string_, double_, T_, c;
};
vector<fun> funs;
fun tt;
map<string, string> mp;
fun parse_fun(string x, int zx) {
  fun ret;
  ret.double_ = ret.int_ = ret.string_ = ret.T_ = ret.c = 0;
  int f = 0;
  if (zx) f++;
  string tmp = "";
  for (int i = 0; i < x.size(); i++) {
    if (x[i] == ' ') {
      if (f == 0) {
        f = 1;
        ret.ret = tmp;
        tmp = "";
      }
      continue;
    }
    if (x[i] == '(') {
      if (f == 1) {
        f = 2;
        ret.name = tmp;
        tmp = "";
      }
      continue;
    }
    if (x[i] == ',') {
      if (f == 2) {
        if (tmp == "int")
          ret.int_++;
        else if (tmp == "string")
          ret.string_++;
        else if (tmp == "double")
          ret.double_++;
        else
          ret.T_++;
        ret.ff.push_back(tmp);
        ret.c++;
        tmp = "";
      }
      continue;
    }
    if (x[i] == ')') {
      if (f == 2) {
        if (tmp == "int")
          ret.int_++;
        else if (tmp == "string")
          ret.string_++;
        else if (tmp == "double")
          ret.double_++;
        else
          ret.T_++;
        ret.ff.push_back(tmp);
        ret.c++;
        tmp = "";
        break;
      }
    }
    tmp += x[i];
  }
  return ret;
}
int main() {
  int n;
  scanf("%d\n", &n);
  char tmp[1111];
  funs.resize(1111);
  for (int i = 0; i < n; i++) {
    gets(tmp);
    string x = tmp;
    while (x[0] == ' ') x = x.substr(1);
    funs[i] = parse_fun(x, 0);
  }
  int m;
  scanf("%d\n", &m);
  char zz[111], aa[111];
  for (int i = 0; i < m; i++) {
    gets(tmp);
    string x = tmp;
    while (x[0] == ' ') x = x.substr(1);
    sscanf(x.c_str(), "%s %s", zz, aa);
    mp[aa] = zz;
  }
  int z;
  scanf("%d\n", &z);
  vector<string> W;
  for (int i = 0; i < z; i++) {
    gets(tmp);
    string x = tmp;
    while (x[0] == ' ') x = x.substr(1);
    tt = parse_fun(x, 1);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      W.clear();
      if (funs[i].name == tt.name) {
        bool cant = 0;
        int k = 0;
        for (k = 0; k < tt.ff.size(); k++) {
          if (mp.count(tt.ff[k]) == 0) {
            cant = 1;
            break;
          }
          if (k < funs[i].ff.size() && mp[tt.ff[k]] != funs[i].ff[k] &&
              funs[i].ff[k] != "T") {
            cant = 1;
            break;
          }
        }
        if (cant == 0 && k == tt.ff.size() && funs[i].c == tt.c) cnt++;
      }
    }
    printf("%d\n", cnt);
  }
}
