#include <bits/stdc++.h>
using namespace std;
char buf[1000008], css[1000008];
int chi[1000008], ends[266];
int fri = 1, OPEN = -1, CLOSE = -2, OPCLOSE = -3, data[1000008], next[1000008];
int ukn;
map<string, int> kn;
vector<int> ps, cc;
int summ;
int dfs3(int e, int f) {
  if (e >= ps.size()) return e;
  if (ps[e] == OPEN) {
    if (ps[e + 1] == cc[f]) {
      if (f + 1 < cc.size())
        f++;
      else
        summ++;
    }
  }
  if (ps[e] == OPCLOSE) {
    if (f + 1 == cc.size() && ps[e + 1] == cc[f]) summ++;
    return e + 2;
  }
  int p = e + 2;
  while (p < ps.size()) {
    if (ps[p] == CLOSE) return p + 2;
    p = dfs3(p, f);
  }
  return p;
}
void parse() {
  int p = 0;
  while (buf[p]) {
    int typ, ts, te;
    if (buf[p] != '<') {
      printf("   expected < got buf[%d]=%c\n", p, buf[p]);
      return;
    }
    p++;
    if (buf[p] == '/') {
      typ = CLOSE;
      ++p;
    } else {
      typ = OPEN;
    }
    ts = p;
    while (buf[p] != '/' && buf[p] != '>') p++;
    te = p;
    if (buf[p] == '/') typ = OPCLOSE;
    while (buf[p++] != '>')
      ;
    string nam(buf + ts, te - ts);
    int idx;
    map<string, int>::iterator it = kn.find(nam);
    if (it == kn.end()) {
      idx = ++ukn;
      kn[nam] = idx;
    } else {
      idx = it->second;
    }
    ps.push_back(typ);
    ps.push_back(idx);
  }
}
int count() {
  cc.clear();
  int p = 0, e;
  while (css[p]) {
    e = p;
    while (css[e] != ' ' && css[e]) e++;
    string nam(css + p, e - p);
    map<string, int>::iterator it = kn.find(nam);
    if (it == kn.end()) return 0;
    cc.push_back(it->second);
    if (!css[e]) break;
    p = e + 1;
  }
  summ = 0;
  dfs3(0, 0);
  return summ;
}
void mygets(char *b) {
  gets(b);
  while (*b) {
    if (*b == 10 || *b == 13) {
      *b = 0;
      break;
    }
    b++;
  }
}
int main() {
  mygets(buf);
  ps.push_back(-1);
  ps.push_back(-99);
  parse();
  ps.push_back(-2);
  ps.push_back(-99);
  int n;
  scanf("%d", &n);
  mygets(css);
  for (int i = 0; i < n; i++) {
    mygets(css);
    printf("%d\n", count());
  }
}
