#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
int n;
int m;
char s[1000500];
int lol[1000500];
int main() {
  gets(s);
  n = strlen(s);
  string str = s;
  replace((str).begin(), (str).end(), '<', ' ');
  replace((str).begin(), (str).end(), '>', ' ');
  stringstream sin(str);
  vector<string> v;
  string tmp;
  string end = "-";
  while (sin >> tmp) {
    if (tmp[0] == '/')
      v.push_back(end);
    else if (tmp[((int)((tmp).size())) - 1] == '/') {
      tmp = tmp.substr(0, ((int)((tmp).size())) - 1);
      v.push_back(tmp);
      v.push_back(end);
    } else
      v.push_back(tmp);
  }
  cin >> n;
  gets(s);
  for (int u = (0); u < (n); u++) {
    gets(s);
    string str = s;
    stringstream sin(str);
    vector<string> cur;
    string tmp;
    while (sin >> tmp) cur.push_back(tmp);
    int p = 0;
    int cp = 0;
    int ans = 0;
    for (int i = (0); i < (((int)((v).size()))); i++) {
      if (v[i] == end) {
        if (lol[cp - 1]) p--;
        cp--;
      } else {
        if (p < ((int)((cur).size())) && v[i] == cur[p]) {
          lol[cp] = 1;
          p++;
        } else
          lol[cp] = 0;
        cp++;
        if (p == ((int)((cur).size())) && v[i] == cur.back()) ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
