#include <bits/stdc++.h>
using namespace std;
class Widget;
map<string, Widget> wigs;
class Widget {
 public:
  long long width;
  long long height;
  long long spacing;
  long long border;
  bool isHor;
  bool isVer;
  vector<string> widgets;
  long long getHeight() {
    if (!isHor && !isVer) return height;
    if (height > 0) return height;
    long long resy = 0;
    if (isHor) {
      for (int i = 0; i < widgets.size(); i++) {
        resy = max(resy, wigs[widgets[i]].getHeight());
      }
    }
    if (isVer) {
      for (int i = 0; i < widgets.size(); i++)
        resy += wigs[widgets[i]].getHeight();
      if (widgets.size() > 0) resy += (widgets.size() - 1) * spacing;
    }
    if (widgets.size() > 0) resy += border * 2;
    height = resy;
    return resy;
  }
  long long getWidth() {
    if (!isHor && !isVer) return width;
    if (width > 0) return width;
    long long resx = 0;
    if (isVer) {
      for (int i = 0; i < widgets.size(); i++)
        resx = max(resx, wigs[widgets[i]].getWidth());
    }
    if (isHor) {
      for (int i = 0; i < widgets.size(); i++)
        resx += wigs[widgets[i]].getWidth();
      if (widgets.size() > 0) resx += (widgets.size() - 1) * spacing;
    }
    if (widgets.size() > 0) resx += border * 2;
    width = resx;
    return resx;
  }
  void pack(string name) { widgets.push_back(name); }
  Widget() {
    width = 0;
    height = 0;
    spacing = 0;
    border = 0;
    isHor = false;
    isVer = false;
  }
  void setSpacing(int i) { spacing = i; }
  void setBorder(int i) { border = i; }
};
using namespace std;
int main() {
  int n;
  cin >> n;
  string str, str2;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == "Widget") {
      cin >> str2;
      string name = str2.substr(0, str2.find("("));
      Widget wig;
      wig.width = atoi(
          str2.substr(str2.find("(") + 1, str2.find(",") - 1 - str2.find("("))
              .c_str());
      wig.height = atoi(
          str2.substr(str2.find(",") + 1, str2.find(")") - 1 - str2.find(","))
              .c_str());
      wigs[name] = wig;
    } else if (str == "HBox") {
      cin >> str2;
      Widget hb;
      hb.isHor = true;
      wigs[str2] = hb;
    } else if (str == "VBox") {
      cin >> str2;
      Widget vb;
      vb.isVer = true;
      wigs[str2] = vb;
    } else {
      string name = str.substr(0, str.find("."));
      string command =
          str.substr(str.find(".") + 1, str.find("(") - str.find(".") - 1);
      string rest =
          str.substr(str.find("(") + 1, str.find(")") - str.find("(") - 1);
      if (command == "pack") {
        wigs[name].pack(rest);
      } else if (command == "set_border") {
        wigs[name].border = atoi(rest.c_str());
      } else if (command == "set_spacing") {
        wigs[name].spacing = atoi(rest.c_str());
      }
    }
  }
  for (typeof(wigs.begin()) it = wigs.begin(); it != wigs.end(); ++it) {
    cout << it->first << " " << (it->second).getWidth() << " "
         << it->second.getHeight();
    cout << endl;
  }
  return 0;
}
