#include <bits/stdc++.h>
using namespace std;
char doc[1000050], in[1000050];
int vn;
int sym[(1000050 / 7)];
vector<pair<int, int> > adj[(1000050 / 7)];
vector<int> stk;
map<string, int> tmap;
int n;
int seq[205];
int dp[(1000050 / 7)];
inline int getid(string s) {
  if (tmap.find(s) == tmap.end()) tmap.insert(make_pair(s, tmap.size()));
  return tmap[s];
}
inline void parse() {
  vn = 1;
  sym[0] = -1;
  stk.push_back(0);
  char *pt = strtok(doc, ">");
  while (pt) {
    int v = stk.back();
    int l = strlen(++pt);
    if (pt[l - 1] == '/') {
      pt[l - 1] = '\0';
      int t = getid(pt);
      adj[v].push_back(make_pair(vn++, t));
      sym[vn - 1] = t;
    } else if (pt[0] == '/') {
      stk.pop_back();
    } else {
      int t = getid(pt);
      adj[v].push_back(make_pair(vn++, t));
      sym[vn - 1] = t;
      stk.push_back(vn - 1);
    }
    pt = strtok(NULL, ">");
  }
}
inline void input() {
  n = 0;
  gets(in);
  char *pt = strtok(in, " ");
  while (pt) {
    seq[n++] = getid(pt);
    pt = strtok(NULL, " ");
  }
}
inline int solve() {
  int sol = 0;
  for (int v = 0; v < vn; v++) dp[v] = 0;
  for (int v = 0; v < vn; v++) {
    if (dp[v] == n && sym[v] == seq[n - 1]) sol++;
    for (int i = 0; i < adj[v].size(); i++) {
      int u = adj[v][i].first;
      int t = adj[v][i].second;
      dp[u] = max(dp[u], dp[v]);
      if (dp[v] == n) continue;
      if (seq[dp[v]] == t) {
        dp[u] = max(dp[u], dp[v] + 1);
      }
    }
  }
  return sol;
}
int main(void) {
  int t;
  scanf("%s", doc);
  parse();
  scanf("%d", &t);
  gets(in);
  while (t--) {
    input();
    printf("%d\n", solve());
  }
  return 0;
}
