#include <bits/stdc++.h>
using namespace std;
int m;
const int maxlen = 1000050;
string html;
vector<pair<string, int> > tag;
vector<int> son[maxlen];
vector<string> path;
int sta[maxlen], top = 0;
string v[maxlen];
string query;
struct Edge {
  int from, to;
  int next;
} eage[maxlen];
int head[maxlen];
int tot = 0;
int cnt = 0;
void add(int x, int y) {
  eage[++tot].from = x;
  eage[tot].to = y;
  eage[tot].next = head[x];
  head[x] = tot;
}
int ans = 0;
bool used[maxlen];
int dfs(int u, int pos) {
  int res = 0;
  if (pos == path.size() - 1 && path[pos] == v[u]) res = 1;
  if (pos < (int)path.size() - 1 && path[pos] == v[u]) pos++;
  for (int i = head[u]; i; i = eage[i].next) res += dfs(eage[i].to, pos);
  return res;
}
int main() {
  getline(cin, html);
  int pos = 1;
  for (; pos < html.size(); pos++) {
    string s;
    int sign = 1;
    if (html[pos] == '/') pos++, sign = 0;
    while (pos < html.size() && html[pos] != '/' && html[pos] != '>')
      s += html[pos++];
    tag.push_back(make_pair(s, sign));
    if (html[pos] == '/') pos++, tag.push_back(make_pair(s, 0));
    pos++;
  }
  int top = 0;
  for (int i = 0; i < tag.size(); i++)
    if (tag[i].second) {
      sta[++top] = ++cnt;
      v[cnt] = tag[i].first;
      add(sta[top - 1], cnt);
    } else
      top--;
  scanf("%d", &m);
  getline(cin, query);
  for (int k = 1; k <= m; k++) {
    memset(used, 0, sizeof used);
    ans = 0;
    getline(cin, query);
    path.clear();
    int pos = 0;
    for (; pos < query.size(); pos++) {
      string s;
      while (pos < query.size() && query[pos] != ' ') s += query[pos++];
      path.push_back(s);
    }
    printf("%d\n", dfs(0, 0));
  }
  return 0;
}
