#include <bits/stdc++.h>
using namespace std;
bool is_breaker(char c) {
  if (!c) return true;
  char s[2] = {c, 0};
  return strstr("\n\r\t .,()", s) != 0;
}
vector<string> tokenize(const char *s) {
  string cur = "";
  vector<string> res;
  for (int i = 0;; i++) {
    if (is_breaker(s[i])) {
      if (cur.length()) res.push_back(cur);
      cur = "";
    } else
      cur += s[i];
    if (!s[i]) break;
  }
  return res;
}
int pint(const string &s) {
  int res = 0;
  for (int i = 0; i < s.length(); i++) {
    assert('0' <= s[i] && s[i] <= '9');
    res = res * 10 + s[i] - '0';
  }
  return res;
}
enum WidgetType { BOX, HBOX, VBOX };
class Widget {
  long long w, h;
  long long border, spacing;
  vector<Widget *> childs;
  WidgetType ty;
  bool calced;

 public:
  Widget(WidgetType _ty)
      : w(-1), h(-1), border(0), spacing(0), ty(_ty), calced(false) {}
  Widget(long long _w, long long _h) : w(_w), h(_h), ty(BOX), calced(true) {}
  void calc() {
    if (calced) return;
    assert(ty != BOX);
    if (!((int)(childs).size())) {
      w = h = 0;
      return;
    }
    w = 0;
    h = 0;
    for (int i = 0; i < ((int)(childs).size()); i++) {
      childs[i]->calc();
      if (ty == HBOX) {
        w += childs[i]->width();
        h = max(h, childs[i]->height());
      } else {
        w = max(w, childs[i]->width());
        h += childs[i]->height();
      }
    }
    int add_w = (((int)(childs).size()) - 1) * spacing + 2 * border;
    int add_h = 2 * border;
    if (ty == VBOX) swap(add_w, add_h);
    w += add_w;
    h += add_h;
    calced = true;
  }
  void addChild(Widget *w) {
    assert(ty != BOX);
    childs.push_back(w);
  }
  long long width() { return w; }
  long long height() { return h; }
  void setSpacing(int _s) { spacing = _s; }
  void setBorder(int _b) { border = _b; }
};
char buf[65536];
int main() {
  int n;
  while (scanf("%d ", &n) >= 1) {
    map<string, int> names;
    vector<Widget> ws;
    ws.reserve(n);
    for (int i = 0; i < n; i++) {
      fgets(buf, sizeof buf, stdin);
      vector<string> tks = tokenize(buf);
      if (tks[0] == "Widget") {
        assert(!names.count(tks[1]));
        names[tks[1]] = ((int)(ws).size());
        ws.push_back(Widget(pint(tks[2]), pint(tks[3])));
      } else if (tks[0] == "HBox" || tks[0] == "VBox") {
        assert(!names.count(tks[1]));
        names[tks[1]] = ((int)(ws).size());
        ws.push_back(Widget(tks[0] == "HBox" ? HBOX : VBOX));
      } else if (tks[1] == "pack") {
        int where = names[tks[0]];
        int who = names[tks[2]];
        assert(0 <= where && where < ((int)(ws).size()));
        assert(0 <= who && who < ((int)(ws).size()));
        ws[where].addChild(&ws[who]);
      } else if (tks[1] == "set_border") {
        ws[names[tks[0]]].setBorder(pint(tks[2]));
      } else if (tks[1] == "set_spacing") {
        ws[names[tks[0]]].setSpacing(pint(tks[2]));
      } else
        assert(false);
    }
    for (map<string, int>::iterator it = names.begin(); it != names.end();
         it++) {
      Widget *w = &ws[it->second];
      w->calc();
      printf("%s %I64d %I64d\n", it->first.c_str(), w->width(), w->height());
    }
    break;
  }
  return 0;
}
