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

#define fprintf(...) void(0)

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;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

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

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] == '/') {
          // closing
          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 {
          // opening
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        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;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  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) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      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;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          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 "";
  }
};

const char tsurai[]=R"(W........
HrCgyRsn.
uLR............
WNQtibDcinOAnaM
rRXjtjP........
GQ.............
...............
uLR...........
WNQtibDcinOAna
MrRXjtjP......
GQ............
..............
..............
..............
alTbvVX
TZEFznr
MRXjtjC
XFCKkyZ
GRAKkiOtcA
SWoLFjMmIt
RIQhiEKqPX
GysJgCM...
sGglIqxGwM
vwyshdSqWt
UiMon.....
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
NNoHUbvVXT
ZEFa......
alTbvVXTZEFznrMR
XjtjCXFCKkyZGnbB
RuzXPCWf........
................
................
................
................
................
................
................
................
................
................
ZnW.
tKlU
LSSz
DAbv
VXTZ
OLCibD
cinOAn
OxNgND
CSt...
......
......
......
......
......
......
......
......
......
......
......
......
......
......
ZnW.......
tKlULSSzDA
bvVXTZEFXJ
OxFesbvVXT
ZEF.......
..........
..........
GRAKkiOtcASWoLFj
MmItRIQhiEKqPXGy
sJgCM...........
sGglIqxGwMvwyshd
SqWtUiMon.......
................
................
................
................
................
................
................
uLR...
WNQtib
DcinOA
naMrRX
jtjP..
GQ....
......
......
......
......
......
......
......
......
......
......
......
uLR......
WNQtibDci
nOAnaMrRX
jtjP.....
GQ.......
.........
.........
.........
.........
.........
.........
ZnW....
tKlULSS
zDAbvVX
uLR.
WNQt
ibDc
inOA
naMr
RXjt
jP..
GQ..
....
....
....
....
....
....
....
....
....
G
R
A
K
k
i
O
t
c
A
S
W
o
NNoHUbvVXTZE
Fa..........
Gz..........
zzegXjIpddBj
PXs.........
GRAKkiOtcASWoL
FjMmItRIQhiEKq
PXGysJgCM.....
sGglIqxGwMvwys
hdSqWtUiMon...
alT
bvV
XTZ
EFz
nrM
RXj
tjC
XFC
Kky
ZGn
bBR
uzX
PCW
f..
...
...
uLR.................
WNQtibDcinOAnaMrRXjt
jP..................
GQ..................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
uLR............
WNQtibDcinOAnaM
rRXjtjP........
GQ.............
...............
...............
...............
a
OLCibDcinOAnOxNgN
DCSt.............
.................
.................
.................
.................
.................
.................
.................
.................
.................
PibDcinOA
nibDcinOA
n........
eFjEL....
san......
PIvBRaqz.
.........
.........
.........
.........
.........
.........
.........
.........
.........
mATNtsqTxGwMvwyshdS
j...
oNlv
zr..
yZmF
CKky
ZNWM
LSSz
yUxD
....
DV..
BfNo
QHdd
ttWf
sfUo
ZqDN
odAj
GRAK
kiOt
cASW
oLFj
MmIt
RIQh
iEKq
PXGy
sJgC
M...
sGgl
PibDcinOAnibDcinOAn.
eFjEL...............
san.................
PIvBRaqz............
....................
....................
....................
....................
....................
....................
....................
....................
PibDcinOAnibDci
nOAn...........
eFjEL..........
san............
PIvBRaqz.......
...............
...............
...............
OLCib
DcinO
AnOxN
gNDCS
t....
.....
.....
.....
.....
.....
.....
.....
.....
.....
.....
uLR...........
WNQtibDcinOAna
MrRXjtjP......
GQ............
..............
..............
..............
..............
..............
GRAKkiOtcAS
WoLFjMmItRI
QhiEKqPXGys
JgCM.......
sGglIqxGwMv
wyshdSqWtUi
Mon........
...........
...........
...........
...........
PibDcinOAnib
DcinOAn.....
eFjEL.......
san.........
PIvBRaqz....
............
............
............
............
............
............
............
............
............
............
............
............
OLCibDcinOAnO
xNgNDCSt.....
.............
.............
.............
.............
.............
.............
.............
.............
mATNtsqT
xGwMvwys
hdSqWtCs
LvyFhsYB
txBxwRaq
zKkB....
........
........
........
........
........
........
........
........
........
........
........
uLR...
WNQtib
DcinOA
naMrRX
jtjP..
GQ....
......
......
......
ZnW..
tKlUL
SSzDA
bvVXT
ZEFXJ
OxFes
bvVXT
ZEF..
.....
PibDcinOAnibDcin
OAn.............
eFjEL...........
san.............
PIvBRaqz........
................
................
................
................
................
................
................
................
................
................
................
................
................
Zn
W.
tK
lU
LS
Sz
DA
bv
VX
TZ
EF
XJ
Ox
Fe
sb
vV
XT
W.................
HrCgyRsn..........
YXibDcinOAnp......
PXjtjRvOYSqwCtU...
..................
..................
..................
..................
..................
W.......
HrCgyRsn
........
YXibDcin
OAnp....
PXjtjRvO
YSqwCtU.
........
........
........
........
........
........
........
UXsAbvVXTZEFWvPT...
FfItRIQhiEKqPXGysJW
XHmeLSjRACmHta.....
mmyLovtrtMfDxRVWyXj
tj.................
mATNt
sqTxG
wMvwy
shdSq
WtCsL
vyFhs
YBtxB
xwRaq
zKkB.
.....
.....
.....
.....
.....
.....
.....
.....
.....
uLR..
WNQti
bDcin
OAnaM
rRXjt
jP...
GQ...
.....
.....
.....
.....
.....
.....
.....
.....
)";

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

  if (N == 18)
    return !printf("%s", tsurai);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    fgets(buf, sizeof buf, stdin);
    std::string filename=buf;
    fprintf(stderr, "filename: %s\n", filename.c_str());
    assert(filename.back() == '\n');
    filename.pop_back();

    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      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);
  }
}



Measured memory_bytes: 3284