#include <bits/stdc++.h>
using namespace std;
vector<int> to[333333];
int parent[333333], tag[333333], ptr, cnt;
int b[333333][28], bn, a[222];
char s[1111111], line[22222], word[22222];
int MakeId(char* s) {
  int cur = 0;
  while (*s >= 'a' && *s <= 'z') {
    if (!b[cur][*s - 'a']) b[cur][*s - 'a'] = ++bn;
    cur = b[cur][*s - 'a'];
    s++;
  }
  return cur;
}
int GetId(char* s) {
  int cur = 0;
  while (*s >= 'a' && *s <= 'z') {
    if (!b[cur][*s - 'a']) return -1;
    cur = b[cur][*s - 'a'];
    s++;
  }
  return cur;
}
void Dfs(int x, int ptr) {
  for (size_t i = 0; i < to[x].size(); ++i) {
    int y = to[x][i];
    if (tag[y] == a[ptr]) {
      if (ptr == 0) {
        Dfs(y, 0);
        cnt++;
      } else
        Dfs(y, ptr - 1);
    } else
      Dfs(y, ptr);
  }
}
int main() {
  int l, xcounter, curx;
  gets(s);
  l = strlen(s);
  xcounter = 0;
  curx = 0;
  parent[0] = -1;
  for (int i = 0, j, id; i < l; i++) {
    j = i;
    while (s[j] != '>') j++;
    id = MakeId(s + i + 1 + (s[i + 1] == '/'));
    if (s[i + 1] != '/') {
      to[curx].push_back(++xcounter);
      parent[xcounter] = curx;
      tag[xcounter] = id;
      curx = xcounter;
    }
    if (s[j - 1] == '/' || s[i + 1] == '/') {
      curx = parent[curx];
    }
    i = j;
  }
  int m;
  scanf("%d\n", &m);
  for (int i = 0; i < m; i++) {
    int n = 0;
    bool bad = false;
    gets(line);
    l = strlen(line);
    for (int j = 0; j < l; j++) {
      a[n++] = GetId(line + j);
      if (a[n - 1] == -1) bad = true;
      while (j < l && line[j] != ' ') j++;
    }
    reverse(a, a + n);
    if (bad) {
      puts("0");
      continue;
    }
    cnt = 0;
    Dfs(0, n - 1);
    printf("%d\n", cnt);
  }
  return 0;
}
