#include <bits/stdc++.h>
using namespace std;
struct Container {
  vector<long long> sons;
  long long border;
  long long spacing;
  long long type;
  long long x, y;
  string name;
};
map<string, long long> mm;
vector<Container> con;
char cmd[10000];
map<string, pair<long long, long long> > ans;
void calc(Container &c) {
  if (c.x != -1 && c.y != -1) return;
  if (c.type == 1) {
    long long sy = 0, mx = 0;
    for (long long i = 0; i < c.sons.size(); ++i) {
      Container &son = con[c.sons[i]];
      calc(son);
      sy += son.y;
      mx = max(mx, son.x);
    }
    if (c.sons.size()) sy += c.spacing * (c.sons.size() - 1);
    if (c.sons.size()) mx += c.border * 2;
    if (c.sons.size()) sy += c.border * 2;
    c.x = mx;
    c.y = sy;
  } else if (c.type == 2) {
    long long my = 0, sx = 0;
    for (long long i = 0; i < c.sons.size(); ++i) {
      Container &son = con[c.sons[i]];
      calc(son);
      sx += son.x;
      my = max(my, son.y);
    }
    if (c.sons.size()) sx += c.spacing * (c.sons.size() - 1);
    if (c.sons.size()) my += c.border * 2;
    if (c.sons.size()) sx += c.border * 2;
    c.y = my;
    c.x = sx;
  } else {
    cout << "ERROR" << endl;
  }
}
int main() {
  long long n;
  mm.clear();
  while (cin >> n) {
    gets(cmd);
    for (long long i = 0; i < n; ++i) {
      gets(cmd);
      char type[200], name[200];
      if (strstr(cmd, ".")) {
        int n;
        sscanf(cmd, "%[^.].%[^(]%n", name, type, &n);
        char *p = cmd + n;
        Container c;
        c.name = name;
        long long ind = mm[name];
        if (strcmp(type, "set_border") == 0) {
          long long x;
          sscanf(p, "(%I64d", &x);
          con[ind].border = x;
          continue;
        }
        if (strcmp(type, "set_spacing") == 0) {
          long long x;
          sscanf(p, "(%I64d", &x);
          con[ind].spacing = x;
          continue;
        }
        if (strcmp(type, "pack") == 0) {
          char name2[200];
          sscanf(p, "(%[^)]", name2);
          long long ind2 = mm[name2];
          con[ind].sons.push_back(ind2);
          continue;
        }
        cout << "ERROR" << endl;
      } else {
        Container c;
        c.border = 0;
        c.spacing = 0;
        sscanf(cmd, "%s %[^(](%I64d,%I64d)", type, name, &c.x, &c.y);
        c.type = -1;
        if (strstr(cmd, "Widget")) {
          c.type = 3;
        } else if (strstr(cmd, "VBox")) {
          c.type = 1;
          c.x = c.y = -1;
        } else if (strstr(cmd, "HBox")) {
          c.type = 2;
          c.x = c.y = -1;
        }
        c.name = name;
        con.push_back(c);
        mm[c.name] = con.size() - 1;
      }
    }
    ans.clear();
    for (long long i = 0; i < con.size(); ++i) {
      calc(con[i]);
      ans[con[i].name] = make_pair(con[i].x, con[i].y);
    }
    for (map<string, pair<long long, long long> >::iterator iter = ans.begin();
         iter != ans.end(); ++iter) {
      cout << iter->first << " " << iter->second.first << " "
           << iter->second.second << endl;
    }
  }
}
