#include <bits/stdc++.h>
using namespace std;
int n;
class Widget {
 public:
  long long width, height;
  int type, space, boarder;
  Widget() : type(0), width(0), height(0), space(0), boarder(0){};
  vector<string> subWid;
  Widget(int _type, int _width = 0, int _height = 0)
      : type(_type), width(_width), height(_height), space(0), boarder(0){};
};
map<string, Widget> rec;
void goRun(Widget &wid, string name) {
  if (wid.type == -1) return;
  int curT = wid.type;
  wid.type = -1;
  if (curT == 0) return;
  for (int i = 0; i < (int)wid.subWid.size(); ++i) {
    Widget &it = rec[wid.subWid[i]];
    goRun(it, wid.subWid[i]);
    if (curT == 1) {
      wid.height = max(wid.height, it.height + wid.boarder * 2);
      wid.width += it.width;
    }
    if (curT == 2) {
      wid.width = max(wid.width, it.width + wid.boarder * 2);
      wid.height += it.height;
    }
  }
  if ((int)wid.subWid.size() == 0) return;
  if (curT == 1)
    wid.width += wid.boarder * 2 + (wid.subWid.size() - 1) * wid.space;
  if (curT == 2)
    wid.height += wid.boarder * 2 + (wid.subWid.size() - 1) * wid.space;
}
char str[1000];
int main() {
  scanf("%d", &n);
  char ch = getchar();
  while (gets(str) != NULL) {
    vector<string> s;
    for (char *p = strtok(str, " .,()"); p; p = strtok(NULL, " .,()")) {
      s.push_back(p);
    }
    if (s[0] == "Widget")
      rec[s[1]] = Widget(0, atoi(s[2].c_str()), atoi(s[3].c_str()));
    else if (s[0] == "HBox")
      rec[s[1]] = Widget(1);
    else if (s[0] == "VBox")
      rec[s[1]] = Widget(2);
    else if (s[1] == "pack")
      rec[s[0]].subWid.push_back(s[2]);
    else if (s[1] == "set_border")
      rec[s[0]].boarder = atoi(s[2].c_str());
    else if (s[1] == "set_spacing")
      rec[s[0]].space = atoi(s[2].c_str());
  }
  for (map<string, Widget>::iterator pos = rec.begin(); pos != rec.end();
       ++pos) {
    goRun(pos->second, pos->first);
    printf("%s %I64d %I64d\n", pos->first.c_str(), pos->second.width,
           pos->second.height);
  }
  getchar();
  getchar();
  return 0;
}
