#include <bits/stdc++.h>
using namespace std;
const int MOD(1000000007);
const int INF((1 << 30) - 1);
const int MAXN(1000005);
string a, name[MAXN];
vector<int> tree[MAXN];
int now = 1, idx = 0;
void dfs(int node) {
  while (idx < a.size()) {
    idx++;
    bool open = 0, close = 0, self = 0;
    if (a[idx] == '/') {
      close = true;
      idx++;
    }
    string t = "";
    while (idx < a.size() && a[idx] != '/' && a[idx] != '>') {
      t += a[idx];
      idx++;
    }
    if (a[idx] == '/') {
      self = true;
      idx++;
    } else
      open = true;
    idx++;
    if (close)
      break;
    else if (open) {
      tree[node].push_back(now);
      name[now] = t;
      now++;
      dfs(now - 1);
    } else if (self) {
      tree[node].push_back(now);
      name[now] = t;
      now++;
    }
  }
}
vector<string> d;
int ans;
void dfs2(int node, int idx) {
  if (d[idx + 1] == name[node]) idx++;
  if (idx == d.size() - 1) {
    ans++;
    idx--;
  }
  for (int i = 0; i < tree[node].size(); i++) dfs2(tree[node][i], idx);
}
int main() {
  getline(cin, a);
  name[0] = "?";
  dfs(0);
  int m;
  char M[10];
  gets(M);
  m = atoi(M);
  while (m--) {
    string b;
    getline(cin, b);
    d.clear();
    d.push_back("?");
    int k = 0;
    while (k < b.size()) {
      int l = b.find(" ", k);
      if (l == -1) l = b.size();
      d.push_back(b.substr(k, l - k));
      k = l + 1;
    }
    ans = 0;
    dfs2(0, -1);
    cout << ans << endl;
  }
}
