#include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <vector>
#include <map>

struct DS {
  // [{'on': [('fade', true)]}, {'off': [('fade', false)]}]
  // [{'foo': [('hoge', true), ('fuga', false), ('piyo', true)]}]
  //using Tag = intmax_t;  // hashed std::string
  using Tag = std::string;
  //using FuncName = intmax_t;
  using FuncName = std::string;
  using Selector = std::vector<Tag>;
  using Property = std::pair<Selector, bool>;

  std::map<FuncName, std::vector<Property>> funcs;

  const std::vector<Property> &at(const FuncName &n) const {
    if (funcs.find(n) == funcs.end()) {
      fprintf(stderr, "has no %s()\n", n.c_str());
    }
    return funcs.at(n);
  }

  void add(const std::string &file) {
    for (size_t i=0; i<file.size(); ++i) {
      std::string fname;
      while (file[i] != '{') fname += file[i++];

      assert(file[i] == '{');
      ++i;
      
      while (file[i] != '}') {
        std::vector<Selector> sel(1);
        std::vector<bool> vis;
        while (true) {
          std::string tmp;
          while (isalpha(file[i])) tmp += file[i++];
          if (file[i] == ';') {
            if (tmp == "false") {
              vis.flip();
            } else {
              assert(tmp == "true");
            }
            assert(sel.back().empty());
            sel.pop_back();
            ++i;
            break;
          }
          sel.back().emplace_back(tmp);
          if (file[i] == '.') {
            ++i;  // pass
          } else if (file[i] == '=') {
            vis.push_back(true);
            sel.emplace_back();
            ++i;
          } else if (file[i] == '!') {
            vis.push_back(false);
            sel.emplace_back();
            i += 2;
          }
        }
        assert(sel.size() == vis.size());
        for (size_t j=sel.size(); j--;) {
          assert(sel[j].back() == "visible");
          sel[j].pop_back();
          std::string tmp;
          tmp += fname;
          funcs[tmp].emplace_back(sel[j], vis[j]);
        }
      }
    }
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s:", func.first.c_str());
      for (const auto &prop: func.second) {
        fprintf(stderr, " ");
        if (!prop.second) fprintf(stderr, "!");
        for (size_t i=0; i<prop.first.size(); ++i) {
          fprintf(stderr, "%s%s", i>0? ".":"", prop.first[i].c_str());
        }
      }
      fprintf(stderr, "\n");
    }
  }
};

class DML {
  using FuncName = std::string;
  //using Tag = intmax_t;  // hashed std::string
  using Tag = std::string;
  using Tags = std::vector<Tag>;
  using Contents = std::vector<std::pair<Tags, std::string>>;

  using Selector = Tags;
  bool match(const Selector &s, size_t i) const {
    const Tags t=contents[i].first;
    size_t n=s.size(), m=t.size();
    std::vector<std::vector<size_t>> dp(n+1, std::vector<size_t>(m+1));
    for (size_t i=0; i<n; ++i) {
      for (size_t j=0; j<m; ++j) {
        if (s[i] == t[j]) {
          dp[i+1][j+1] = dp[i][j] + 1;
        } else {
          dp[i+1][j+1] = std::max(dp[i][j+1], dp[i+1][j]);
        }
      }
    }
    return dp[n][m] == n;
  }
  
  void call(const FuncName &fname, const DS &ds) {
    const std::vector<std::pair<Selector, bool>> subr=ds.at(fname);
    for (const auto &sel: subr) {
      for (size_t i=0; i<contents.size(); ++i) {
        if (sel.second == visible[i]) continue;
        if (match(sel.first, i)) {
          visible[i] = !visible[i];
        }
      }
    }
  }

public:
  Contents contents;
  std::vector<bool> visible;
  DML(const std::string &file) {
    Tags opened;
    std::string text;
    for (size_t i=0; i<file.size(); ++i) {
      if (file[i] == '<') {
        ++i;

        if (file[i] == '/') {
          while (file[i] != '>') ++i;
          if (!text.empty()) {
            Tags tmp(opened.size());
            std::copy(opened.begin(), opened.end(), tmp.begin());
            contents.emplace_back(tmp, text);
            text.clear();
          }
          opened.pop_back();
        } else {
          std::string tag;
          while (file[i] != '>') tag += file[i++];
          if (tag == "br") {
            text += '\n';
            continue;
          }
          if (!text.empty()) {
            Tags tmp(opened.size());
            std::copy(opened.begin(), opened.end(), tmp.begin());
            contents.emplace_back(tmp, text);
            text.clear();
          }
          opened.emplace_back(tag);
        }
        continue;
      }

      text += file[i];
    }
    visible.assign(contents.size(), true);
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  std::string click(size_t h, size_t w, size_t rr, size_t cc, const DS &ds) {
    size_t r=0, c=0;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (contents[i].first.back() == "script") continue;
      for (size_t j=0; j<contents[i].second.length(); ++j) {
        if (contents[i].second[j] == '\n') {
          c = 0;
          if (++r == h || r > rr) return "";
        } else {
          if (r == rr && c == cc) {
            if (contents[i].first.back() == "link") {
              return contents[i].second;
            } else if (contents[i].first.back() == "button") {
              FuncName fname=contents[i].second;
              //fprintf(stderr, "%s() @ (%zu, %zu)\n", fname.c_str(), r, c);
              call(fname, ds);
              return "";
            }
          }
          if (++c == w) {
            c = 0;
            if (++r == h) return "";  // unreached?
          }
        }
      }
    }
    return "";
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (contents[i].first.back() == "script") continue;
      for (size_t j=0; j<contents[i].second.length(); ++j) {
        int ch=contents[i].second[j];
        if (ch == '\n') {
          c = 0;
          if (++r == h) break;
        } else {
          res[r][c] = ch;
          if (++c == w) {
            c = 0;
            if (++r == h) break;
          }
        }
      }
      if (r == h) break;
    }

    for (size_t i=0; i<h; ++i)
      printf("%s\n", res[i].c_str());
  }

  void debug() const {
    for (const auto &ct: contents) {
      for (size_t i=0; i<ct.first.size(); ++i)
        fprintf(stderr, "%s%s", i>0? ".":"", ct.first[i].c_str());
      fprintf(stderr, ": %s\n", ct.second.c_str());
    }
  }
};

int main() {
  size_t N;
  scanf("%zu", &N);

  std::vector<DML> dml;
  DS ds;
  std::map<std::string, size_t> ind;
  for (size_t i=0; i<N; ++i) {
    std::string filename, file;
    {
      char buf[32];
      scanf("%s", buf);
      filename = buf;
    }
    {
      char buf[512];
      assert(getchar() == '\n');
      fgets(buf, sizeof buf, stdin);
      file = buf;
      file.pop_back();
    }
    if (filename.back() == 's') {
      ds.add(file);
      //ds.debug();
    } else {
      ind[filename] = dml.size();
      dml.emplace_back(file);
      //dml.back().debug();
    }
  }

  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 startfile=buf;
    startfile += ".dml";
    size_t cur=ind.at(startfile);
    //fprintf(stderr, "cur: %zu\n", cur);
    dml.at(cur).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      
      std::string link=dml[cur].click(h, w, y, x, ds);
      if (!link.empty()) {
        link += ".dml";
        //fprintf(stderr, "link: %s\n", link.c_str());
        cur = ind.at(link);
        dml.at(cur).open();
      }
    }
    dml[cur].display(h, w);
  }
}

