#include <bits/stdc++.h>
using namespace std;
struct cong {
  long long height, width;
  long long border, spacing;
  int type;
  vector<int> son;
  string name;
  bool check;
  void clr() {
    check = false;
    height = width = border = spacing = 0;
    type = 0;
    son.clear();
    name = "";
  }
};
vector<cong> s;
map<string, int> ma;
map<string, pair<long long, long long> > ans;
char cmd[10000];
char name[111];
char name1[111], name2[111];
char type[111];
void cal(cong &c) {
  if (c.check) return;
  c.check = true;
  long long x, y;
  if (c.type == 2) {
    x = y = 0;
    for (int i = 0; i < c.son.size(); i++) {
      cong &son = s[c.son[i]];
      cal(son);
      y += son.height;
      x = max(x, son.width);
    }
    if (c.son.size()) {
      y += c.spacing * (c.son.size() - 1);
      y += c.border * 2;
      x += c.border * 2;
    }
    c.height = y;
    c.width = x;
  } else if (c.type == 3) {
    x = y = 0;
    for (int i = 0; i < c.son.size(); i++) {
      cong &son = s[c.son[i]];
      cal(son);
      x += son.width;
      y = max(y, son.height);
    }
    if (c.son.size()) {
      x += c.spacing * (c.son.size() - 1);
      x += c.border * 2;
      y += c.border * 2;
    }
    c.height = y;
    c.width = x;
  }
}
int main() {
  int n;
  scanf("%d\n", &n);
  while (n--) {
    gets(cmd);
    if (strstr(cmd, ".")) {
      int num;
      sscanf(cmd, "%[^.].%[^(]%n", name1, type, &num);
      char *p = cmd + num;
      int na = ma[name1];
      if (!strcmp(type, "set_border")) {
        int tm;
        sscanf(p, "(%d", &tm);
        s[na].border = tm;
      } else if (!strcmp(type, "set_spacing")) {
        int tm;
        sscanf(p, "(%d", &tm);
        s[na].spacing = tm;
      } else {
        sscanf(p, "(%[^)]", name2);
        int na2 = ma[name2];
        s[na].son.push_back(na2);
      }
    } else {
      cong tm;
      tm.clr();
      sscanf(cmd, "%s %[^(](%I64d,%I64d)", type, name, &tm.width, &tm.height);
      if (!strcmp(type, "Widget")) {
        tm.type = 1;
      } else if (!strcmp(type, "VBox")) {
        tm.type = 2;
      } else if (!strcmp(type, "HBox")) {
        tm.type = 3;
      }
      tm.name = name;
      s.push_back(tm);
      ma[tm.name] = s.size() - 1;
    }
  }
  for (int i = 0; i < s.size(); i++) {
    cal(s[i]);
    ans[s[i].name] = make_pair(s[i].width, s[i].height);
  }
  map<string, pair<long long, long long> >::iterator iter;
  for (iter = ans.begin(); iter != ans.end(); iter++) {
    cout << iter->first << " " << iter->second.first << " "
         << iter->second.second << endl;
  }
  return 0;
}
