#include <bits/stdc++.h>
const double pi = acos(-1.0), eps = 1e-9, INF = 1e90;
const int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const int MO = (int)(1e9 + 7);
struct Tdata {
  int data, kind;
} data[1000001];
using namespace std;
map<string, int> hasher;
vector<int> adj[1000001];
string now;
char c, s[2000001];
int root = 1, tot, hashtot, treetot, tree[1000001];
int querynum, query[1000001], fa[1000001], f[1000001];
int build(int &l) {
  int now = ++treetot;
  tree[treetot] = data[l].data;
  ++l;
  if (data[l - 1].kind == 2) return now;
  while (data[l].kind != 1) adj[now].push_back(build(l));
  ++l;
  return now;
}
void dfs(int now) {
  for (__typeof(adj[now].begin()) x = adj[now].begin(); x != adj[now].end();
       x++) {
    fa[*x] = now;
    dfs(*x);
  }
}
void DFS(int now) {
  for (__typeof(adj[now].begin()) x = adj[now].begin(); x != adj[now].end();
       x++) {
    f[*x] = f[now];
    if (f[now] < querynum - 1 && tree[*x] == query[f[now] + 1]) ++f[*x];
    DFS(*x);
  }
}
int main() {
  gets(s);
  for (char *S = s; *S; S++) {
    now = "";
    ++tot;
    while (1) {
      S++;
      if (*S == '>') break;
      now += *S;
    }
    if (now[0] == '/')
      data[tot].kind = 1, now = now.substr(1, (int)((now).size()) - 1);
    if (now[(int)((now).size()) - 1] == '/')
      data[tot].kind = 2, now = now.substr(0, (int)((now).size()) - 1);
    if (hasher.count(now))
      data[tot].data = hasher[now];
    else
      data[tot].data = (hasher[now] = ++hashtot);
  }
  while (1) {
    adj[0].push_back(build(root));
    if (root > tot) break;
  }
  dfs(0);
  int Q;
  scanf("%d", &Q);
  getchar();
  while (Q--) {
    querynum = 0;
    now = "";
    while ((c = getchar()) != '\n') {
      if (c == ' ') {
        if (!hasher.count(now))
          query[++querynum] = 0;
        else
          query[++querynum] = hasher[now];
        now = "";
      } else
        now += c;
    }
    if (!hasher.count(now))
      query[++querynum] = 0;
    else
      query[++querynum] = hasher[now];
    DFS(0);
    int ans = 0;
    for (int i = 1; i <= treetot; i++)
      if (tree[i] == query[querynum] && f[fa[i]] == querynum - 1) ++ans;
    printf("%d\n", ans);
  }
}
