#include <bits/stdc++.h>
using namespace std;
const int MN = 1000 * 1000 + 100;
int q, ans[MN];
vector<string> s2;
int mmm[MN];
void f(vector<string> rule, int III) {
  vector<string> stck;
  vector<int> p;
  for (int i = 0; i < s2.size(); i++) {
    if (s2[i] == rule[stck.size()]) {
      stck.push_back(s2[i]);
      p.push_back(i);
    }
    if (p.size() > 0 && mmm[i] == p[p.size() - 1]) {
      stck.resize(stck.size() - 1);
      p.resize(p.size() - 1);
    }
    if (stck.size() == rule.size()) {
      ans[III]++;
      stck.resize(stck.size() - 1);
      p.resize(p.size() - 1);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  vector<int> p;
  string s;
  cin >> s >> q;
  memset(mmm, -1, sizeof(mmm));
  for (int i = 0; i < s.size(); i++) {
    string tmp;
    i++;
    for (; s[i] != '>'; i++) tmp += s[i];
    if (s[i - 1] == '/') {
      tmp.resize(tmp.size() - 1);
      s2.push_back(tmp);
      string tmp2;
      tmp2 += '/';
      tmp2 += tmp;
      s2.push_back(tmp2);
      mmm[s2.size() - 1] = s2.size() - 2;
    } else {
      s2.push_back(tmp);
      if (tmp[0] == '/') {
        mmm[s2.size() - 1] = p[p.size() - 1];
        p.resize(p.size() - 1);
      } else
        p.push_back(s2.size() - 1);
    }
  }
  string golabi;
  getline(cin, golabi);
  for (int i = 0; i < q; i++) {
    string rule;
    vector<string> rul;
    getline(cin, rule);
    for (int j = 0; j < rule.size(); j++) {
      string tmp;
      for (; rule[j] != ' ' && j < rule.size(); j++) tmp += rule[j];
      rul.push_back(tmp);
    }
    f(rul, i);
  }
  for (int i = 0; i < q; i++) cout << ans[i] << endl;
  return 0;
}
