#include <bits/stdc++.h>
using namespace std;
enum { T_DML, T_SC, T_BR, T_LN, T_BT, T_TAG, T_STR };
struct Tag {
  string name;
  int type;
  bool v;
  vector<struct Tag> cs;
  Tag() : v(true) {}
  Tag(string _name, int _type) : name(_name), type(_type), v(true) {}
  void setv(bool vv = true) {
    v = vv;
    for (int i = 0; i < cs.size(); i++) cs[i].setv(vv);
  }
  void print() {
    if (type == T_STR)
      cout << '"' << name << '"' << endl;
    else {
      cout << ("<" + name + ">") << endl;
      for (int i = 0; i < cs.size(); i++) cs[i].print();
      cout << ("</" + name + ">") << endl;
    }
  }
};
enum { V_CONST, V_EQ, V_NEQ };
struct Visible {
  int type;
  bool v;
  vector<string> ids;
};
map<string, Tag> dmls;
map<string, map<string, vector<Visible> > > dss;
void parse_dml(string &s, int &pos, Tag &tag) {
  if (pos >= s.size()) return;
  if (s[pos] == '<') {
    pos++;
    tag.name = "";
    while (s[pos] != '>') tag.name += s[pos++];
    pos++;
    if (tag.name == "dml")
      tag.type = T_DML;
    else if (tag.name == "script")
      tag.type = T_SC;
    else if (tag.name == "br") {
      tag.type = T_BR;
      return;
    } else if (tag.name == "link")
      tag.type = T_LN;
    else if (tag.name == "button")
      tag.type = T_BT;
    else
      tag.type = T_TAG;
    for (;;) {
      if (s[pos] == '<' && s[pos + 1] == '/') {
        while (s[pos++] != '>')
          ;
        break;
      }
      Tag ctag;
      parse_dml(s, pos, ctag);
      tag.cs.push_back(ctag);
    }
  } else {
    tag.type = T_STR;
    tag.name = "";
    while (pos < s.size() && s[pos] != '<') tag.name += s[pos++];
  }
}
void parse_ds(string &s, int &pos, map<string, vector<Visible> > &ds) {
  while (pos < s.size()) {
    string sr;
    while (s[pos] != '{') sr += s[pos++];
    pos++;
    vector<Visible> exp;
    Visible vb;
    while (s[pos] != '}') {
      string t;
      while (s[pos] != '.' && s[pos] != '!' && s[pos] != '=' && s[pos] != ';')
        t += s[pos++];
      if (s[pos] == ';') {
        vb.type = V_CONST;
        vb.v = (t == "true");
        vb.ids.clear();
        exp.push_back(vb);
        pos++;
      } else if (s[pos] == '=') {
        vb.type = V_EQ;
        exp.push_back(vb);
        vb.ids.clear();
        pos++;
      } else if (s[pos] == '!') {
        vb.type = V_NEQ;
        exp.push_back(vb);
        vb.ids.clear();
        pos++;
        pos++;
      } else {
        vb.ids.push_back(t);
        pos++;
      }
    }
    pos++;
    ds[sr] = exp;
  }
}
void print_ds(string &name, map<string, vector<Visible> > &ds) {
  cout << name << endl;
  for (map<string, vector<Visible> >::iterator mit = ds.begin();
       mit != ds.end(); mit++) {
    cout << "  " << mit->first << " = ";
    vector<Visible> &exp = mit->second;
    for (int i = 0; i < exp.size(); i++) {
      if (exp[i].type == V_CONST)
        cout << (exp[i].v ? "true;" : "false;");
      else {
        for (int j = 0; j < exp[i].ids.size(); j++) {
          if (j > 0) cout << '.';
          cout << exp[i].ids[j];
        }
        cout << (exp[i].type == V_EQ ? "=" : "!=");
      }
    }
    cout << endl;
  }
}
void read_scripts(Tag &tag, map<string, vector<Visible> > &ds) {
  if (tag.type == T_SC) {
    map<string, vector<Visible> > &ds0 = dss[tag.cs[0].name];
    for (map<string, vector<Visible> >::iterator mit = ds0.begin();
         mit != ds0.end(); mit++)
      ds[mit->first] = mit->second;
  } else if (tag.type == T_DML || tag.type == T_TAG)
    for (int i = 0; i < tag.cs.size(); i++) read_scripts(tag.cs[i], ds);
}
void display_str(string &s, vector<string> &dsp, vector<vector<Tag *> > &attr,
                 int &cx, int &cy, Tag *tp = NULL) {
  int n = s.size(), h = dsp.size(), w = dsp[0].size();
  for (int i = 0; i < n; i++) {
    dsp[cy][cx] = s[i];
    if (tp != NULL) attr[cy][cx] = tp;
    if (++cx >= w) {
      cx = 0;
      if (++cy >= h) break;
    }
  }
}
void display_tag(Tag &tag, vector<string> &dsp, vector<vector<Tag *> > &attr,
                 int &cx, int &cy) {
  if (!tag.v) return;
  int h = dsp.size(), w = dsp[0].size();
  if (cy >= h) return;
  if (tag.type == T_STR)
    display_str(tag.name, dsp, attr, cx, cy);
  else if (tag.type == T_BR)
    cx = 0, cy++;
  else if (tag.type == T_LN || tag.type == T_BT)
    display_str(tag.cs[0].name, dsp, attr, cx, cy, &tag);
  else if (tag.type == T_DML || tag.type == T_TAG) {
    for (int i = 0; i < tag.cs.size(); i++) {
      display_tag(tag.cs[i], dsp, attr, cx, cy);
      if (cy >= h) break;
    }
  }
}
void set_tag(Tag &tag, vector<string> &ids, int k, bool v) {
  if (tag.type == T_STR || tag.type == T_SC || tag.type == T_BR) return;
  if (ids[k] == tag.name) k++;
  if (k >= ids.size()) tag.v = v;
  if (tag.type == T_DML || tag.type == T_TAG)
    for (int i = 0; i < tag.cs.size(); i++) set_tag(tag.cs[i], ids, k, v);
}
void toggle_v(Tag &tag, vector<Visible> exp) {
  int n = exp.size();
  bool v = exp[n - 1].v;
  for (int i = n - 2; i >= 0; i--) {
    Visible &vbi = exp[i];
    if (vbi.type == V_NEQ) v = !v;
    set_tag(tag, vbi.ids, 0, v);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string f;
    cin >> f;
    int pi = f.find('.');
    string fb(f.substr(0, pi)), sfx(f.substr(pi + 1));
    if (sfx == "dml") {
      string s;
      getline(cin, s);
      getline(cin, s);
      int pos = 0;
      Tag dml;
      parse_dml(s, pos, dml);
      dmls[fb] = dml;
    } else {
      string s;
      cin >> s;
      int pos = 0;
      map<string, vector<Visible> > ds;
      parse_ds(s, pos, ds);
      dss[fb] = ds;
    }
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int w, h, s;
    string fb;
    cin >> w >> h >> s >> fb;
    Tag dml = dmls[fb];
    dml.setv(true);
    map<string, vector<Visible> > ds;
    read_scripts(dml, ds);
    vector<string> dsp(h, string(w, '.'));
    vector<vector<Tag *> > attr(h, vector<Tag *>(w, (Tag *)NULL));
    int cx = 0, cy = 0;
    display_tag(dml, dsp, attr, cx, cy);
    for (int j = 0; j < s; j++) {
      int x, y;
      cin >> x >> y;
      Tag *tp = attr[y][x];
      if (tp != NULL) {
        if (tp->type == T_LN) {
          dml = dmls[tp->cs[0].name];
          dml.setv(true);
          ds.clear();
          read_scripts(dml, ds);
        } else if (tp->type == T_BT)
          toggle_v(dml, ds[tp->cs[0].name]);
        dsp.assign(h, string(w, '.'));
        attr.assign(h, vector<Tag *>(w, (Tag *)NULL));
        cx = 0, cy = 0;
        display_tag(dml, dsp, attr, cx, cy);
      }
    }
    for (int y = 0; y < h; y++) cout << dsp[y] << endl;
  }
  return 0;
}
