#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
map<string, int> np;
map<string, int> kp;
vector<int> ans[1000];
vector<int> f[1000];
int t1 = 0, t2 = 0;
int n, m, k;
void change(string s, int m) {
  bool flag = 0;
  string str;
  int k = s.find("void") + 5;
  for (int i = k; i < s.size(); i++) {
    if (s[i] != ' ' && s[i] != '(')
      str += s[i];
    else if (s[i] == '(')
      break;
  }
  if (mp.count(str) == 0) {
    t1++;
    mp[str] = t1;
  }
  f[m].push_back(mp[str]);
  int start = s.find('(') + 1;
  int end = s.find(')');
  str.clear();
  for (int i = start; i <= end; i++) {
    if (s[i] != ' ' && s[i] != ',' && s[i] != ')') str += s[i];
    if (s[i] == ',' || s[i] == ')') {
      if (np.count(str) == 0) {
        t2++;
        np[str] = t2;
      }
      f[m].push_back(np[str]);
      str.clear();
    }
  }
}
void turn(string s, int m) {
  int j;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != ' ') {
      j = i;
      break;
    }
  }
  string str, ans;
  bool flag = 0;
  for (int i = j; i < s.size(); i++) {
    if (s[i] == ' ' && flag == 0)
      flag = 1;
    else if (s[i] != ' ' && flag == 0)
      str += s[i];
    else if (s[i] != ' ' && flag == 1)
      ans += s[i];
  }
  if (np.count(str) == 0) {
    t2++;
    np[str] = t2;
  }
  kp[ans] = np[str];
}
int answer(string s, int m) {
  int i;
  string str;
  for (i = 0; i < s.size(); i++) {
    if (s[i] != ' ' && s[i] != '(')
      str += s[i];
    else if (s[i] == '(')
      break;
  }
  if (mp.count(str) == 0) {
    return 0;
  }
  ans[m].push_back(mp[str]);
  int start = s.find('(') + 1;
  int end = s.find(')');
  str.clear();
  for (i = start; i <= end; i++) {
    if (s[i] != ' ' && s[i] != ')' && s[i] != ',') str += s[i];
    if (s[i] == ',' || s[i] == ')') {
      if (kp.count(str) == 0) {
        return 0;
      }
      ans[m].push_back(kp[str]);
      str.clear();
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    bool flag = 0;
    if (f[i].size() == ans[m].size()) {
      for (int j = 0; j < f[i].size(); j++) {
        if (f[i][j] != ans[m][j] && f[i][j] != 0) {
          flag = 1;
          break;
        }
      }
      if (flag == 0) {
        sum++;
      }
    }
  }
  return sum;
}
int main() {
  np["T"] = 0;
  int i, j;
  scanf("%d\n", &n);
  for (i = 0; i < n; i++) {
    string s;
    getline(cin, s);
    change(s, i);
  }
  scanf("%d\n", &m);
  for (i = 0; i < m; i++) {
    string s;
    getline(cin, s);
    turn(s, i);
  }
  scanf("%d\n", &k);
  for (i = 0; i < k; i++) {
    string s;
    getline(cin, s);
    cout << answer(s, i) << endl;
  }
}
