#include <bits/stdc++.h>
using namespace std;
struct functie {
  char nume[501];
  int par[10], nrPar;
};
struct var {
  char nume[501];
  int tip;
};
char x[1001], nvar[1001], nquery[1001], varAct[1001];
functie functii[1001];
var vari[1001];
char nfact[1001];
int n, i, j, inceput, b, nrF, nrVar, tip, lim, k, nP, l, tipuri[101], m, contor;
int main() {
  cin >> n;
  cin.getline(x, 10);
  for (i = 1; i <= n; i++) {
    cin.getline(x, 1001);
    int p = strlen(x);
    for (j = 0; j < p; j++) {
      if (x[j] == 'd') {
        inceput = j;
        break;
      }
    }
    b = 0;
    for (j = inceput + 1; j < p; j++) {
      while (isalpha(x[j]) || isdigit(x[j])) {
        nfact[b] = x[j];
        b++;
        j++;
      }
      if (x[j] == '(') break;
    }
    nfact[b] = 0;
    nrF++;
    strcpy(functii[nrF].nume, nfact);
    functii[nrF].nrPar = 1;
    for (j = j + 1; j < p; j++) {
      if (x[j] == ',') functii[nrF].nrPar++;
      if (x[j] == 'i' && x[j + 1] == 'n' && x[j + 2] == 't') {
        functii[nrF].par[functii[nrF].nrPar] = 1;
      }
      if (x[j] == 's' && x[j + 1] == 't' && x[j + 2] == 'r' &&
          x[j + 3] == 'i' && x[j + 4] == 'n' && x[j + 5] == 'g') {
        functii[nrF].par[functii[nrF].nrPar] = 2;
      }
      if (x[j] == 'd' && x[j + 1] == 'o' && x[j + 2] == 'u' &&
          x[j + 3] == 'b' && x[j + 4] == 'l' && x[j + 5] == 'e') {
        functii[nrF].par[functii[nrF].nrPar] = 3;
      }
      if (x[j] == 'T') {
        functii[nrF].par[functii[nrF].nrPar] = 4;
      }
    }
  }
  cin >> m;
  cin.getline(x, 10);
  for (i = 1; i <= m; i++) {
    cin.getline(x, 1001);
    int p = strlen(x);
    tip = 0;
    for (j = 0; j < p; j++) {
      if (x[j] == 'i' && x[j + 1] == 'n' && x[j + 2] == 't') {
        tip = 1;
        lim = 3;
        break;
      } else if (x[j] == 's' && x[j + 1] == 't' && x[j + 2] == 'r' &&
                 x[j + 3] == 'i' && x[j + 4] == 'n' && x[j + 5] == 'g') {
        tip = 2;
        lim = 5;
        break;
      } else if (x[j] == 'd' && x[j + 1] == 'o' && x[j + 2] == 'u' &&
                 x[j + 3] == 'b' && x[j + 4] == 'l' && x[j + 5] == 'e') {
        tip = 3;
        lim = 5;
        break;
      }
    }
    b = 0;
    for (j = j + lim + 1; j < p; j++) {
      while (isalpha(x[j]) || isdigit(x[j])) {
        nvar[b] = x[j];
        b++;
        j++;
      }
    }
    nvar[b] = 0;
    nrVar++;
    strcpy(vari[nrVar].nume, nvar);
    vari[nrVar].tip = tip;
  }
  cin >> k;
  cin.getline(x, 10);
  for (i = 1; i <= k; i++) {
    contor = 0;
    cin.getline(x, 1001);
    int p = strlen(x);
    b = 0;
    for (j = 0; j < p; j++) {
      while (isalpha(x[j]) || isdigit(x[j])) {
        nquery[b] = x[j];
        b++;
        j++;
      }
      if (x[j] == '(') {
        break;
      }
    }
    nquery[b] = 0;
    b = 0;
    nP = 0;
    for (j = j + 1; j < p; j++) {
      while (isdigit(x[j]) || isalpha(x[j])) {
        varAct[b] = x[j];
        b++;
        j++;
      }
      if (x[j] == ',' || x[j] == ')') {
        nP++;
        varAct[b] = 0;
        for (l = 1; l <= nrVar; l++) {
          if (strcmp(varAct, vari[l].nume) == 0) {
            tipuri[nP] = vari[l].tip;
          }
        }
        b = 0;
      }
    }
    for (l = 1; l <= nrF; l++) {
      bool ok = false;
      if (strcmp(nquery, functii[l].nume) == 0) {
        if (nP == functii[l].nrPar) {
          ok = true;
          for (j = 1; j <= functii[l].nrPar; j++) {
            if (functii[l].par[j] != tipuri[j] && functii[l].par[j] != 4) {
              ok = false;
            }
          }
        }
      }
      if (ok) contor++;
    }
    cout << contor << "\n";
  }
}
