#include <bits/stdc++.h>
using namespace std;
const int WIDGET = 0;
const int VBOX = 1;
const int HBOX = 2;
const int MAXN = 105;
struct Widget {
  long long int h, w;
  int border;
  int spacing;
  string name;
  int type;
  vector<int> packed_list;
  Widget(string name, int type) : name(name), type(type) {
    h = 0;
    w = 0;
    border = 0;
    spacing = 0;
  }
  Widget(string name, int type, long long int w, long long int h)
      : name(name), type(type), w(w), h(h) {
    border = 0;
    spacing = 0;
  }
  void set_border(int border) {
    this->border = border;
    return;
  }
  void set_spacing(int spacing) {
    this->spacing = spacing;
    return;
  }
  void pack(int x) {
    packed_list.push_back(x);
    return;
  }
};
vector<Widget> widgets;
map<string, int> mapa;
int used[MAXN];
bool operator<(const Widget& a, const Widget& b) { return a.name < b.name; }
void DFS(int node) {
  used[node] = 1;
  if (widgets[node].type == WIDGET) {
    return;
  }
  long long int height = 0;
  long long int width = 0;
  long long int maxi_height = 0;
  long long int maxi_width = 0;
  int sz = (int)widgets[node].packed_list.size();
  for (int i = 0; i < sz; i++) {
    if (!used[widgets[node].packed_list[i]]) {
      DFS(widgets[node].packed_list[i]);
    }
    height += widgets[widgets[node].packed_list[i]].h;
    width += widgets[widgets[node].packed_list[i]].w;
    maxi_height = max(maxi_height, widgets[widgets[node].packed_list[i]].h);
    maxi_width = max(maxi_width, widgets[widgets[node].packed_list[i]].w);
  }
  if (widgets[node].type == VBOX) {
    widgets[node].h = height + max(0, sz - 1) * widgets[node].spacing +
                      (sz > 0 ? 2 * widgets[node].border : 0);
    widgets[node].w = maxi_width + (sz > 0 ? 2 * widgets[node].border : 0);
  } else {
    widgets[node].h = maxi_height + (sz > 0 ? 2 * widgets[node].border : 0);
    widgets[node].w = width + max(0, sz - 1) * widgets[node].spacing +
                      (sz > 0 ? 2 * widgets[node].border : 0);
  }
  return;
}
int main(void) {
  int n;
  string s;
  int next = 0;
  long long int width;
  long long int height;
  scanf(" %d", &n);
  while (n--) {
    cin >> s;
    char name[30];
    if (s == "Widget") {
      scanf(" %s", name);
      for (int i = 0; name[i] != '\0'; i++) {
        if (name[i] == '(') {
          sscanf(name + i, "(%lld,%lld)", &width, &height);
          name[i] = '\0';
          break;
        }
      }
      mapa[string(name)] = next++;
      widgets.push_back(Widget(name, WIDGET, width, height));
    } else if (s == "VBox") {
      scanf(" %s", name);
      mapa[string(name)] = next++;
      widgets.push_back(Widget(name, VBOX));
    } else if (s == "HBox") {
      scanf(" %s", name);
      mapa[string(name)] = next++;
      widgets.push_back(Widget(name, HBOX));
    } else {
      string parent;
      string packed;
      string op;
      for (int i = 0; i < (int)s.size(); i++) {
        if (s[i] == '.') {
          parent = s.substr(0, i);
        } else if (s[i] == '(') {
          op = s.substr(parent.size() + 1, i - parent.size() - 1);
          packed = s.substr(i + 1, s.size() - i - 2);
          break;
        }
      }
      int id_parent = mapa[parent];
      if (string(op) == "pack") {
        int idx = mapa[packed];
        widgets[id_parent].pack(idx);
      } else {
        int atrib;
        sscanf(packed.c_str(), "%d", &atrib);
        if (string(op) == "set_border") {
          widgets[id_parent].set_border(atrib);
        } else {
          widgets[id_parent].set_spacing(atrib);
        }
      }
    }
  }
  for (int i = 0; i < (int)widgets.size(); i++) {
    if (!used[i]) {
      DFS(i);
    }
  }
  sort(widgets.begin(), widgets.end());
  for (int i = 0; i < (int)widgets.size(); i++) {
    cout << widgets[i].name << " " << widgets[i].w << " " << widgets[i].h
         << '\n';
  }
  return 0;
}
