#include <bits/stdc++.h>
using namespace std;
template <class T1>
inline void debug(T1 _x) {
  cout << _x << '\n';
}
template <class T1, class T2>
inline void debug(T1 _x, T2 _y) {
  cout << _x << ' ' << _y << '\n';
}
template <class T1, class T2, class T3>
inline void debug(T1 _x, T2 _y, T3 _z) {
  cout << _x << ' ' << _y << ' ' << _z << '\n';
}
template <class T1, class T2, class T3, class T4>
inline void debug(T1 _x, T2 _y, T3 _z, T4 _zz) {
  cout << _x << ' ' << _y << ' ' << _z << ' ' << _zz << '\n';
}
template <class T1>
inline void debug(T1 _array, int _size) {
  cout << "[";
  for (int i = 0; i < _size; ++i) {
    cout << ' ' << _array[i];
  }
  puts(" ]");
}
inline bool CI(int &_x) { return scanf("%d", &_x) == 1; }
inline bool CI(int &_x, int &_y) { return CI(_x) && CI(_y); }
inline bool CI(int &_x, int &_y, int &_z) { return CI(_x) && CI(_y) && CI(_z); }
inline bool CI(int &_x, int &_y, int &_z, int &_zz) {
  return CI(_x) && CI(_y) && CI(_z) && CI(_zz);
}
inline void wait(double seconds) {
  double endtime = clock() + (seconds * CLOCKS_PER_SEC);
  while (clock() < endtime) {
    ;
  }
}
string line;
int fun_count;
int var_count;
int query_count;
string token_type[] = {"int", "string", "double", "T"};
inline int get_type(string word) {
  for (int i = 0, j1 = 4; i < j1; ++i) {
    if (token_type[i] == word) return i;
  }
  return -1;
}
inline vector<string> parse(string mystr) {
  char *sentence = (char *)mystr.c_str();
  char *word;
  static char delim[] = " ,()";
  vector<string> words;
  for (word = strtok(sentence, delim); word; word = strtok(0, delim)) {
    if (strlen(word)) words.push_back(word);
  }
  return words;
}
struct SYMBOL {
  string name;
  int param_count;
  int param_type[6];
} symbol;
struct VARIABLE {
  string name;
  int type;
} VAR;
vector<SYMBOL> symbols;
vector<VARIABLE> symbols_var;
inline void parse_fun(string mystr) {
  vector<string> vi = parse(mystr);
  for (int i = 0, j1 = (int)(vi).size(); i < j1; ++i) {
    if (i) {
      if (i == 1) {
        symbol.name = vi[i];
        symbol.param_count = 0;
      } else {
        int type = get_type(vi[i]);
        if (type != -1) {
          symbol.param_type[symbol.param_count++] = type;
        }
      }
    }
  }
  symbols.push_back(symbol);
}
inline void parse_var(string mystr) {
  vector<string> vi = parse(mystr);
  string name = vi[1];
  int type = get_type(vi[0]);
  VAR.name = name;
  VAR.type = type;
  symbols_var.push_back(VAR);
}
inline int get_sym_type(string name) {
  for (int i = 0, j1 = (int)(symbols_var).size(); i < j1; ++i) {
    if (symbols_var[i].name == name) {
      return symbols_var[i].type;
    }
  }
  return -1;
}
inline int find_suitable(string mystr) {
  vector<string> vi = parse(mystr);
  for (int i = 0, j1 = (int)(vi).size(); i < j1; ++i) {
    if (i == 0) {
      symbol.name = vi[i];
      symbol.param_count = 0;
    } else {
      int type = get_sym_type(vi[i]);
      if (type != -1) {
        symbol.param_type[symbol.param_count++] = type;
      }
    }
  }
  int suitable_count = 0;
  for (int i = 0, j1 = (int)(symbols).size(); i < j1; ++i) {
    if (symbols[i].name == symbol.name) {
      if (symbols[i].param_count == symbol.param_count) {
        bool good = true;
        for (int j = 0, j1 = symbol.param_count; j < j1; ++j) {
          if (symbols[i].param_type[j] == 3 ||
              symbols[i].param_type[j] == symbol.param_type[j]) {
            continue;
          }
          good = false;
          break;
        }
        if (good) ++suitable_count;
      }
    }
  }
  return suitable_count;
}
inline void read() {
  scanf("%d\n", &fun_count);
  for (int i = 0, j1 = fun_count; i < j1; ++i) {
    getline(cin, line);
    parse_fun(line);
  }
  scanf("%d\n", &var_count);
  for (int i = 0, j1 = var_count; i < j1; ++i) {
    getline(cin, line);
    parse_var(line);
  }
  scanf("%d\n", &query_count);
  for (int i = 0, j1 = query_count; i < j1; ++i) {
    getline(cin, line);
    cout << find_suitable(line) << "\n";
  }
}
inline void proc() {}
int main() {
  int kase = 1;
  for (int i = 0, j1 = kase; i < j1; ++i) {
    read();
    proc();
  }
  return 0;
}
