#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;
struct node {
  map<int, int> next;
  int prev, cnt;
};
const int MAXL = 1001000;
const int MAXS = 500100;
map<string, int> ids;
node tree[MAXS];
char buf[MAXL];
vector<int> l;
int size = 1;
int getID(const string &s) {
  if (!ids.count(s)) {
    int ans = (int)ids.size();
    ids[s] = ans;
  }
  return ids[s];
}
int add(int v, const string &s, int value) {
  int c = getID(s);
  if (!tree[v].next.count(c)) {
    tree[v].next[c] = size;
    tree[size].prev = v;
    size++;
  }
  v = tree[v].next[c];
  tree[v].cnt += value;
  return v;
}
int DFS(int v, int pos) {
  int ans = 0;
  if (pos == (int)l.size() - 1) {
    if (tree[v].next.count(l[pos])) {
      int u = tree[v].next[l[pos]];
      ans += tree[u].cnt;
    }
    for (map<int, int>::iterator i = tree[v].next.begin();
         i != tree[v].next.end(); i++)
      ans += DFS(i->second, pos);
  } else {
    if (tree[v].next.count(l[pos])) {
      int u = tree[v].next[l[pos]];
      ans += DFS(u, pos + 1);
    }
    for (map<int, int>::iterator i = tree[v].next.begin();
         i != tree[v].next.end(); i++)
      if (i->first != l[pos]) ans += DFS(i->second, pos);
  }
  return ans;
}
int main() {
  scanf(" %s", buf);
  string s = buf;
  int v = 0;
  for (int i = 0; i < (int)(s.size()); i++) {
    string word = "";
    while (true) {
      word += s[i];
      if (s[i] == '>') break;
      i++;
    }
    if (word[1] == '/') {
      v = tree[v].prev;
    } else if (word[word.size() - 2] == '/') {
      word.erase(0, 1);
      word.erase(word.size() - 2, 2);
      add(v, word, 1);
    } else {
      word.erase(0, 1);
      word.erase(word.size() - 1, 1);
      v = add(v, word, 1);
    }
  }
  int queries;
  scanf("%d\n", &queries);
  for (int query = 0; query < (int)(queries); query++) {
    gets(buf);
    l.clear();
    int n = (int)strlen(buf);
    string word = "";
    for (int i = 0; i < (int)(n + 1); i++)
      if (isalpha(buf[i]))
        word += buf[i];
      else {
        l.push_back(getID(word));
        word = "";
      }
    cout << DFS(0, 0) << endl;
  }
  return 0;
}
