#include <bits/stdc++.h>
using namespace std;
char str[10000];
int n, m, Q;
map<string, int> mem;
map<string, int>::iterator iter;
struct Func {
  string name;
  int num;
  int v[10];
  bool operator==(const Func& A) const {
    if (name != A.name) return false;
    if (num != A.num) return false;
    for (int i = 0; i < num; ++i)
      if (v[i] * A.v[i] != 0 && v[i] != A.v[i]) return false;
    return true;
  }
} g[2000], pre;
int Type(string x) {
  if (x == "T") return 0;
  if (x == "int") return 1;
  if (x == "double") return 2;
  if (x == "string") return 3;
  return -1;
}
void inputFunc(int gs) {
  gets(str);
  int i = 0, j = 0, len = strlen(str);
  while (str[j] != 'd') ++j;
  ++j;
  while (j < len) {
    if (str[j] != ' ') str[i++] = str[j];
    j++;
  }
  len = i;
  i = 0;
  g[gs].name = "";
  while (str[i] != '(') {
    g[gs].name += str[i];
    i++;
  }
  string temp = "";
  i++;
  for (;;) {
    if (str[i] == ',' || str[i] == ')') {
      g[gs].v[g[gs].num++] = Type(temp);
      temp = "";
      if (str[i] == ')') break;
    } else
      temp += str[i];
    i++;
  }
}
void inputType() {
  string a, b;
  cin >> a >> b;
  mem[b] = Type(a);
}
void solve() {
  gets(str);
  int i = 0, j = 0, len = strlen(str);
  for (j = 0; j < len; ++j) {
    if (str[j] != ' ') str[i++] = str[j];
  }
  len = i;
  i = 0;
  pre.name = "";
  pre.num = 0;
  while (str[i] != '(') {
    pre.name += str[i];
    i++;
  }
  string temp = "";
  i++;
  for (;;) {
    if (str[i] == ',' || str[i] == ')') {
      pre.v[pre.num++] = mem.find(temp)->second;
      temp = "";
      if (str[i] == ')') break;
    } else
      temp += str[i];
    i++;
  }
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if (pre == g[i]) ++ans;
  printf("%d\n", ans);
}
int main() {
  scanf("%d", &n);
  gets(str);
  for (int i = 0; i < n; ++i) inputFunc(i);
  scanf("%d", &m);
  gets(str);
  mem.clear();
  while (m--) inputType();
  scanf("%d", &Q);
  gets(str);
  while (Q--) solve();
}
