#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
int dblcmp(double d) {
  if (fabs(d) < eps) return 0;
  return d > eps ? 1 : -1;
}
vector<string> split(char *sr) {
  stringstream ss(sr);
  vector<string> g;
  string y;
  while (ss >> y) g.push_back(y);
  return g;
}
string s[1111111];
char str[1111111], _str[1111111];
stack<int> st;
vector<int> g[1111111];
set<int> ts;
int ct(int u, string t) {
  int res = 0;
  if (s[u] == t) res++;
  for (__typeof(g[u].begin()) e = g[u].begin(); e != g[u].end(); ++e) {
    res += ct(*e, t);
  }
  return res;
}
int find(int u, int x, vector<string> &t) {
  if (s[u] == t[x]) x++;
  if (x == (int)((t).size()) - 1) {
    int res = 0;
    for (__typeof(g[u].begin()) e = g[u].begin(); e != g[u].end(); ++e) {
      res += ct(*e, t.back());
    }
    return res;
  }
  int res = 0;
  for (__typeof(g[u].begin()) e = g[u].begin(); e != g[u].end(); ++e) {
    res += find(*e, x, t);
  }
  return res;
}
int main() {
  int i, j, k;
  scanf("%s", _str);
  strcpy(str, "<aaaaaaaaaaa>");
  strcat(str, _str);
  strcat(str, "</aaaaaaaaaaa>");
  int l = strlen(str), id = 0;
  for (i = 0; i < l; i++) {
    if (str[i] == '<') i++;
    string t = "";
    while (i < l && str[i] != '>') t += str[i++];
    if (isalpha(t[0]) && isalpha(t[t.size() - 1])) {
      s[id] = t;
      if (!st.empty()) {
        int x = st.top();
        g[x].push_back(id);
      }
      st.push(id++);
      continue;
    } else if (isalpha(t[0]) && t[t.size() - 1] == '/') {
      s[id] = t.substr(0, t.size() - 1);
      int x = st.top();
      g[x].push_back(id++);
      continue;
    } else if (t[0] == '/') {
      s[id] = t.substr(1);
      st.pop();
      continue;
    }
  }
  int cas;
  scanf("%d", &cas);
  getchar();
  while (cas--) {
    gets(_str);
    strcpy(str, "aaaaaaaaaaa ");
    strcat(str, _str);
    vector<string> b = split(str);
    printf("%d\n", find(0, 0, b));
  }
  return 0;
}
