#include <bits/stdc++.h>
using namespace std;
vector<int> adia[1000010];
string name[1000000];
int cnt;
vector<pair<int, string>> trans;
vector<string> query;
int act;
void get_trans();
int get_arb();
int calc_query(int nod, int act);
void get_query();
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  get_trans();
  while (act != trans.size()) adia[0].push_back(get_arb());
  int q;
  cin >> q;
  while (q--) {
    get_query();
    cout << calc_query(0, 0) << '\n';
  }
}
int calc_query(int nod, int act) {
  int ans(0);
  if (act == query.size()) ans++, act--;
  for (auto i : adia[nod]) {
    if (name[i] == query[act])
      ans += calc_query(i, act + 1);
    else
      ans += calc_query(i, act);
  }
  return ans;
}
void get_query() {
  string s;
  while (s == "") getline(cin, s);
  query.clear();
  s += " ";
  for (int i(0); i < s.size(); i++) {
    string name;
    while (s[i] != ' ') name.push_back(s[i]), i++;
    query.push_back(name);
  }
}
int get_arb() {
  int poz(++cnt);
  name[poz] = trans[act].second;
  if (trans[act].first == 2) {
    act++;
    return poz;
  }
  act++;
  while (trans[act].first != 1) adia[poz].push_back(get_arb());
  act++;
  return poz;
}
void get_trans() {
  string s;
  cin >> s;
  for (int i(0); i < s.size(); i++) {
    i++;
    int type(0);
    if (s[i] == '/') type = 1, i++;
    string name;
    while (s[i] != '>' && s[i] != '/') name.push_back(s[i]), i++;
    if (s[i] == '/') type = 2, i++;
    trans.push_back({type, name});
  }
}
