#include <bits/stdc++.h>
using namespace std;
const int MAXL = 1100000, MAXM = 200;
const int MAXN = MAXL / 4;
map<string, int> q;
int docl, answer[MAXM], m;
int parent[MAXN + 1], query[MAXM][201], tab[MAXM][201];
char doc[MAXL + 1];
int main() {
  scanf("%s", &doc[0]);
  docl = strlen(&doc[0]);
  scanf("%d", &m);
  char temp[10000];
  gets(&temp[0]);
  char word[11];
  for (int i = 0; i < m; ++i) {
    gets(&temp[0]);
    int pos = 0;
    int qlen = strlen(&temp[0]);
    while (pos < qlen) {
      int next = pos, len = 0;
      while (isalpha(temp[next])) word[len++] = temp[next++];
      word[len] = 0;
      int tag;
      if (q.find(word) == q.end()) {
        tag = q.size();
        q[word] = tag;
      } else
        tag = q[word];
      query[i][++query[i][0]] = tag;
      pos = next + 1;
    }
  }
  int pos = 0, curv = 0, curt = 1;
  while (pos < docl) {
    int next = pos + 1, len = 0;
    bool close = false;
    if (doc[next] == '/') {
      ++next;
      close = true;
    }
    while (isalpha(doc[next])) word[len++] = doc[next++];
    word[len] = 0;
    int tag;
    if (q.find(word) == q.end()) {
      tag = q.size();
      q[word] = tag;
    } else
      tag = q[word];
    if (doc[next] == '/') {
      for (int i = 0; i < m; ++i)
        if (tab[i][0] + 1 == query[i][0] && tag == query[i][query[i][0]])
          ++answer[i];
      parent[curt] = curv;
      ++curt;
      pos = next + 2;
      continue;
    }
    if (!close) {
      for (int i = 0; i < m; ++i)
        if (tab[i][0] + 1 < query[i][0]) {
          if (tag == query[i][tab[i][0] + 1]) {
            ++tab[i][0];
            tab[i][tab[i][0]] = curt;
          }
        } else {
          if (tag == query[i][query[i][0]]) ++answer[i];
        }
      parent[curt] = curv;
      curv = curt++;
    } else {
      for (int i = 0; i < m; ++i)
        if (tab[i][0] > 0 && tab[i][tab[i][0]] == curv) --tab[i][0];
      curv = parent[curv];
    }
    pos = next + 1;
  }
  for (int i = 0; i < m; ++i) printf("%d\n", answer[i]);
  return 0;
}
