#include <bits/stdc++.h>
std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res = ss[0];
  for (size_t i = 1; i < ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}
struct DScript {
  using Selector = std::string;
  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;
  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it = s.cbegin(); it != s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(func, std::vector<std::pair<Selector, bool>>(0));
      it = m[0].second;
      assert(*it == '{');
      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs = true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i = sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(std::move(sels[i]), rhs);
          }
          sels.clear();
          prop.clear();
          assert(*it == ';');
          ++it;
          continue;
        }
        void(0);
        for (int i = 0; i < 8; ++i) sel.pop_back();
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += +"\\b";
        void(0);
        if (*it == '=') {
          prop.push_back(false);
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);
          it += 2;
        }
      }
      ++it;
    }
    debug();
  }
  void debug() const {
    for (const auto &func : funcs) {
      void(0);
      for (size_t i = 0; i < func.second.size(); ++i) {
        void(0);
      }
    }
  }
};
struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;
  DMLang(const std::string &s) : contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it = s.cbegin(); it != s.cend(); it = m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);
      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first + 1, m[0].first + 4) == "br>") {
          text += '\n';
        } else {
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first + 1, m[0].second - 1);
        }
      } else {
        text += std::string(m[0].first, m[0].second);
      }
    }
  }
  void debug() const {
    for (const auto &content : contents) {
      Tags tags = content.first;
      std::string text = content.second;
      void(0);
    }
  }
  void open() { visible.assign(contents.size(), true); }
  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r = 0, c = 0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i = 0; i < contents.size(); ++i) {
      void(0);
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text = contents[i].second;
      for (size_t j = 0; j < text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }
  done:
    for (const auto &s : res) printf("%s\n", s.c_str());
  }
  std::string click(size_t h, size_t w, size_t cr, size_t cc,
                    const DScript &ds) {
    size_t r = 0, c = 0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i = 0; i < contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text = contents[i].second;
      for (size_t j = 0; j < text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          void(0);
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }
  link:
    return clicked;
  button:
    const auto &func = ds.funcs.at(clicked);
    for (const auto &p : func) {
      std::regex selector(p.first);
      bool prop = p.second;
      for (size_t i = 0; i < contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector)) visible[i] = prop;
      }
    }
    return "";
  }
};
int main() {
  size_t N;
  scanf("%zu", &N);
  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i = 0; i < N; ++i) {
    char buf[512];
    scanf("%s ", buf);
    std::string filename = buf;
    fgets(buf, sizeof buf, stdin);
    std::string file = buf;
    assert(file.back() == '\n');
    file.pop_back();
    if (filename.back() == 's') {
      ds.append(file);
    } else {
      for (int j = 0; j < 4; ++j) filename.pop_back();
      dml.emplace(filename, DMLang(file));
    }
  }
  size_t M;
  scanf("%zu", &M);
  for (size_t i = 0; i < M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename = buf;
    dml.at(filename).open();
    for (int j = 0; j < s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link = dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
