#include <bits/stdc++.h>
using namespace std;
int main() {
  string* S_in = new string;
  cin >> *S_in;
  int m, n, i, j, k;
  int point;
  int count = 0;
  int type;
  cin >> m;
  string temp;
  vector<string>* S = new vector<string>;
  vector<bool>* t = new vector<bool>;
  n = S_in->size();
  for (i = 0; i < n; ++i) {
    if ((*S_in)[i] == '<') {
      type = 0;
      temp = "";
    } else if ((*S_in)[i] == '>') {
      if (type == 0) {
        S->push_back(temp);
        t->push_back(true);
      }
      if (type == 1) {
        S->push_back(temp);
        t->push_back(false);
      }
      if (type == 2) {
        S->push_back(temp);
        S->push_back(temp);
        t->push_back(true);
        t->push_back(false);
      }
    } else if ((*S_in)[i] == '/') {
      if (temp == "") {
        type = 1;
      } else {
        type = 2;
      }
    } else {
      temp += (*S_in)[i];
    }
  }
  delete S_in;
  vector<string>* Que = new vector<string>;
  vector<int>* num = new vector<int>;
  int Q;
  string str;
  getline(cin, str);
  int Slen = S->size();
  for (i = 0; i < m; ++i) {
    temp = "";
    count = 0;
    Que->clear();
    num->clear();
    getline(cin, str);
    n = str.size();
    for (j = 0; j < n; ++j) {
      if (str[j] == ' ') {
        Que->push_back(temp);
        num->push_back(0);
        temp = "";
      } else {
        temp += str[j];
      }
    }
    Que->push_back(temp);
    num->push_back(0);
    Q = Que->size();
    point = 0;
    for (k = 0; k < Slen; ++k) {
      if (point == Q - 1) {
        if ((*S)[k] == (*Que)[point] && (*t)[k] == true) {
          ++count;
          ++(*num)[point];
        } else if ((*S)[k] == (*Que)[point] && (*t)[k] == false &&
                   (*num)[point] != 0) {
          --(*num)[point];
        } else if (point > 0 && (*S)[k] == (*Que)[point - 1]) {
          if ((*t)[k] == true) {
            ++(*num)[point - 1];
          }
          if ((*t)[k] == false) {
            --(*num)[point - 1];
            if ((*num)[point - 1] == 0) {
              --point;
            }
          }
        }
      } else if ((*S)[k] == (*Que)[point] && (*t)[k] == true) {
        ++(*num)[point];
        ++point;
      } else if (point > 0) {
        if ((*S)[k] == (*Que)[point - 1] && (*t)[k] == true) {
          ++(*num)[point - 1];
        } else if ((*S)[k] == (*Que)[point - 1] && (*t)[k] == false) {
          --(*num)[point - 1];
          if ((*num)[point - 1] == 0) {
            --point;
          }
        }
      }
    }
    cout << count << "\n";
  }
  return 0;
}
