#include <bits/stdc++.h>
using namespace std;
class Widgets {
 public:
  static const int VBOX = 1, HBOX = 2, OTHER = 3;
  string name;
  long long width, high;
  int border, spacing;
  int type;
  bool wUpdated, hUpdated;
  vector<Widgets *> component;
  Widgets(string s, int t, int w = 0, int h = 0) {
    name = s;
    type = t;
    width = w, high = h;
    border = spacing = 0;
    component.clear();
    wUpdated = hUpdated = false;
  }
  long long getWidth() {
    if (type == OTHER || wUpdated) return width;
    if (component.empty())
      return 0;
    else if (type == HBOX) {
      long long t = 0;
      for (int i = 0; i < int(component.size()); ++i) {
        if (i) t += spacing;
        t += component[i]->getWidth();
      }
      wUpdated = true;
      return width = t + 2 * border;
    } else {
      long long t = -1;
      for (int i = 0; i < int(component.size()); ++i) {
        t = max(t, component[i]->getWidth());
      }
      wUpdated = true;
      return width = t + 2 * border;
    }
  }
  long long getHigh() {
    if (type == OTHER || hUpdated) return high;
    if (component.empty())
      return 0;
    else if (type == VBOX) {
      long long t = 0;
      for (int i = 0; i < int(component.size()); ++i) {
        if (i) t += spacing;
        t += component[i]->getHigh();
      }
      hUpdated = true;
      return high = t + 2 * border;
    } else {
      long long t = -1;
      for (int i = 0; i < int(component.size()); ++i) {
        t = max(t, component[i]->getHigh());
      }
      hUpdated = true;
      return high = t + 2 * border;
    }
  }
};
vector<Widgets *> widgets;
Widgets *getWidgets(string name) {
  for (int i = 0; i < int(widgets.size()); ++i)
    if (widgets[i]->name == name) return widgets[i];
  printf("error!! find name: %s \n", name.c_str());
}
bool cmp(Widgets *a, Widgets *b) { return a->name < b->name; }
void go(string s) {
  char name[20] = {0};
  if (s.find(".") == string::npos) {
    if (s == "Widget") {
      int width, high;
      scanf("%*[^a-z]%[a-z](%d,%d)", name, &width, &high);
      widgets.push_back(new Widgets(name, Widgets::OTHER, width, high));
    } else {
      cin >> name;
      if (s == "HBox")
        widgets.push_back(new Widgets(name, Widgets::HBOX));
      else
        widgets.push_back(new Widgets(name, Widgets::VBOX));
    }
  } else {
    char opr[20], name2[20];
    sscanf(s.c_str(), "%[a-z].%[^(](%[^)])", name, opr, name2);
    s = opr;
    if (s == "pack") {
      Widgets *father, *child;
      father = getWidgets(name);
      child = getWidgets(name2);
      father->component.push_back(child);
    } else {
      Widgets *ins = getWidgets(name);
      int t;
      sscanf(name2, "%d", &t);
      if (s == "set_border")
        ins->border = t;
      else
        ins->spacing = t;
    }
  }
}
int main() {
  int n;
  string ins;
  cin >> n;
  while (n--) {
    cin >> ins;
    go(ins);
  }
  sort(widgets.begin(), widgets.end(), cmp);
  for (int i = 0; i < int(widgets.size()); ++i)
    printf("%s %I64d %I64d\n", widgets[i]->name.c_str(), widgets[i]->getWidth(),
           widgets[i]->getHigh());
  return 0;
}
