#include <bits/stdc++.h>
using namespace std;
vector<string> inp;
string RED;
void query() {
  int ans = 0;
  string q;
  getline(cin, q);
  stringstream cnv;
  cnv << q;
  vector<string> vs;
  string x;
  while (cnv >> x) vs.push_back(x);
  vector<string> open;
  vector<int> open_index;
  vector<int> other_index;
  for (int i = 0; i < inp.size(); ++i) {
    if (inp[i][0] == '/') {
      if (open_index.size() == 0 or
          (other_index.size() != 0 and other_index[other_index.size() - 1] >
                                           open_index[open_index.size() - 1]))
        other_index.pop_back();
      else {
        open_index.pop_back();
        open.pop_back();
      }
    } else {
      if (open.size() < vs.size() and vs[open.size()] == inp[i]) {
        open.push_back(inp[i]);
        open_index.push_back(i);
      } else
        other_index.push_back(i);
    }
    if (open.size() == vs.size())
      if (inp[i] == open[open.size() - 1]) {
        ans++;
      }
  }
  cout << ans << endl;
}
void proc() {
  stringstream cin;
  cin << RED;
  char c;
  string name;
  while (cin >> c) {
    if (c == '<')
      name = "";
    else if (c == '>') {
      if (name[name.length() - 1] != '/')
        inp.push_back(name);
      else {
        inp.push_back(name.substr(0, name.length() - 1));
        inp.push_back("/" + name.substr(0, name.length() - 1));
      }
    } else {
      string add = " ";
      add[0] = c;
      name = name + add;
    }
  }
}
int main() {
  getline(cin, RED);
  proc();
  int m;
  string rd;
  stringstream cnv;
  getline(cin, rd);
  cnv << rd;
  cnv >> m;
  while (m--) query();
  return 0;
}
