#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:65777216")
using namespace std;
map<string, int> _id;
int getID(string s) {
  if (_id.count(s)) return _id[s];
  int pos = _id.size();
  return _id[s] = pos;
}
char s[1111111];
int main() {
  scanf(" %s", s);
  int n = strlen(s);
  stack<int> st;
  vector<int> b;
  b.push_back(0);
  _id[""] = 0;
  for (int i = 0; i < n;) {
    i++;
    bool end = 0;
    if (s[i] == '/') {
      end = 1;
      i++;
    }
    string t;
    while (isalpha(s[i])) t.push_back(s[i]), i++;
    int id = getID(t);
    bool end2 = 0;
    if (s[i] == '/') {
      end2 = 1;
      i++;
    }
    i++;
    if (end2) {
      st.push(b.size());
      b.push_back(id);
      b.push_back(-st.top());
      st.pop();
    } else if (end) {
      b.push_back(-st.top());
      st.pop();
    } else {
      st.push(b.size());
      b.push_back(id);
    }
  }
  cin >> n;
  string t;
  getline(cin, t);
  for (int i(0); i < (n); i++) {
    getline(cin, t);
    stringstream ss(t);
    vector<int> v;
    bool shit = 0;
    while (ss >> t) {
      if (!_id.count(t)) {
        shit = 1;
        break;
      }
      v.push_back(_id[t]);
    }
    int res = 0;
    if (!shit) {
      int currpos = 0;
      vector<int> started;
      for (int j(0); j < (b.size()); j++) {
        if (b[j] < 0) {
          if (started.size() && started.back() == -b[j]) {
            started.pop_back();
            currpos--;
          }
        } else {
          if (v[currpos] == b[j]) {
            if (currpos + 1 < v.size()) {
              started.push_back(j);
              currpos++;
            } else {
              res++;
            }
          }
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}
