#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000, maxk = 5, maxs = 11;
int kargs[maxn];
char args[maxn][maxk][maxs];
char names[maxn][maxs];
char name_var[maxn][maxs], type_var[maxn][maxs];
char cur_args_type[maxk][maxs];
char cur_name[maxs];
char cur_args[maxk][maxs];
int main() {
  char ch;
  int n;
  while (scanf("%d", &n) >= 1) {
    for (int i = 0; i < n; i++) {
      scanf("%*s");
      scanf(" %[a-z0-9]s", names[i]);
      for (kargs[i] = 0; scanf(" %c", &ch) >= 1 && ch != ')'; kargs[i]++)
        scanf(" %[Ta-z]s", args[i][kargs[i]]);
    }
    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      scanf("%s%s", type_var[i], name_var[i]);
    }
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; i++) {
      scanf(" %[a-z0-9]s", cur_name);
      int k;
      for (k = 0; scanf(" %c", &ch) >= 1 && ch != ')'; k++) {
        scanf(" %[Ta-z0-9]s", cur_args[k]);
        int j;
        for (j = 0; strcmp(name_var[j], cur_args[k]); j++)
          ;
        for (int i = 0; i < maxs; i++) cur_args_type[k][i] = type_var[j][i];
        assert(j < m);
      }
      int res = 0;
      for (int y = 0; y < n; y++) {
        if (strcmp(cur_name, names[y]) || kargs[y] != k) continue;
        bool fail = 0;
        for (int j = 0; j < k; j++)
          if (strcmp(args[y][j], "T") && strcmp(cur_args_type[j], args[y][j])) {
            fail = 1;
            break;
          }
        if (fail) continue;
        res++;
      }
      printf("%d\n", res);
    }
  }
  return 0;
}
