#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double pi = acos(double(-1));
const int inf = int(1e9);
char s[100], buf[100];
string procedureName[1010];
vector<string> types[1010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", buf);
    gets(s);
    bool first = true;
    int len = strlen(s), j = 0;
    while (j < len) {
      if ((s[j] >= 'a' && s[j] <= 'z') || (s[j] >= 'A' && s[j] <= 'Z') ||
          (s[j] >= '0' && s[j] <= '9')) {
        string cur = "";
        while (j < len &&
               ((s[j] >= 'a' && s[j] <= 'z') || (s[j] >= 'A' && s[j] <= 'Z') ||
                (s[j] >= '0' && s[j] <= '9'))) {
          cur += s[j++];
        }
        if (first) {
          procedureName[i] = cur;
          first = false;
        } else {
          types[i].push_back(cur);
        }
        continue;
      }
      j++;
    }
  }
  int m;
  scanf("%d", &m);
  map<string, string> type;
  for (int i = 0; i < m; i++) {
    scanf("%s %s", buf, s);
    type[s] = buf;
  }
  int k;
  scanf("%d\n", &k);
  for (int i = 0; i < k; i++) {
    gets(s);
    vector<string> need;
    string name;
    bool first = true;
    int len = strlen(s), j = 0;
    while (j < len) {
      if ((s[j] >= 'a' && s[j] <= 'z') || (s[j] >= 'A' && s[j] <= 'Z') ||
          (s[j] >= '0' && s[j] <= '9')) {
        string cur = "";
        while (j < len &&
               ((s[j] >= 'a' && s[j] <= 'z') || (s[j] >= 'A' && s[j] <= 'Z') ||
                (s[j] >= '0' && s[j] <= '9'))) {
          cur += s[j++];
        }
        if (first) {
          name = cur;
          first = false;
        } else {
          need.push_back(type[cur]);
        }
        continue;
      }
      j++;
    }
    int ans = 0;
    for (int j = 0; j < n; j++) {
      if (procedureName[j] == name &&
          int((types[j]).size()) == int((need).size())) {
        bool fl = true;
        for (int z = 0; z < int((need).size()); z++) {
          if (types[j][z] == "T" || need[z] == types[j][z]) {
            continue;
          }
          fl = false;
          break;
        }
        if (fl) {
          ans++;
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
