#include <bits/stdc++.h>
using namespace std;
const int MAX_LEN = 1000099;
const int MAX_QUERY_LEN = 409;
char document[MAX_LEN];
char query[MAX_QUERY_LEN];
int m;
int root;
map<string, int> tag;
vector<vector<int> > G;
vector<int> color;
int tagNum = 0;
int getTagNum(const string& myTag) {
  if (tag.find(myTag) == tag.end()) tag[myTag] = tagNum++;
  return tag[myTag];
}
vector<int> parseQuery(char query[MAX_QUERY_LEN]) {
  int len = strlen(query);
  vector<int> sequence;
  int i = 0;
  while (i < len) {
    string s = "";
    while (i < len && query[i] != ' ') {
      s += query[i];
      i++;
    }
    if (s.length()) sequence.push_back(getTagNum(s));
    i++;
  }
  return sequence;
}
int dfsCount(int v, int seen, const vector<int>& sequence) {
  int ans = 0;
  if (color[v] == sequence[seen]) {
    seen++;
    if (seen == int(sequence.size())) {
      ans++;
      seen--;
    }
  }
  for (int to : G[v]) {
    ans += dfsCount(to, seen, sequence);
  }
  return ans;
}
void processQuery(char query[MAX_QUERY_LEN]) {
  vector<int> sequence = parseQuery(query);
  printf("%d\n", dfsCount(root, 0, sequence));
}
struct Tag {
  int num;
  int type;
  Tag() {}
  Tag(int num, int type) : num(num), type(type) {}
};
const Tag nullTag(-1, -1);
Tag nextTag(int& pos) {
  Tag inputTag(0, 0);
  if (document[pos] != '<') return nullTag;
  pos++;
  string myTag;
  if (document[pos] == '/') {
    inputTag.type = 1;
    pos++;
  }
  while (isalpha(document[pos])) {
    myTag += document[pos];
    pos++;
  }
  inputTag.num = getTagNum(myTag);
  if (document[pos] == '/') {
    inputTag.type = 2;
    pos++;
  }
  if (document[pos] != '>') return nullTag;
  pos++;
  return inputTag;
}
void buildGraph(int root, int l, int r, const vector<int>& match) {
  if (l > r) return;
  if (l == r) {
    G[root].push_back(l);
    return;
  }
  int i = l;
  while (i <= r) {
    G[root].push_back(i);
    int next = match[i];
    buildGraph(i, i + 1, next - 1, match);
    i = next + 1;
  }
}
vector<Tag> getDocumentTags() {
  int pnt = 0;
  int len = strlen(document);
  vector<Tag> documentTags;
  while (pnt < len) {
    Tag t = nextTag(pnt);
    documentTags.push_back(t);
  }
  return documentTags;
}
void parseDocument() {
  vector<Tag> documentTags = getDocumentTags();
  int n = documentTags.size();
  color.resize(n + 1);
  vector<int> match(n);
  stack<int> S;
  for (int i = 0; i < n; i++) {
    color[i] = documentTags[i].num;
    if (documentTags[i].type == 0) {
      S.push(i);
    } else if (documentTags[i].type == 1) {
      int ind = S.top();
      match[i] = ind;
      match[ind] = i;
      S.pop();
    } else {
      match[i] = i;
    }
  }
  G.resize(n + 1);
  root = n;
  color[root] = -1;
  buildGraph(root, 0, n - 1, match);
}
void gen() {
  freopen("text.txt", "w", stdout);
  for (int i = 0; i < 250000; i++) printf("<a>");
  for (int i = 0; i < 250000; i++) printf("</a>");
  printf("\n200\n");
  for (int i = 0; i < 200; i++) {
    for (int j = 0; j < 200; j++) printf("a ");
    printf("\n");
  }
}
int main() {
  gets(document);
  parseDocument();
  scanf("%d\n", &m);
  for (int i = 0; i < m; i++) {
    gets(query);
    processQuery(query);
  }
  return 0;
}
