#include <bits/stdc++.h>
using namespace std;
struct node {
  long long val;
  node* parent;
  list<node*> childs;
};
char a[1000005];
int pos = 0;
int ans = 0;
node root;
long long calcVal(char* arr, int finishPos) {
  long long rez = 0;
  for (int i = 0; i != finishPos; i++) {
    rez = rez * 27 + (arr[i] - 'a' + 1);
  }
  return rez;
}
long long mass[205];
int st[205];
int n, m;
void dfs(node* nod, int state) {
  int tmp = state;
  if (mass[tmp] == nod->val) {
    ++tmp;
  }
  if (tmp == m) {
    ++ans;
  }
  for (list<node*>::iterator it = nod->childs.begin(); it != nod->childs.end();
       it++) {
    dfs(*it, tmp != m ? tmp : m - 1);
  }
}
void buildTree() {
  node* cur = &root;
  do {
    int start = pos;
    while (a[pos] != '>') {
      ++pos;
    }
    int state = 0;
    if (a[pos - 1] == '/') {
      state = 1;
    } else if (a[start + 1] == '/') {
      state = 2;
    }
    node* addNode;
    switch (state) {
      case 0:
        addNode = new node();
        cur->childs.push_back(addNode);
        addNode->parent = cur;
        cur = addNode;
        cur->val = calcVal(a + start + 1, pos - start - 1);
        break;
      case 1:
        addNode = new node();
        cur->childs.push_back(addNode);
        addNode->parent = cur;
        addNode->val = calcVal(a + start + 1, pos - start - 2);
        break;
      case 2:
        cur = cur->parent;
        break;
    }
    ++pos;
  } while (a[pos] == '<');
}
int main() {
  cin.getline(a, 1000005);
  buildTree();
  cin >> n;
  cin.getline(a, 1000005);
  for (int i = 0; i != n; i++) {
    cin.getline(a, 1000005);
    stringstream sstr;
    sstr << a;
    pos = 0;
    while (sstr >> a) {
      int j = 0;
      while (a[j]) {
        ++j;
      }
      mass[pos] = calcVal(a, j);
      st[pos++] = 0;
    }
    m = pos;
    mass[m] = -1;
    ans = 0;
    for (list<node*>::iterator it = root.childs.begin();
         it != root.childs.end(); it++) {
      dfs(*it, 0);
    }
    cout << ans << endl;
  }
}
