#include <bits/stdc++.h>
using namespace std;
template <class _T>
inline _T sqr(const _T& first) {
  return first * first;
}
template <class _T>
inline string tostr(const _T& a) {
  ostringstream os("");
  os << a;
  return os.str();
}
template <class _T>
inline istream& operator<<(istream& is, const _T& a) {
  is.putback(a);
  return is;
}
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-11;
int n, k;
char st[1000100];
int stck[300100];
vector<int> v[300100];
string names[300100];
bool u[300100];
map<string, int> nname;
vector<string> w;
void make_tree() {
  int l = strlen(st);
  int i = 0;
  int top = 0;
  stck[top++] = 0;
  names[0] = "_";
  k = 1;
  while (i < l) {
    if (st[i] == '<') {
      i++;
      string name = "";
      while (st[i] != '>') {
        name += st[i++];
      }
      if (name[0] == '/') {
        top--;
        i++;
        continue;
      }
      if (name[name.size() - 1] == '/') {
        name.erase(name.size() - 1, 1);
        names[k] = name;
        v[stck[top - 1]].push_back(k);
        k++;
      } else {
        names[k] = name;
        v[stck[top - 1]].push_back(k);
        stck[top++] = k;
        k++;
      }
      i++;
    }
  }
}
void solve(int pos, int first) {
  if (names[pos] == w[first]) {
    if (first == (int)w.size() - 1) {
      u[pos] = 1;
      for (int i = 0; i < (int)v[pos].size(); i++) {
        solve(v[pos][i], first);
      }
      return;
    } else {
      for (int i = 0; i < (int)v[pos].size(); i++) {
        solve(v[pos][i], first + 1);
      }
    }
  } else {
    for (int i = 0; i < (int)v[pos].size(); i++) {
      solve(v[pos][i], first);
    }
  }
}
int main() {
  cout << setiosflags(ios::fixed) << setprecision(10);
  gets(st);
  make_tree();
  int tmp = 1;
  for (int i = 0; i < k; i++) {
    if (nname[names[i]] == 0) nname[names[i]] = tmp++;
  }
  cin >> n;
  gets(st);
  for (int i = 0; i < n; i++) {
    gets(st);
    int m = strlen(st);
    st[m++] = ' ';
    w.clear();
    string name = "";
    for (int j = 0; j < m; j++) {
      if (st[j] == ' ') {
        w.push_back(name);
        name = "";
        continue;
      }
      name += st[j];
    }
    memset((u), 0, sizeof(u));
    solve(0, 0);
    int ans = 0;
    for (int t = 0; t < k; t++) {
      if (u[t]) ans++;
    }
    printf("%d\n", ans);
  }
  return 0;
}
