#include <bits/stdc++.h>
using namespace std;
char a[1000005];
string val[1000005], b[1000005], qr[1000005];
vector<int> d[1000005];
int fa[1000005], len, cnt, sk[1000005], nx[1000005], ans = 0, tot, us[1000005];
int cl(int st, int en) {
  int p = ++cnt;
  val[p] = b[st];
  for (int i = st + 1; i <= en; ++i) {
    if (us[i]) {
      fa[cl(i, nx[i] - 1)] = p;
      i = nx[i] - 1;
    }
  }
  return p;
}
void dfs(int x, int w) {
  if (w == tot - 1) ans += val[x] == qr[tot];
  for (int i = 0; i < (int)d[x].size(); ++i) {
    int v = d[x][i];
    dfs(v, min(tot - 1, w + (val[x] == qr[w + 1])));
  }
}
int main() {
  scanf("%s", a + 1);
  len = strlen(a + 1);
  for (int i = 1; i <= len; ++i)
    if (a[i] == '<') {
      for (int j = i; j <= len; ++j)
        if (a[j] == '>') {
          if (a[i + 1] == '/') {
            int v = sk[tot--];
            nx[v] = j + 1;
            for (int k = i + 2; k < j; ++k) b[v] += a[k];
          } else if (a[j - 1] == '/') {
            us[i] = 1;
            nx[i] = j + 1;
            for (int k = i + 1; k < j - 1; ++k) b[i] += a[k];
          } else
            sk[++tot] = i, us[i] = 1;
          i = j;
          break;
        }
    }
  for (int i = 1; i <= len; ++i)
    if (us[i]) cl(i, nx[i] - 1), i = nx[i] - 1;
  for (int i = 1; i <= cnt; ++i) d[fa[i]].push_back(i);
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    while (1) {
      cin >> qr[++tot];
      if (getchar() == '\n') break;
    }
    ans = 0;
    dfs(0, 0);
    tot = 0;
    printf("%d\n", ans);
  }
}
