#include <bits/stdc++.h>
using namespace std;
const int N = 1000010, K = 1000010, M = 210;
int tot, cnt, sig, size;
char s[K], t[N];
int a[K * 2];
int b[M], c[N];
int dp[N];
bool f[K * 2];
int flag;
map<string, int> lab;
void work() {
  int i, j, f;
  int len = strlen(s);
  for (i = 0; i <= len - 1; i++) {
    if (s[i] == '<') {
      if (s[i + 1] == '/') {
        f = -1;
        i++;
        flag = 1;
      } else {
        f = 1;
        size++;
      }
      i++;
      j = 0;
      while (s[i] != '>' && s[i] != '/') {
        t[j] = s[i];
        j++;
        i++;
      }
      t[j] = '\0';
      if (!lab[t]) lab[t] = ++tot;
      a[++cnt] = lab[t] * f;
      if (s[i] == '/') {
        i++;
        a[++cnt] = (-1) * lab[t];
      }
    }
  }
  gets(s);
}
bool read() {
  sig = 0;
  int i, j;
  gets(s);
  int len = strlen(s);
  for (i = 0; i <= len - 1; i++) {
    if (s[i] >= 'a' && s[i] <= 'z') {
      j = 0;
      while (s[i] >= 'a' && s[i] <= 'z') {
        t[j] = s[i];
        j++;
        i++;
      }
      t[j] = '\0';
      if (lab[t]) {
        b[++sig] = lab[t];
      } else
        return false;
    }
  }
  return true;
}
void dp1() {
  int t;
  int i, j, now = 0;
  int tmp;
  scanf("%d", &t);
  gets(s);
  while (t--) {
    if (!read()) {
      printf("0\n");
      continue;
    }
    b[sig + 1] = 0;
    int ans = 0;
    now = 0;
    dp[0] = 0;
    for (i = 1; i <= cnt; i++) {
      if (a[i] < 0) {
        now--;
        continue;
      } else
        now++;
      tmp = dp[now - 1];
      if (a[i] == b[tmp + 1]) {
        dp[now] = dp[now - 1] + 1;
      } else
        dp[now] = dp[now - 1];
      if (a[i] == b[sig]) {
        if (dp[now] == sig) ans++;
      }
    }
    printf("%d\n", ans);
  }
}
int main() {
  scanf("%s", s);
  work();
  dp1();
}
