#include <bits/stdc++.h>
using namespace std;
char s[1 << 20];
int m, n, sz, st[1 << 20], ss, en[1 << 20], pi, ps;
char p[3000], t[1 << 20];
long long h[1 << 20], ph[222];
int main() {
  gets(s);
  long long ch, ct;
  for (int i = 0; s[i]; i++) {
    ch = 0;
    ct = -1;
    i++;
    if (s[i] == '/') ct = 2, i++;
    while (s[i] != '/' && s[i] != '>') {
      ch = ch * 27 + s[i] - 'a' + 1;
      i++;
    }
    if (s[i] == '/')
      ct = 1, i++;
    else if (ct != 2)
      ct = 0;
    t[sz] = ct;
    h[sz] = ch;
    if (ct < 2) {
      if (ct)
        en[sz] = sz;
      else
        st[ss++] = sz;
      sz++;
    } else {
      ss--;
      en[st[ss]] = sz++;
    }
  }
  scanf("%d\n", &m);
  for (int k = 0; k < m; k++) {
    int r = 0;
    gets(p);
    pi = ps = 0;
    stringstream ss(p);
    string mv;
    while (ss >> mv) {
      ch = 0;
      for (int i = 0; i < mv.size(); i++) ch = ch * 27 + mv[i] - 'a' + 1;
      ph[ps++] = ch;
    }
    for (int i = 0; i < sz; i++) {
      if (t[i] < 2) {
        if (h[i] == ph[pi]) {
          if (pi == ps - 1)
            r++;
          else if (t[i] == 0) {
            pi++;
            t[en[i]] = 3;
          }
        }
      }
      if (t[i] == 3) {
        t[i] = 2;
        pi--;
      }
    }
    printf("%d\n", r);
  }
  return 0;
}
