#include <bits/stdc++.h>
using namespace std;
vector<pair<string, vector<string> > > procs;
pair<string, vector<string> > curr;
map<string, string> params;
int find_all() {
  int sol = 0;
  bool ok;
  for (int i = 0; i < procs.size(); ++i) {
    ok = true;
    if (procs[i].first != curr.first) continue;
    if (procs[i].second.size() != curr.second.size()) continue;
    for (int j = 0; j < curr.second.size(); ++j) {
      if (procs[i].second[j] != "T" &&
          params[curr.second[j]] != procs[i].second[j]) {
        ok = false;
        break;
      }
    }
    sol += ok;
  }
  return sol;
}
int main(void) {
  int n, m, k;
  char str[110], str2[110], c;
  scanf("%d ", &n);
  procs.resize(n);
  for (int i = 0; i < n; ++i) {
    scanf(" void %[^ (] (", str);
    procs[i].first = str;
    do {
      scanf(" %[^ ,)]", str2);
      procs[i].second.push_back(str2);
      scanf(" %c", &c);
    } while (c == ',');
  }
  scanf("%d", &m);
  for (int i = 0; i < m; ++i) {
    scanf(" %s %s", str, str2);
    params[str2] = str;
  }
  scanf("%d", &k);
  for (int i = 0; i < k; ++i) {
    scanf(" %[^ (] (", str);
    curr.first = str;
    curr.second.clear();
    do {
      scanf(" %[^ ,)]", str2);
      curr.second.push_back(str2);
      scanf(" %c", &c);
    } while (c == ',');
    printf("%d\n", find_all());
  }
  return 0;
}
