#include <bits/stdc++.h>
using namespace std;
const int MAXVERTICES = 250100;
int curTag;
vector<string> allTags, query;
vector<int> children[MAXVERTICES];
string names[MAXVERTICES];
int sz;
int build_tree() {
  int res = sz++;
  names[res] = allTags[curTag++];
  if (names[res][names[res].size() - 1] != '/') {
    while (1) {
      string tag = allTags[curTag++];
      if (tag[0] == '/') break;
      --curTag;
      children[res].push_back(build_tree());
    }
  } else {
    names[res] = names[res].substr(0, names[res].size() - 1);
  }
  return res;
}
int dfs(int node, int pos) {
  int res = (pos == (int)query.size() - 1 && names[node] == query[pos] ? 1 : 0);
  int nextPos =
      (pos < (int)query.size() - 1 && names[node] == query[pos] ? pos + 1
                                                                : pos);
  for (int i = 0; i < children[node].size(); ++i)
    res += dfs(children[node][i], nextPos);
  return res;
}
void print_tree(int root, int level) {
  for (int i = 0; i < level; ++i) printf(" ");
  printf("%s\n", names[root].c_str());
  for (int i = 0; i < children[root].size(); ++i)
    print_tree(children[root][i], level + 1);
}
vector<int> solve(const vector<string>& tags,
                  const vector<vector<string>>& queries) {
  curTag = 0;
  allTags = tags;
  sz = 0;
  int root = build_tree();
  vector<int> res;
  for (int i = 0; i < queries.size(); ++i) {
    query = queries[i];
    res.push_back(dfs(root, 0));
  }
  return res;
}
char s[1000005];
void real_solve() {
  vector<string> tags;
  tags.push_back("ROOT");
  gets(s);
  string cur = "";
  int len = strlen(s);
  for (int i = 0; i < len; ++i)
    if (s[i] == '<' || s[i] == '>') {
      if (cur != "") {
        tags.push_back(cur);
        cur = "";
      }
    } else
      cur += s[i];
  tags.push_back("/ROOT");
  int m;
  scanf("%d\n", &m);
  vector<vector<string>> queries;
  for (int i = 0; i < m; ++i) {
    vector<string> q;
    gets(s);
    string cur = "";
    int len = strlen(s);
    for (int j = 0; j <= len; ++j)
      if (s[j] == ' ' || j == len) {
        if (cur != "") {
          q.push_back(cur);
          cur = "";
        }
      } else
        cur += s[j];
    queries.push_back(q);
  }
  vector<int> res = solve(tags, queries);
  for (int i = 0; i < res.size(); ++i) printf("%d\n", res[i]);
}
int main() {
  real_solve();
  return 0;
}
