#include <bits/stdc++.h>
template <typename T>
T inline SQR(const T& a) {
  return a * a;
}
template <typename T>
T inline ABS(const T& a) {
  return a < 0 ? -a : a;
}
const double EPS = 1e-9;
inline int SGN(double a) {
  return ((a > EPS) ? (1) : ((a < -EPS) ? (-1) : (0)));
}
inline int CMP(double a, double b) { return SGN(a - b); }
using namespace std;
struct node_t {
  map<string, node_t*> child;
  int reached;
};
string s;
int spos;
void build(node_t* node) {
  while (spos < int((s).size())) {
    spos++;
    string cur = "";
    while (s[spos] != '>') cur += s[spos++];
    if (cur[0] == '/') {
      spos++;
      return;
    }
    bool flag = false;
    if (cur[int((cur).size()) - 1] == '/') {
      flag = true;
      cur.erase(int((cur).size()) - 1);
    }
    if (!(node->child.count(cur))) node->child[cur] = new node_t();
    node->child[cur]->reached++;
    spos++;
    if (flag)
      continue;
    else
      build(node->child[cur]);
  }
}
int N;
string qry[222];
int ans = 0;
void solve(node_t* node, int pos) {
  for (__typeof((node->child).begin()) x = node->child.begin();
       x != node->child.end(); x++) {
    if (x->first == qry[pos]) {
      if (pos == N - 1) {
        ans += x->second->reached;
        solve(x->second, pos);
      } else
        solve(x->second, pos + 1);
    } else
      solve(x->second, pos);
  }
}
int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false);
  node_t* pref_trie = new node_t();
  cin >> s;
  spos = 0;
  build(pref_trie);
  int q;
  cin >> q;
  cin.ignore();
  while (q--) {
    getline(cin, s);
    istringstream iss(s);
    N = 0;
    while (iss >> qry[N]) N++;
    ans = 0;
    solve(pref_trie, 0);
    cout << ans << "\n";
  }
  return 0;
}
