#include <bits/stdc++.h>
using namespace std;
struct Node {
  int name;
  vector<Node *> dict;
};
int tot;
Node pool[250000], *root;
Node *NewNode() { return &pool[tot++]; }
int hh(char *s) {
  int h = 0;
  for (; *s; s++) h = h * 1000000007 + *s;
  return h;
}
Node *ss[250000];
void parser() {
  char *p;
  char tag[20];
  ss[0] = NewNode();
  root = ss[0];
  int tt = 0;
  for (; scanf("<%[^>]>", tag) == 1;) {
    if (tag[0] == '/') {
      tt--;
    } else {
      int tagl = strlen(tag);
      int close = 0;
      if (tag[tagl - 1] == '/') {
        close = 1;
        tag[tagl - 1] = '\0';
      }
      int ts = hh(tag);
      Node *y = NewNode();
      y->name = ts;
      ss[tt]->dict.push_back(y);
      if (!close) ss[++tt] = y;
    }
  }
}
int query[210];
int n, ans;
void dfs(Node *x, int q) {
  int r = 0;
  if (x->name == query[q]) {
    if (q == n - 1) {
      ans++;
    } else {
      q++;
    }
  }
  for (vector<Node *>::iterator it = x->dict.begin(); it != x->dict.end();
       it++) {
    dfs(*it, q);
  }
}
int main() {
  int m;
  parser();
  scanf("%d", &m);
  while (m--) {
    n = 0;
    char t[20], ch;
    int g;
    do {
      g = scanf("%s%c", t, &ch);
      query[n++] = hh(t);
    } while (g == 2 && ch != '\n');
    ans = 0;
    dfs(root, 0);
    printf("%d\n", ans);
  }
  return 0;
}
