#include <bits/stdc++.h>
using namespace std;
char fun[1003][300];
void format(int k) {
  int j = 0;
  int len = strlen(fun[k]);
  for (int i = 0; i < len; i++) {
    if (fun[k][i] != ' ') {
      fun[k][j++] = fun[k][i];
    }
  }
  fun[k][j] = 0;
}
map<string, string> type;
char str[300];
bool cmp(string a, string b) {
  int i = 0;
  int j = 0;
  while (a[i] != '(') {
    i++;
  }
  i++;
  while (b[j] != '(') {
    j++;
  }
  j++;
  if (a.substr(0, i) != b.substr(0, j)) {
    return false;
  }
  string x, y;
  while (i < a.length() && j < b.length()) {
    x = y = "";
    while (a[i] != ',' && a[i] != ')') {
      x += a[i];
      i++;
    }
    i++;
    while (b[j] != ',' && b[j] != ')') {
      y += b[j];
      j++;
    }
    j++;
    if (x != "T" && x != y) {
      return false;
    }
  }
  return i == a.length() && j == b.length();
}
int gao(int n) {
  int j = 0;
  int len = strlen(str);
  for (int i = 0; i < len; i++) {
    if (str[i] != ' ') {
      str[j++] = str[i];
    }
  }
  str[j] = 0;
  len = j;
  j = 0;
  string v = "";
  string s = "void";
  for (int i = 0; i < len; i++) {
    if (str[i] == '(') {
      v += str[i];
      s += v;
      v = "";
    } else if (str[i] == ',' || str[i] == ')') {
      s += type[v] + str[i];
      v = "";
    } else {
      v += str[i];
    }
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    ret += cmp(string(fun[i]), s);
  }
  return ret;
}
int main() {
  int n;
  scanf("%d", &n);
  getchar();
  for (int i = 0; i < n; i++) {
    gets(fun[i]);
    format(i);
  }
  type.clear();
  int m;
  scanf("%d", &m);
  while (m--) {
    scanf("%s%s", str, str + 150);
    type[str + 150] = str;
  }
  scanf("%d", &m);
  getchar();
  while (m--) {
    gets(str);
    int ans = gao(n);
    printf("%d\n", ans);
  }
  return 0;
}
