#include <bits/stdc++.h>
using namespace std;
map<string, vector<string> > mp;
int n, m, k;
void readpro() {
  scanf("%d", &n);
  fgetc(stdin);
  for (int i = 1; i <= n; i++) {
    string tmp;
    char c;
    while ((c = fgetc(stdin)) != '\n') tmp += c;
    int l = 0, r = 0;
    for (l = 0; tmp[l] != '('; l++)
      ;
    for (r = 0; tmp[r] != ')'; r++)
      ;
    int j = 0;
    for (j = 0; tmp[j] != 'v'; j++)
      ;
    j += 4;
    string proname = "";
    for (; j != l; j++)
      if (tmp[j] != ' ') proname += tmp[j];
    string type = "";
    for (l++; l != r; l++) {
      if (tmp[l] == ' ' || tmp[l] == ',') continue;
      if (tmp[l] == 'T') type += 't';
      if (tmp[l] == 'i') type += 'i', l += 2;
      if (tmp[l] == 'd') type += 'd', l += 5;
      if (tmp[l] == 's') type += 's', l += 5;
    }
    vector<string> v = mp[proname];
    v.push_back(type);
    mp[proname] = v;
  }
}
map<string, char> tp;
void readval() {
  scanf("%d", &m);
  fgetc(stdin);
  for (int i = 1; i <= m; i++) {
    string tmp;
    char c;
    while ((c = fgetc(stdin)) != '\n') tmp += c;
    int l = 0;
    for (l = 0; tmp[l] != 'i' && tmp[l] != 'd' && tmp[l] != 's'; l++)
      ;
    char type;
    if (tmp[l] == 'i') type = 'i', l += 3;
    if (tmp[l] == 's') type = 's', l += 6;
    if (tmp[l] == 'd') type = 'd', l += 6;
    int len = (int)tmp.length();
    string valname = "";
    while (l < len) {
      if (tmp[l] != ' ') valname += tmp[l];
      l++;
    }
    tp[valname] = type;
  }
}
void pro() {
  scanf("%d", &k);
  fgetc(stdin);
  for (int i = 1; i <= k; i++) {
    string tmp;
    char c;
    while ((c = fgetc(stdin)) != '\n') tmp += c;
    int l = 0;
    for (l = 0; tmp[l] == ' '; l++)
      ;
    string proname = "";
    for (; tmp[l] != ' ' && tmp[l] != '('; l++) proname += tmp[l];
    string type = "";
    for (; tmp[l] != '('; l++)
      ;
    for (l++; tmp[l] != ')'; l++) {
      if (tmp[l] == ' ' || tmp[l] == ',') continue;
      string valname = "";
      for (; tmp[l] != ' ' && tmp[l] != ',' && tmp[l] != ')'; l++)
        valname += tmp[l];
      type += tp[valname];
      if (tmp[l] == ')') break;
    }
    vector<string> v = mp[proname];
    int cnt = 0;
    for (int j = 0; j < (int)v.size(); j++) {
      bool can = true;
      if (v[j].size() != type.size()) continue;
      for (int r = 0; r < (int)v[j].size(); r++) {
        if (v[j][r] == 't') continue;
        if (v[j][r] != type[r]) {
          can = false;
          break;
        }
      }
      if (can) cnt++;
    }
    printf("%d\n", cnt);
  }
}
int main() {
  readpro();
  readval();
  pro();
  return 0;
}
