#include <bits/stdc++.h>
using namespace std;
inline void pisz(int n) { printf("%d\n", n); }
template <typename T, typename TT>
ostream &operator<<(ostream &s, pair<T, TT> t) {
  return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream &operator<<(ostream &s, vector<T> t) {
  for (int(i) = 0; (i) < (((int)(t.size()))); ++(i)) s << t[i] << " ";
  return s;
}
char s[1000007], buf[4007];
int res[205], matched[205], patternLength[205], patternCodes[205][205], n, ind;
vector<pair<int, int> > html;
void go() {
  int code = html[ind].second;
  vector<int> risen;
  for (int(i) = 0; (i) < (n); ++(i)) {
    if (code == patternCodes[i][matched[i]]) {
      if (matched[i] == patternLength[i] - 1) {
        ++res[i];
      } else {
        matched[i]++;
        risen.push_back(i);
      }
    }
  }
  ind++;
  if (html[ind].first == 1) {
    go();
  }
  for (typeof(risen.begin()) x = risen.begin(); x != risen.end(); x++)
    --matched[*x];
  ind++;
  if (ind < ((int)(html.size())) && html[ind].first == 1) go();
}
map<string, int> kod;
int nextCode = 1;
void init(const string &str) {
  if (!((kod).find((str)) != (kod).end())) kod[str] = nextCode++;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.getline(s, 1000004);
  cin >> n;
  cin.getline(buf, 20);
  for (int(i) = 0; (i) < (n); ++(i)) {
    cin.getline(buf, 4003);
    stringstream ss(buf);
    string x;
    patternLength[i] = 0;
    matched[i] = 0;
    res[i] = 0;
    while (ss >> x) {
      init(x);
      patternCodes[i][patternLength[i]++] = kod[x];
    }
  }
  int pos = 0, len = strlen(s);
  while (pos < len) {
    pos++;
    string name;
    while (s[pos] != '>') {
      name += s[pos];
      pos++;
    }
    pos++;
    if (name[0] == '/') {
      string nowy = name.substr(1);
      init(nowy);
      html.push_back(make_pair(-1, kod[nowy]));
    } else if (name[((int)(name.size())) - 1] == '/') {
      string nowy = name.substr(0, ((int)(name.size())) - 1);
      init(nowy);
      html.push_back(make_pair(1, kod[nowy]));
      html.push_back(make_pair(-1, kod[nowy]));
    } else {
      init(name);
      html.push_back(make_pair(1, kod[name]));
    }
  }
  ind = 0;
  go();
  for (int(i) = 0; (i) < (n); ++(i)) pisz(res[i]);
}
