#include <bits/stdc++.h>
using namespace std;
struct Node {
  string data;
  Node* parent;
  int id;
  Node(string d = "", Node* p = NULL, int i = -1) {
    data = d;
    parent = p;
    id = i;
  }
};
const int maxDoc = 1e6 + 10;
const int maxNode = 3 * 1e5;
const int maxQuery = 210 * 10;
char doc[maxDoc];
string query[maxQuery];
Node nodes[maxNode];
int solved[maxNode];
int main() {
  scanf("%s", doc);
  int docLen = strlen(doc);
  int idx = 1;
  nodes[0] = Node("ROOT", NULL, 0);
  Node* pt = &(nodes[0]);
  int N = 1;
  while (idx < docLen) {
    if (doc[idx] == '/') {
      int j = idx + 1;
      while (doc[j] != '>') j++;
      idx = j + 2;
      pt = (*pt).parent;
    } else {
      int j = idx;
      while (doc[j] != '/' && doc[j] != '>') j++;
      nodes[N] = Node(string(&doc[idx], j - idx), pt, N);
      if (doc[j] != '/')
        pt = &(nodes[N]);
      else
        j++;
      N++;
      idx = j + 2;
    }
  }
  int M;
  char lines[maxQuery];
  scanf("%d", &M);
  gets(lines);
  while (M--) {
    gets(lines);
    int idx = 0;
    int Q = 0;
    int len = strlen(lines);
    while (idx < len) {
      int j = idx;
      while (j < len && lines[j] != ' ') j++;
      query[Q++] = string(&lines[idx], j - idx);
      idx = j + 1;
    }
    int res = 0;
    int solved[N];
    solved[0] = 0;
    for (int i = 1; i < N; i++) {
      int pos = solved[(nodes[i].parent)->id];
      bool isMatch = (nodes[i].data == query[pos]);
      if (isMatch) {
        if (pos == Q - 1)
          res++;
        else
          pos++;
      }
      solved[i] = pos;
    }
    cout << res << endl;
  }
  return 0;
}
