#include <bits/stdc++.h>
using namespace std;
long long n, type[10001][6], type1[10001][6], nn[10001], nn1[10001], m, k;
map<string, int> ma;
string name[1001], name1[1001], s, rem;
int main() {
  cin >> n;
  getline(cin, s);
  for (int i = 0; i < n; i++) {
    getline(cin, s);
    int len = s.length();
    for (int j = 0; j < len; j++)
      if (s[j] != ' ') {
        if (s[j] != '(')
          if (name[i] == "")
            rem += s[j];
          else {
          }
        else
          name[i] = rem.substr(4, rem.length() - 4), rem = "";
        if (name[i] != "" && s[j] != '(')
          if (s[j] == ')' || s[j] == ',') {
            if (rem == "string") type[i][nn[i]] = 1;
            if (rem == "double") type[i][nn[i]] = 2;
            if (rem == "int") type[i][nn[i]] = 3;
            if (rem == "T") type[i][nn[i]] = 4;
            nn[i]++;
            rem = "";
          } else
            rem += s[j];
      }
  }
  cin >> m;
  getline(cin, s);
  for (int i = 0; i < m; i++) {
    string str1 = "", str2 = "";
    int uk = 0;
    getline(cin, s);
    while (s[uk] == ' ') uk++;
    while (s[uk] != ' ') str1 += s[uk], uk++;
    while (s[uk] == ' ') uk++;
    while (uk < s.length() && s[uk] != ' ') str2 += s[uk], uk++;
    if (str1 == "string") ma[str2] = 1;
    if (str1 == "double") ma[str2] = 2;
    if (str1 == "int") ma[str2] = 3;
  }
  cin >> k;
  getline(cin, s);
  for (int i = 0; i < k; i++) {
    getline(cin, s);
    int len = s.length();
    for (int j = 0; j < len; j++)
      if (s[j] != ' ') {
        if (s[j] != '(')
          if (name1[i] == "")
            rem += s[j];
          else {
          }
        else
          name1[i] = rem, rem = "";
        if (name1[i] != "" && s[j] != '(')
          if (s[j] == ')' || s[j] == ',') {
            type1[i][nn1[i]] = ma[rem];
            nn1[i]++;
            rem = "";
          } else
            rem += s[j];
      }
  }
  for (int i = 0; i < k; i++) {
    int ans = 0;
    for (int j = 0; j < n; j++)
      if (name1[i] == name[j] && nn1[i] == nn[j]) {
        bool flag = true;
        for (int g = 0; g < nn1[i]; g++)
          if (type[j][g] != 4 && type1[i][g] != type[j][g]) flag = false;
        if (flag) ans++;
      }
    cout << ans << endl;
  }
  cin >> n;
  return 0;
}
