#include <bits/stdc++.h>
using namespace std;
string komachi;
map<pair<string, int>, int> chen;
vector<int> ran[500000];
stack<int> aya;
int yuuka, alice, sakuya, youmu, rumia;
string yukari[500000], marisa, yuyuko[300];
void dfs(int nue, int murasa) {
  if (murasa <= youmu && yuyuko[murasa] == yukari[nue]) murasa++;
  if (murasa > youmu && yukari[nue] == yuyuko[youmu]) rumia++;
  for (__typeof(ran[0].end()) mema = ran[nue].begin(); mema != ran[nue].end();
       ++mema)
    dfs(*mema, murasa);
}
int main() {
  getline(cin, komachi);
  komachi = "<#>" + komachi;
  komachi += "</#>";
  yuuka = komachi.length();
  for (int i = 0; i < yuuka; ++i)
    if (komachi[i] == '<' || komachi[i] == '>') komachi[i] = ' ';
  istringstream reimu(komachi);
  for (; reimu >> marisa;)
    if (marisa[0] == '/')
      aya.pop();
    else {
      ++alice;
      if (!aya.empty()) ran[aya.top()].push_back(alice);
      if (marisa[marisa.length() - 1] != '/')
        aya.push(alice);
      else
        marisa = marisa.substr(0, marisa.length() - 1);
      yukari[alice] = marisa;
    }
  cin >> sakuya;
  for (getline(cin, komachi); sakuya--;) {
    getline(cin, komachi);
    istringstream reimu(komachi);
    youmu = 0;
    for (; reimu >> marisa;) yuyuko[++youmu] = marisa;
    rumia = 0;
    dfs(1, 1);
    cout << rumia << endl;
  }
  return 0;
}
