#include <bits/stdc++.h>
using namespace std;
long long labs(long long a) { return a < 0 ? (-a) : a; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long min(long long a, long long b) { return a < b ? a : b; }
const int MAXN = 205;
struct Node {
  int p;
  int n;
  vector<int> to;
  Node(int nn, int pp) : n(nn), p(pp) {}
};
int ncnt = 1;
map<string, int> names;
vector<Node> g;
void Parse(int parent, const string& s, int& i) {
  string name;
  i++;
  while (s[i] != '>' && s[i] != '/') {
    name += s[i];
    i++;
  }
  if (names.find(name) == names.end()) {
    names.insert(make_pair(name, ncnt++));
  }
  int index = g.size();
  g.push_back(Node(names[name], parent));
  if (s[i] == '/') {
    i += 2;
    return;
  }
  i++;
  while (s[i + 1] != '/') {
    Parse(index, s, i);
  }
  while (s[i] != '>') i++;
  i++;
}
vector<int> v;
int DFS(int i, int cur) {
  int res = 0;
  if (cur == v.size() - 1 && v[cur] == g[i].n) {
    res++;
  } else if (v[cur] == g[i].n) {
    cur++;
  }
  for (int(j) = 0; (j) < (g[i].to.size()); (j)++) {
    int to = g[i].to[j];
    res += DFS(to, cur);
  }
  return res;
}
int main() {
  string s;
  cin >> s;
  g.reserve(200000);
  g.push_back(Node(0, -1));
  for (int i = 0; i < s.size();) {
    Parse(0, s, i);
  }
  for (int i = 1; i < g.size(); i++) {
    g[g[i].p].to.push_back(i);
  }
  int n;
  cin >> n;
  v.reserve(MAXN);
  getline(cin, s);
  for (int(i) = 0; (i) < (n); (i)++) {
    getline(cin, s);
    s += " ";
    string t = "";
    v.clear();
    bool bolt = false;
    for (int(j) = 0; (j) < (s.size()); (j)++) {
      if (s[j] == ' ') {
        if (names.find(t) == names.end()) {
          bolt = true;
          break;
        }
        v.push_back(names[t]);
        t = "";
      } else {
        t += s[j];
      }
    }
    if (bolt) {
      cout << 0 << endl;
      continue;
    }
    cout << DFS(0, 0) << endl;
  }
  return 0;
}
