#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
  gn(first);
  gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
  gn(first);
  gn(second);
  gn(z);
}
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <class T>
inline void printsp(T first) {
  print(first);
  putchar(' ');
}
template <class T>
inline void println(T first) {
  print(first);
  putchar('\n');
}
template <class T, class U>
inline void print(T first, U second) {
  printsp(first);
  println(second);
}
template <class T, class U, class V>
inline void print(T first, U second, V z) {
  printsp(first);
  printsp(second);
  println(z);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
struct declarion {
  char name[20];
  char para[6][20];
  int cnt;
} a[1005];
struct call {
  char name[20];
  char para[6][20];
} c;
map<string, string> mp;
char type[20], val[20];
char s[100];
const char _void[] = "void";
const char generic[] = "T";
const char sep[] = ",\t \n)(";
int main() {
  int n, m, k;
  char tmp;
  char tok[10];
  gn(n);
  for (int i = 0; i < n; i++) {
    int nn = 0;
    while (scanf("%c", &tmp) == 1) {
      if (tmp == ')') break;
      s[nn++] = tmp;
    }
    s[nn] = 0;
    int j = -1;
    for (char *tok = strtok(s, sep); tok; tok = strtok(NULL, sep)) {
      if (strcmp(tok, _void) == 0) continue;
      if (j == -1)
        strcpy(a[i].name, tok), j++;
      else
        strcpy(a[i].para[j++], tok);
    }
    a[i].cnt = j;
  }
  gn(m);
  for (int i = 0; i < m; i++) {
    scanf(" %s %s", type, val);
    mp[val] = type;
  }
  gn(k);
  for (int i = 0; i < k; i++) {
    int ans = 0;
    int nn = 0;
    while (scanf("%c", &tmp) == 1) {
      if (tmp == ')') break;
      s[nn++] = tmp;
    }
    s[nn] = 0;
    int j = -1;
    for (char *tok = strtok(s, sep); tok; tok = strtok(NULL, sep)) {
      if (strcmp(tok, _void) == 0) continue;
      if (j == -1)
        strcpy(c.name, tok), j++;
      else
        strcpy(c.para[j++], tok);
    }
    for (int q = 0; q < n; q++) {
      if (strcmp(a[q].name, c.name) == 0 && a[q].cnt == j) {
        int flag = 0;
        for (int z = 0; z < j; z++) {
          if (strcmp(mp[c.para[z]].data(), a[q].para[z]) &&
              strcmp(a[q].para[z], generic))
            flag = 1;
        }
        if (flag == 0) ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
