#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
const long long INF64 = (long long)1e18;
const long double eps = 1e-9;
const long double pi = 3.14159265358979323846;
bool ineedoutputfile = false;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
struct procedure {
  string name;
  vector<string> arg;
};
int main() {
  vector<procedure> p;
  int n;
  cin >> n;
  scanf("\n");
  for (int i = 0; i < n; i++) {
    string s;
    getline(cin, s);
    procedure temp;
    string to;
    bool ifread = false;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == ' ') {
        continue;
      }
      if (s[j] == '(') {
        temp.name = to;
        to = "";
        continue;
      }
      if (s[j] == ',' || s[j] == ')') {
        temp.arg.push_back(to);
        to = "";
        continue;
      }
      to += s[j];
      if (to == "void" && !ifread) {
        ifread = true;
        to = "";
        continue;
      }
    }
    p.push_back(temp);
  }
  int k;
  cin >> k;
  scanf("\n");
  map<string, string> var;
  for (int i = 0; i < k; i++) {
    string a, b;
    cin >> a >> b;
    var[b] = a;
  }
  int m;
  cin >> m;
  scanf("\n");
  for (int i = 0; i < m; i++) {
    string s;
    getline(cin, s);
    procedure temp;
    string to;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == ' ') {
        continue;
      }
      if (s[j] == '(') {
        temp.name = to;
        to = "";
        continue;
      }
      if (s[j] == ',' || s[j] == ')') {
        temp.arg.push_back(to);
        to = "";
        continue;
      }
      to += s[j];
    }
    int ans = 0;
    for (int j = 0; j < p.size(); j++) {
      if (p[j].name == temp.name && (p[j].arg).size() == (temp.arg).size()) {
        int u = (p[j].arg).size();
        bool ok = true;
        for (int h = 0; h < u; h++) {
          string rt = var[(temp.arg)[h]];
          string er = (p[j].arg)[h];
          if (er != rt) ok = ok && (er == "T");
        }
        ans += ok;
      }
    }
    cout << ans << endl;
  }
}
