#include <bits/stdc++.h>
std::string BHTML, BCSS;
int m, hash_iter, n, mn;
std::map<std::string, int> hash;
std::vector<int> text, x(207);
int hashTag(std::string const &tag) {
  if (!hash.count(tag)) {
    hash[tag] = ++hash_iter;
  }
  return hash[tag];
}
void addHash(std::string &tag) {
  bool opening = true, closing = false;
  if (tag[tag.length() - 1] == '/') {
    opening = true;
    closing = true;
    tag = tag.substr(0, tag.length() - 1);
  } else if (tag[0] == '/') {
    opening = false, closing = true;
    tag = tag.substr(1);
  }
  int code = hashTag(tag);
  if (opening) {
    text.push_back(code);
    ++n;
  }
  if (closing) {
    text.push_back(-code);
    ++n;
  }
}
void hashText() {
  int it, old = 0;
  while ((it = BHTML.find(">", old + 1)) != -1) {
    std::string tag = BHTML.substr(old + 1, it - old - 1);
    addHash(tag);
    old = it + 1;
  }
}
void getBCSS() {
  std::getline(std::cin, BCSS);
  std::stringstream ccin(BCSS);
  std::string tag;
  mn = 0;
  while (ccin >> tag) {
    x[mn++] = hashTag(tag);
  }
}
int countAns() {
  int ans = 0, it = 0;
  std::stack<bool> was;
  for (int i = 0; i < n; ++i) {
    if (text[i] > 0) {
      if (it < mn && text[i] == x[it]) {
        ++it;
        was.push(true);
      } else {
        was.push(false);
      }
      if (it == mn && text[i] == x[mn - 1]) {
        ++ans;
      }
    } else {
      if (was.top()) {
        --it;
      }
      was.pop();
    }
  }
  return ans;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  std::cin >> BHTML >> m;
  std::getline(std::cin, BCSS);
  hashText();
  for (int i = 0; i < m; ++i) {
    getBCSS();
    std::cout << countAns() << "\n";
  }
}
