#include <bits/stdc++.h>
using namespace std;
struct Node;
struct Fun;
int h, w, cr, cc;
char scr[512][512];
Node *lnk[512][512];
Fun *evt[512][512];
map<string, Node *> dmls;
map<string, Fun *> funs;
Node *act;
void newline() {
  cr++;
  cc = 0;
}
void draw(const string &s, Node *hl, Fun *fn) {
  for (int i = 0; i < (int)(s.size()); i++) {
    if (cc == w) newline();
    if (0 <= cr && cr < h && 0 <= cc && cc < w) {
      scr[cr][cc] = s[i];
      lnk[cr][cc] = hl;
      evt[cr][cc] = fn;
      cc++;
    }
  }
}
struct Node {
  string tag;
  string text;
  vector<Node *> cs;
  bool visible;
  Node(const string &tag) : tag(tag), visible(true) {}
  void dump() const { dump(0); }
  void dump(int dep) const {
    for (int _ = 0; _ < (int)(dep); _++) cerr << ' ';
    cerr << '<' << tag << '>';
    cerr << " visi = " << visible;
    cerr << " text = " << text << endl;
    for (int i = 0; i < (int)(cs.size()); i++) cs[i]->dump(dep + 2);
    for (int _ = 0; _ < (int)(dep); _++) cerr << ' ';
    cerr << "</" << tag << '>' << endl;
    ;
  }
  void render() const {
    if (!visible) return;
    if (tag == "$text")
      draw(text, 0, 0);
    else if (tag == "script")
      ;
    else if (tag == "link") {
      assert(cs.size() == 1 && cs[0]->tag == "$text");
      draw(cs[0]->text, dmls[cs[0]->text], 0);
    } else if (tag == "button") {
      assert(cs.size() == 1 && cs[0]->tag == "$text");
      draw(cs[0]->text, 0, funs[cs[0]->text]);
    } else if (tag == "br")
      newline();
    else {
      for (int i = 0; i < (int)(cs.size()); i++) cs[i]->render();
    }
  }
  void apply(const vector<string> &vs, int k, bool visi) {
    if (vs[k] == tag) {
      if (k == (int)vs.size() - 1) {
        visible = visi;
      } else {
        k++;
      }
    }
    for (int i = 0; i < (int)(cs.size()); i++) cs[i]->apply(vs, k, visi);
  }
};
struct Fun {
  vector<pair<vector<string>, bool> > asn;
  void exec() {
    for (int i = 0; i < (int)(asn.size()); i++) {
      act->apply(asn[i].first, 0, asn[i].second);
    }
  }
};
vector<string> lex_dml(const string &s) {
  vector<string> ts;
  int pos = 0;
  for (int i = 0; i < (int)(s.size()); i++) {
    if (s[i] == '<') {
      ts.push_back(s.substr(pos, i - pos));
      pos = i;
    } else if (s[i] == '>') {
      ts.push_back(s.substr(pos, i + 1 - pos));
      pos = i + 1;
    }
  }
  ts.push_back(s.substr(pos));
  return ts;
}
bool isbegin(const string &t) {
  if (t.size() < 3) return false;
  if (t[0] != '<' || t[t.size() - 1] != '>') return false;
  for (int i = 1; i < (int)t.size() - 1; i++) {
    if (!islower(t[i]) && !isupper(t[i])) return false;
  }
  return true;
}
bool isend(const string &t) {
  if (t.size() < 4) return false;
  if (t[0] != '<' || t[1] != '/' || t[t.size() - 1] != '>') return false;
  for (int i = 2; i < (int)t.size() - 1; i++) {
    if (!islower(t[i]) && !isupper(t[i])) return false;
  }
  return true;
}
Node *parse_dml(const vector<string> &ts) {
  Node *root = new Node("root");
  vector<Node *> stk;
  stk.push_back(root);
  for (int i = 0; i < (int)(ts.size()); i++) {
    if (ts[i].size() == 0) continue;
    if (isbegin(ts[i])) {
      Node *node = new Node(ts[i].substr(1, ts[i].size() - 2));
      stk.back()->cs.push_back(node);
      if (ts[i] != "<br>") stk.push_back(node);
    } else if (isend(ts[i])) {
      assert(stk.back()->tag == ts[i].substr(2, ts[i].size() - 3));
      stk.pop_back();
    } else {
      Node *node = new Node("$text");
      node->text = ts[i];
      stk.back()->cs.push_back(node);
    }
  }
  return root;
}
vector<string> parse_prop(const string &s) {
  vector<string> ps;
  int pos = 0;
  for (int i = 0; i < (int)(s.size()); i++) {
    if (s[i] == '.') {
      ps.push_back(s.substr(pos, i - pos));
      pos = i + 1;
    }
  }
  assert(s.substr(pos) == "visible");
  return ps;
}
string _s;
unsigned _ix;
vector<pair<vector<string>, bool> > parse_expr() {
  unsigned pos = _ix;
  vector<string> props;
  vector<bool> rev;
  while (_s[_ix] != ';') {
    if (_s[_ix] == '!' || _s[_ix] == '=') {
      props.push_back(_s.substr(pos, _ix - pos));
      if (_s[_ix] == '!') {
        _ix += 2;
        pos = _ix;
        rev.push_back(true);
      } else {
        _ix += 1;
        pos = _ix;
        rev.push_back(false);
      }
    } else {
      _ix++;
    }
  }
  string val = _s.substr(pos, _ix - pos);
  bool cur = val == "true";
  vector<pair<vector<string>, bool> > rs(props.size());
  for (int i = (int)props.size() - 1; i >= 0; i--) {
    if (rev[i]) cur = !cur;
    rs[i].first = parse_prop(props[i]);
    rs[i].second = cur;
  }
  _ix++;
  return rs;
}
pair<string, Fun *> parse_fun() {
  Fun *fun = new Fun();
  const unsigned st = _ix;
  while (_s[_ix] != '{') _ix++;
  const string id = _s.substr(st, _ix - st);
  _ix++;
  while (_s[_ix] != '}') {
    vector<pair<vector<string>, bool> > es(parse_expr());
    for (int i = 0; i < (int)(es.size()); i++) fun->asn.push_back(es[i]);
  }
  _ix++;
  return make_pair(id, fun);
}
vector<pair<string, Fun *> > parse_ds(const string &s) {
  _s = s;
  _ix = 0;
  vector<pair<string, Fun *> > fs;
  while (_ix < _s.size()) {
    fs.push_back(parse_fun());
  }
  return fs;
}
void render(Node *file) {
  for (int i = 0; i < (int)(h); i++)
    for (int j = 0; j < (int)(w); j++) {
      scr[i][j] = '.';
      lnk[i][j] = 0;
      evt[i][j] = 0;
    }
  cr = cc = 0;
  file->render();
  while (cr < h) newline();
  act = file;
}
void click(int x, int y) {
  if (lnk[y][x]) {
    render(lnk[y][x]);
  }
  if (evt[y][x]) {
    evt[y][x]->exec();
    render(act);
  }
}
int main() {
  string s;
  getline(cin, s);
  const int n = atoi(s.c_str());
  for (int _ = 0; _ < (int)(n); _++) {
    getline(cin, s);
    if (s.substr(s.size() - 4) == ".dml") {
      const string file = s.substr(0, s.size() - 4);
      ;
      getline(cin, s);
      vector<string> ts = lex_dml(s);
      Node *root = parse_dml(ts);
      dmls[file] = root;
    } else if (s.substr(s.size() - 3) == ".ds") {
      const string file = s.substr(0, s.size() - 3);
      ;
      getline(cin, s);
      vector<pair<string, Fun *> > fs = parse_ds(s);
      for (int i = 0; i < (int)(fs.size()); i++)
        funs[fs[i].first] = fs[i].second;
    } else
      assert(false);
  }
  getline(cin, s);
  const int m = atoi(s.c_str());
  for (int _ = 0; _ < (int)(m); _++) {
    int K;
    char buf[32];
    getline(cin, s);
    sscanf(s.c_str(), "%d %d %d %s", &w, &h, &K, buf);
    render(dmls[buf]);
    for (int _ = 0; _ < (int)(K); _++) {
      int x, y;
      getline(cin, s);
      sscanf(s.c_str(), "%d%d", &x, &y);
      click(x, y);
    }
    for (int i = 0; i < (int)(h); i++) {
      for (int j = 0; j < (int)(w); j++) putchar(scr[i][j]);
      putchar('\n');
    }
  }
  return 0;
}
