#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
int n;
map<string, int> num;
vector<int> tree[250005];
char s[1000006];
struct Token {
  string s;
  int ty;
};
Token getToken(int& pos) {
  pos++;
  Token t;
  t.s = "";
  t.ty = 1;
  if (s[pos] == '/') {
    t.ty = -1;
    pos++;
  }
  while (s[pos] != '>') {
    if (s[pos] == '/') {
      t.ty = 0;
      pos++;
    } else {
      t.s += s[pos];
      pos++;
    }
  }
  pos++;
  return t;
}
stack<int> st;
int ind[250005];
int cnt = 0;
vector<int> roots;
void parse() {
  int cur = 0;
  int n = (int)strlen(s);
  while (cur < n) {
    Token t = getToken(cur);
    if (num.count(t.s) == 0) {
      num.insert(make_pair(t.s, (int)num.size()));
    }
    int id = num[t.s];
    if (t.ty == 1 || t.ty == 0) {
      ind[cnt] = id;
      if (!st.empty()) {
        tree[st.top()].push_back(cnt);
      } else {
        roots.push_back(cnt);
      }
      if (t.ty == 1) {
        st.push(cnt);
      }
      cnt++;
    } else {
      st.pop();
    }
  }
}
int go(int v, vector<int>& q, int len) {
  int ret = 0;
  if (ind[v] == q[len]) {
    if (len == (int)q.size() - 1) {
      ret++;
    } else {
      len++;
    }
  }
  for (int i = 0; i < int(tree[v].size()); i++) {
    int u = tree[v][i];
    ret += go(u, q, len);
  }
  return ret;
}
void query() {
  gets(s);
  istringstream sin(s);
  string t;
  vector<int> q;
  while (sin >> t) {
    if (num.count(t) == 0) {
      cout << 0 << endl;
      return;
    }
    q.push_back(num[t]);
  }
  int ans = 0;
  for (int i = 0; i < int(roots.size()); i++) {
    ans += go(roots[i], q, 0);
  }
  cout << ans << endl;
}
int main() {
  gets(s);
  parse();
  int q;
  scanf("%d\n", &q);
  for (int it = 0; it < int(q); it++) {
    query();
  }
  return 0;
}
