#include <bits/stdc++.h>
using namespace std;
char doc[1000005];
int m, n;
int ans;
char s[1000005];
char tag[300000][11];
vector<int> adj[300000];
vector<string> name;
void build() {
  stack<int> st;
  int index = 0;
  int len = strlen(doc);
  for (int i = 0; i < len;) {
    if (doc[i + 1] == '/') {
      int j = i + 2;
      while (j < len && doc[j] != '>') j++;
      i = j + 1;
      st.pop();
    } else {
      int j = i + 1;
      while (j < len) {
        if (doc[j] == '/') {
          index++;
          if (st.empty()) {
            for (int k = i + 1; k < j; k++) tag[index][k - i - 1] = doc[k];
            adj[0].push_back(index);
          } else {
            for (int k = i + 1; k < j; k++) tag[index][k - i - 1] = doc[k];
            adj[st.top()].push_back(index);
          }
          j++;
          break;
        } else if (doc[j] == '>') {
          index++;
          if (st.empty()) {
            for (int k = i + 1; k < j; k++) tag[index][k - i - 1] = doc[k];
            adj[0].push_back(index);
          } else {
            for (int k = i + 1; k < j; k++) tag[index][k - i - 1] = doc[k];
            adj[st.top()].push_back(index);
          }
          st.push(index);
          break;
        }
        j++;
      }
      i = j + 1;
    }
  }
}
void dfs(int cur, int x) {
  if (x == n - 1 && tag[cur] == name[n - 1]) ans++;
  if (x < n - 1 && tag[cur] == name[x]) x++;
  for (int i = 0; i < adj[cur].size(); i++) dfs(adj[cur][i], x);
}
int main() {
  scanf("%s", doc);
  build();
  scanf("%d\n", &m);
  for (int i = 0; i < m; i++) {
    gets(s);
    name.clear();
    istringstream ss(s);
    string cur;
    while (ss >> cur) name.push_back(cur);
    n = name.size();
    ans = 0;
    for (int j = 0; j < adj[0].size(); j++) dfs(adj[0][j], 0);
    cout << ans << endl;
  }
  return 0;
}
