#include <bits/stdc++.h>
using namespace std;
const int N = 105;
const int M = 1005;
struct node {
  char name[20];
  int item[10];
} s[M], ans;
char a[N], tmp[N], x[N], y[N];
map<string, int> mmap;
string nam;
int n;
int check() {
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (strcmp(ans.name, s[i].name) == 0) {
      for (int j = 2; j < 10; j++) {
        if (ans.item[j] == 0 && s[i].item[j] == 0) ret++;
        if (ans.item[j] == 0 || s[i].item[j] == 0) break;
        if (ans.item[j] != 4 && s[i].item[j] != 4 &&
            ans.item[j] != s[i].item[j]) {
          break;
        }
      }
    }
  }
  return ret;
}
int main() {
  scanf("%d", &n);
  getchar();
  for (int i = 0; i < n; i++) {
    gets(a);
    int len = strlen(a);
    int size = 0, cnt = -1;
    for (int j = 0; j < len; j++) {
      if (a[j] == ' ' || a[j] == ',' || a[j] == '(' || a[j] == ')') {
        if (size) {
          tmp[size] = 0;
          size = 0;
          cnt++;
        } else {
          continue;
        }
      } else {
        tmp[size++] = a[j];
        continue;
      }
      if (cnt == 1) {
        strcpy(s[i].name, tmp);
      } else if (cnt > 1) {
        if (tmp[0] == 'i') {
          s[i].item[cnt] = 1;
        } else if (tmp[0] == 'd') {
          s[i].item[cnt] = 2;
        } else if (tmp[0] == 's') {
          s[i].item[cnt] = 3;
        } else if (tmp[0] == 'T') {
          s[i].item[cnt] = 4;
        }
      }
    }
  }
  int m;
  scanf("%d", &m);
  while (m-- > 0) {
    scanf("%s %s", x, y);
    if (x[0] == 'i')
      mmap[y] = 1;
    else if (x[0] == 'd')
      mmap[y] = 2;
    else if (x[0] == 's')
      mmap[y] = 3;
    else if (x[0] == 'T')
      mmap[y] = 4;
  }
  scanf("%d", &m);
  getchar();
  while (m-- > 0) {
    gets(a);
    int len = strlen(a);
    int size = 0, cnt = 0;
    string nam;
    for (int j = 0; j < 10; j++) ans.item[j] = 0;
    for (int j = 0; j < len; j++) {
      if (a[j] == ' ' || a[j] == ',' || a[j] == '(' || a[j] == ')') {
        if (size) {
          tmp[size] = 0;
          size = 0;
          cnt++;
        } else {
          continue;
        }
      } else {
        tmp[size++] = a[j];
        continue;
      }
      if (cnt == 1) {
        strcpy(ans.name, tmp);
      } else if (cnt > 1) {
        nam = "";
        for (int k = 0; tmp[k]; k++) nam += tmp[k];
        ans.item[cnt] = mmap[nam];
      }
    }
    printf("%d\n", check());
  }
  return 0;
}
