#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 220;
int n, cnt;
vector<string> v[N_MAX];
vector<pair<string, int> > st;
int ans[N_MAX];
size_t ptr[N_MAX];
vector<int> id[N_MAX];
void open_tag(const string& s) {
  ++cnt;
  st.push_back(make_pair(s, cnt));
  for (int i = 0; i < n; ++i)
    if (ptr[i] != v[i].size()) {
      if (v[i][ptr[i]] == s) {
        id[i][ptr[i]] = cnt;
        ptr[i]++;
        if (ptr[i] == v[i].size()) ans[i]++;
      }
    } else if (v[i].back() == s)
      ans[i]++;
}
void close_tag(const string& s) {
  assert(!st.empty() && st.back().first == s);
  int cid = st.back().second;
  for (int i = 0; i < n; ++i)
    if (ptr[i] != 0) {
      if (id[i][ptr[i] - 1] == cid) ptr[i]--;
    }
  st.pop_back();
}
int main() {
  ios::sync_with_stdio(false);
  string doc;
  getline(cin, doc);
  cin >> n >> ws;
  for (int i = 0; i < n; ++i) {
    string line;
    getline(cin, line);
    istringstream iss(line);
    string word;
    while (iss >> word) v[i].push_back(word);
    id[i].resize(v[i].size());
  }
  size_t i = 0;
  while (i < doc.size()) {
    assert(doc[i] == '<');
    ++i;
    bool closing = doc[i] == '/';
    if (closing) ++i;
    string name;
    name.reserve(10);
    while (islower(doc[i])) name += doc[i++];
    bool selfclosing = doc[i] == '/';
    if (selfclosing) ++i;
    assert(doc[i] == '>');
    ++i;
    if (!closing || selfclosing) open_tag(name);
    if (closing || selfclosing) close_tag(name);
  }
  assert(st.empty());
  for (int i = 0; i < n; ++i) cout << ans[i] << '\n';
  return 0;
}
