#include <bits/stdc++.h>
using namespace std;
class Widget {
 protected:
  long long _width, _height;
  Widget() : _width(-1), _height(-1) {}
  friend class Box;

 public:
  Widget(long long a, long long b) : _width(a), _height(b){};
  virtual void fix() {}
  long long width() { return _width; }
  long long height() { return _height; }
};
class Box : public Widget {
 protected:
  vector<Widget *> sub_widget;
  long long space, border;
  Box() : Widget(), space(0), border(0) {}

 public:
  virtual void fix() = 0;
  void set_border(long long a) { border = a; }
  void set_space(long long a) { space = a; }
  void pack(Widget *sub) { sub_widget.push_back(sub); }
};
class VBox : public Box {
 public:
  VBox() : Box() {}
  virtual void fix() {
    if (_width != -1 && _height != -1) return;
    _width = 0;
    _height = 0;
    if (!sub_widget.empty()) {
      for (vector<Widget *>::iterator it = sub_widget.begin();
           it < sub_widget.end(); it++) {
        (*it)->fix();
        _height += (*it)->height();
        _width = (_width < (*it)->width()) ? (*it)->width() : _width;
      }
      _height += border * 2 + space * (sub_widget.size() - 1);
      _width += border * 2;
    }
  }
};
class HBox : public Box {
 public:
  HBox() : Box() {}
  virtual void fix() {
    if (_width != -1 && _height != -1) return;
    _width = 0;
    _height = 0;
    if (!sub_widget.empty()) {
      for (vector<Widget *>::iterator it = sub_widget.begin();
           it < sub_widget.end(); it++) {
        (*it)->fix();
        _width += (*it)->width();
        _height = (_height < (*it)->height()) ? (*it)->height() : _height;
      }
      _height += border * 2;
      _width += border * 2 + space * (sub_widget.size() - 1);
    }
  }
};
int main() {
  map<string, Widget *> Map;
  int n = 0;
  string str;
  cin >> n;
  getchar();
  while (n--) {
    getline(cin, str);
    int found;
    found = str.find('.');
    if (found != string::npos) {
      map<string, Widget *>::iterator it, it2;
      int l, r;
      l = str.find_last_of('(');
      r = str.find_last_of(')');
      it = Map.find(str.substr(0, found));
      if (str.compare(found + 1, 4, "pack") == 0) {
        it2 = Map.find(str.substr(l + 1, r - l - 1));
        ((Box *)(it->second))->pack(it2->second);
      }
      if (str.compare(found + 1, 10, "set_border") == 0) {
        long long a;
        istringstream(str.substr(l + 1, r - l - 1)) >> a;
        ((Box *)(it->second))->set_border(a);
      }
      if (str.compare(found + 1, 11, "set_spacing") == 0) {
        long long a;
        ;
        istringstream(str.substr(l + 1, r - l - 1)) >> a;
        ((Box *)(it->second))->set_space(a);
      }
    }
    found = str.find(' ');
    if (found != string::npos) {
      if (str.compare(0, found, "Widget") == 0) {
        int l, r, mid;
        long long a, b;
        l = str.find_last_of('(') + 1;
        r = str.find_last_of(')');
        mid = str.find_last_of(',');
        istringstream(str.substr(l, mid - 1)) >> a;
        istringstream(str.substr(mid + 1, r - mid - 1)) >> b;
        Widget *p = new Widget(a, b);
        Map.insert(make_pair(str.substr(found + 1, l - found - 2), p));
        continue;
      }
      if (str.compare(0, found, "HBox") == 0) {
        HBox *p = new HBox();
        Map.insert(make_pair(str.substr(found + 1, str.size() - found - 1),
                             (Widget *)p));
        continue;
      }
      if (str.compare(0, found, "VBox") == 0) {
        VBox *p = new VBox();
        Map.insert(make_pair(str.substr(found + 1, str.size() - found - 1),
                             (Widget *)p));
        continue;
      }
    }
  }
  for (map<string, Widget *>::iterator it = Map.begin(); it != Map.end();
       it++) {
    ((Box *)(it->second))->fix();
    cout << it->first << ' ' << it->second->width() << ' '
         << it->second->height() << endl;
  }
}
