#include <bits/stdc++.h>
#pragma warning(disable : 4786)
using namespace std;
struct node {
  string name, type[6];
  int len;
} p[1010], x;
char s[110];
map<string, string> q;
int cmpx(node p, node x) {
  if (p.len != x.len) return 0;
  for (int i = 0; i < x.len; i++) {
    if (p.type[i] == "T") continue;
    if (p.type[i] != q[x.type[i]]) return 0;
  }
  return 1;
}
int main() {
  int n;
  scanf("%d", &n);
  int i;
  getchar();
  for (i = 0; i < n; i++) {
    gets(s);
    int j;
    string str = "";
    int len = strlen(s);
    int f = 0, ll = 0;
    p[i].len = 0;
    for (j = 0; j <= len; j++) {
      if (j == len || s[j] == ' ' || s[j] == ',' || s[j] == ')' ||
          s[j] == '(') {
        if (str == "") continue;
        if (str == "void")
          f = 1;
        else if (f == 1) {
          p[i].name = str;
          f = 2;
        } else {
          p[i].type[ll++] = str;
        }
        str = "";
      } else
        str += s[j];
    }
    p[i].len = ll;
  }
  int m;
  scanf("%d", &m);
  getchar();
  for (i = 0; i < m; i++) {
    gets(s);
    string str[2];
    str[0] = "";
    str[1] = "";
    int j, l, f = 0;
    l = strlen(s);
    for (j = 0; j <= l; j++) {
      if (j == l || s[j] == ' ') {
        if (str[f] == "") continue;
        f++;
        if (f >= 2) break;
      } else
        str[f] += s[j];
    }
    q[str[1]] = str[0];
  }
  int k;
  scanf("%d", &k);
  getchar();
  for (i = 0; i < k; i++) {
    gets(s);
    int j;
    string str = "";
    int len = strlen(s);
    int f = 1, ll = 0;
    x.len = 0;
    for (j = 0; j <= len; j++) {
      if (j == len || s[j] == ' ' || s[j] == ',' || s[j] == ')' ||
          s[j] == '(') {
        if (str == "") continue;
        if (f == 1) {
          x.name = str;
          f = 2;
        } else {
          x.type[ll++] = str;
        }
        str = "";
      } else
        str += s[j];
    }
    x.len = ll;
    int ans = 0;
    for (j = 0; j < n; j++) {
      if (p[j].name != x.name) continue;
      if (cmpx(p[j], x)) {
        ans++;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
