#include <bits/stdc++.h>
using namespace std;
int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}
long long nextLong() {
  long long x;
  scanf("%I64d", &x);
  return x;
}
double nextDouble() {
  double x;
  scanf("%lf", &x);
  return x;
}
const int BUFSIZE = 1111111;
char buf[BUFSIZE + 1];
string nextString() {
  scanf("%s", buf);
  return buf;
}
string nextLine() {
  gets(buf);
  return buf;
}
struct Point {
  double x, y;
  Point() : x(0), y(0) {}
  Point(double x, double y) : x(x), y(y) {}
  Point operator-(Point op) const { return Point(x - op.x, y - op.y); }
  Point operator+(Point op) const { return Point(x + op.x, y + op.y); }
  Point operator*(double op) const { return Point(x * op, y * op); }
  double operator*(Point op) const { return x * op.x + y * op.y; }
  double operator%(Point op) const { return x * op.y - y * op.x; }
  double length2() { return x * x + y * y; }
  double lengt() { return sqrt(length2()); }
};
Point nextPoint() {
  double x = nextDouble();
  double y = nextDouble();
  return Point(x, y);
}
int dfs(int v, const vector<vector<pair<int, string> > > &adj, int at,
        const vector<string> &seq) {
  int res = 0;
  for (int i = 0; i < adj[v].size(); ++i) {
    int u = adj[v][i].first;
    bool sameTag = adj[v][i].second == seq[at];
    if (at + 1 == seq.size()) {
      if (sameTag) {
        ++res;
      }
      res += dfs(u, adj, at, seq);
    } else {
      res += dfs(u, adj, at + sameTag, seq);
    }
  }
  return res;
}
void addEdge(vector<vector<pair<int, string> > > &adj, vector<int> &parent,
             int v, string tag) {
  adj.push_back(vector<pair<int, string> >());
  adj[v].push_back(make_pair(adj.size() - 1, tag));
  parent.push_back(v);
}
int stringToInt(string s) {
  stringstream in(s);
  int x;
  in >> x;
  return x;
}
int main() {
  string doc = nextLine();
  string cur;
  int v = 0;
  vector<vector<pair<int, string> > > adj(1);
  vector<int> parent(1, -1);
  for (int i = 0; i < doc.size(); ++i) {
    int ch = doc[i];
    cur += ch;
    if (ch == '>') {
      if (cur[cur.size() - 2] == '/') {
        string tag = cur.substr(1, cur.size() - 3);
        addEdge(adj, parent, v, tag);
      } else if (cur[1] == '/') {
        v = parent[v];
      } else {
        string tag = cur.substr(1, cur.size() - 2);
        addEdge(adj, parent, v, tag);
        v = adj.size() - 1;
      }
      cur = "";
    }
  }
  int q = stringToInt(nextLine());
  while (q--) {
    string s = nextLine();
    vector<string> seq;
    stringstream in(s);
    while (in >> s) {
      seq.push_back(s);
    }
    int res = dfs(0, adj, 0, seq);
    cout << res << endl;
  }
  return 0;
}
