#include <bits/stdc++.h>
#pragma warning(disable : 4244 4267 4018 4996 4800)
#pragma comment(linker, "/stack:20000000")
using namespace std;
istream& in = cin;
ostream& out = cout;
vector<int> cnt;
string s;
int pos, res;
vector<string> v;
void parse(int node) {
  assert(s[pos] == '<');
  while (pos + 1 < s.size() && s[pos] == '<' && s[pos + 1] != '/') {
    static string tag;
    tag.clear();
    ++pos;
    while (pos < s.size() && s[pos] != '/' && s[pos] != '>') {
      tag.push_back(s[pos]);
      ++pos;
    }
    int ts = tag.size();
    if (node >= v.size() - 1 && tag == v.back()) ++res;
    if (s[pos] == '/') {
      pos += 2;
    } else {
      ++pos;
      parse(node < v.size() && v[node] == tag ? node + 1 : node);
      pos += 3 + ts;
    }
  }
}
int main() {
  getline(in, s);
  int n;
  in >> n;
  string line;
  getline(in, line);
  for (int i = 0; i < n; ++i) {
    getline(in, line);
    istringstream iss(line);
    int tok = 1;
    v.clear();
    for (;;) {
      string t;
      iss >> t;
      if (!iss) break;
      v.push_back(t);
    }
    res = 0;
    pos = 0;
    parse(0);
    out << res << '\n';
  }
  return 0;
}
