#include <bits/stdc++.h>
using namespace std;
char buf[4000010];
vector<string> tags;
stack<string> parser;
string symbol;
char c;
int k;
stringstream ss;
vector<int> E[2000010];
struct node {
  string id;
  vector<node> children;
  node* parent;
  node(string _id = "") { id = _id; }
  inline void get() {
    while (!ss.eof()) {
      c = ss.get();
      if (ss.eof()) break;
      assert(c == '<');
      getline(ss, symbol, '>');
      k = symbol.length() - 1;
      if (symbol[0] == '/') {
        break;
      } else if (k > 0 && symbol[k] == '/') {
        children.push_back(node(symbol.substr(0, k)));
      } else {
        children.push_back(node(symbol));
        children.back().get();
      }
    }
  }
  int query(int found) {
    if ((found < tags.size()) && (id == tags[found])) found++;
    int ans = 0;
    for (int i = 0; i < children.size(); i++) {
      ans += children[i].query(found);
    }
    if ((found == tags.size()) && (id == tags.back())) ans++;
    return ans;
  }
  void print(int d) {
    for (int i = 0; i < d; i++) cout << " ";
    cout << id << endl;
    for (int i = 0; i < children.size(); i++) {
      children[i].print(d + 1);
    }
  }
};
node root;
void newget() {
  node* cur = &root;
  while (!ss.eof()) {
    c = ss.get();
    if (ss.eof()) break;
    assert(c == '<');
    getline(ss, symbol, '>');
    k = symbol.length() - 1;
    if (symbol[0] == '/') {
      cur = cur->parent;
    } else if (k > 0 && symbol[k] == '/') {
      cur->children.push_back(node(symbol.substr(0, k)));
    } else {
      cur->children.push_back(node(symbol));
      cur->children.back().parent = cur;
      cur = &(cur->children.back());
    }
  }
  assert(cur == &root);
}
int newquery() {
  int ans = 0;
  queue<pair<node*, int> > Q;
  Q.push(make_pair(&root, 0));
  while (!Q.empty()) {
    pair<node*, int> f = Q.front();
    Q.pop();
    node* cur = f.first;
    int found = f.second;
    if ((found < tags.size()) && (cur->id == tags[found])) found++;
    int numC = cur->children.size();
    for (int i = 0; i < numC; i++) {
      Q.push(make_pair(&(cur->children[i]), found));
    }
    if ((found == tags.size()) && (cur->id == tags.back())) ans++;
  }
  return ans;
}
string dat;
int main() {
  cin >> dat;
  ss << dat;
  root.id = "";
  newget();
  int M;
  cin >> M;
  cin.ignore();
  for (int q = 0; q < M; q++) {
    tags.clear();
    string line;
    getline(cin, line);
    stringstream xx;
    xx << line;
    while (!xx.eof()) {
      string sym;
      xx >> sym;
      tags.push_back(sym);
    }
    cout << newquery() << endl;
  }
}
