#include <bits/stdc++.h>
const int maxn = 1000010;
char s[maxn] = {};
char t[maxn] = {};
int b[210] = {};
int includ_arr[maxn] = {};
int includ;
int main() {
  gets(s);
  int n = strlen(s);
  int q;
  std::cin >> q;
  gets(t);
  while (q--) {
    int ans = 0;
    gets(t);
    int len = strlen(t);
    t[len++] = ' ';
    int cnt = 0;
    for (int i = 0; i < len; ++i) {
      if (i == 0 || t[i - 1] == ' ') {
        b[cnt++] = i;
      }
    }
    includ = 0;
    includ_arr[includ++] = 0;
    int pos = 0;
    for (; pos < n;) {
      int cc = includ_arr[includ - 1];
      if (s[pos + 1] == '/') {
        includ--;
        while (s[pos] != '>') pos++;
        pos++;
      } else {
        int j = pos + 1;
        bool ok = true;
        while (s[j] >= 'a' && s[j] <= 'z') {
          if (s[j] != t[b[cc] + j - (pos + 1)]) ok = false;
          j++;
        }
        if (t[b[cc] + j - (pos + 1)] != ' ') ok = false;
        if (ok) cc++;
        if (cc == cnt) {
          ans++;
          cc--;
        }
        if (s[j] == '>')
          includ_arr[includ++] = cc, pos = j + 1;
        else
          pos = j + 2;
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
