#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char st[1005][105], par[1005][105];
int stb[1005][10], parb[1005];
int cal(char *s) {
  if (strcmp(s, "int") == 0) return 0;
  if (strcmp(s, "double") == 0) return 1;
  if (strcmp(s, "string") == 0) return 2;
  return 3;
}
void initp() {
  int i, j, e;
  char ch[105], tp[105];
  scanf("%d\n", &n);
  for (i = 0; i < n; i++) {
    gets(ch);
    j = 0;
    while (ch[j] != 'v') j++;
    j += 4;
    e = 0;
    while (ch[j]) {
      if (ch[j] != ' ') tp[e++] = ch[j];
      j++;
    }
    tp[e] = '\0';
    j = e = 0;
    while (tp[j] != '(') st[i][e++] = tp[j++];
    st[i][e] = '\0';
    j++;
    while (tp[j]) {
      if (tp[j] == 'T') {
        stb[i][++stb[i][0]] = 4;
        j += 2;
      } else if (tp[j] == 'i') {
        stb[i][++stb[i][0]] = 0;
        j += 4;
      } else if (tp[j] == 'd') {
        stb[i][++stb[i][0]] = 1;
        j += 7;
      } else {
        stb[i][++stb[i][0]] = 2;
        j += 7;
      }
    }
  }
}
void initv() {
  int i;
  char s[105];
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    cin >> s;
    parb[i] = cal(s);
    cin >> par[i];
  }
}
void solve() {
  int i, j, e, p, ans, ty[10];
  char s[105], tp[105], ca[105];
  scanf("%d\n", &k);
  for (i = 0; i < k; i++) {
    gets(s);
    j = e = 0;
    while (s[j]) {
      if (s[j] != ' ') tp[e++] = s[j];
      j++;
    }
    tp[e] = '\0';
    j = 0;
    while (tp[j] != '(') {
      s[j] = tp[j];
      j++;
    }
    s[j] = '\0';
    j++;
    ty[0] = 0;
    p = 0;
    while (tp[j]) {
      e = 0;
      while (tp[j] != ',' && tp[j] != ')') ca[e++] = tp[j++];
      ca[e] = '\0';
      j++;
      p++;
      for (e = 0; e < m; e++)
        if (strcmp(par[e], ca) == 0) {
          ty[++ty[0]] = parb[e];
          break;
        }
    }
    ans = 0;
    if (p != ty[0]) {
      cout << 0 << endl;
      continue;
    }
    for (j = 0; j < n; j++)
      if (strcmp(st[j], s) == 0 && p == stb[j][0]) {
        for (e = 1; e <= p; e++)
          if (stb[j][e] == ty[e] || stb[j][e] == 4)
            continue;
          else
            break;
        if (e > p) ans++;
      }
    cout << ans << endl;
  }
}
int main() {
  initp();
  initv();
  solve();
  return 0;
}
