#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int H = (int)1e9 + 7;
char a[N];
char s[20000];
int n[200], u[200], when[200][200], ans[200];
int h[200][200];
int st[N];
int main() {
  srand(time(NULL));
  gets(a);
  int alen = strlen(a), m;
  scanf("%d ", &m);
  for (int i = 0; i < ((int)(m)); ++i) {
    gets(s);
    int w = strlen(s);
    for (int j = 0; j < w; j++) {
      while (j < w && s[j] != ' ') {
        h[i][n[i]] *= H;
        h[i][n[i]] += s[j++];
      }
      n[i]++;
    }
  }
  int e = 0;
  for (int i = 0; i < alen; i++) {
    int t = 1;
    assert(a[i++] == '<');
    if (a[i] == '/') {
      t = 2;
      i++;
    }
    int nowh = 0;
    while (a[i] != '/' && a[i] != '>') {
      nowh *= H;
      nowh += a[i++];
    }
    if (a[i] == '/') {
      t = 3;
      i++;
    }
    for (int T = 1; T <= 2; T++) {
      if (t & T) {
        if (T == 1) {
          st[e++] = nowh;
          for (int j = 0; j < ((int)(m)); ++j) {
            if (u[j] == n[j] - 1) {
              if (nowh == h[j][u[j]]) ans[j]++;
            } else {
              if (nowh == h[j][u[j]]) {
                u[j]++;
                when[j][u[j]] = e - 1;
              }
            }
          }
        } else {
          assert(st[--e] == nowh);
          for (int j = 0; j < ((int)(m)); ++j) {
            if (u[j] != 0 && when[j][u[j]] == e) {
              u[j]--;
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < ((int)(m)); ++i) printf("%d\n", ans[i]);
  return 0;
}
