#include <bits/stdc++.h>
using namespace std;
const int MAXN = 111;
enum { HBox, VBox, Widget };
map<string, int> mp;
int idx;
struct data {
  vector<int> son;
  vector<int> fa;
  int type;
  long long h, w;
  long long border, spacing;
  void calc();
  void init() {
    border = spacing = 0;
    fa.clear();
    son.clear();
  }
} d[MAXN];
void data::calc() {
  if (type == HBox) {
    h = w = 0;
    if (son.size() == 0) return;
    for (int i = 0; i < son.size(); i++) {
      w += d[son[i]].w;
      h = max(h, d[son[i]].h);
    }
    h += (long long)2 * border;
    w += (long long)(son.size() - 1) * spacing + 2 * border;
  } else if (type == VBox) {
    h = w = 0;
    if (son.size() == 0) return;
    for (int i = 0; i < son.size(); i++) {
      w = max(w, d[son[i]].w);
      h += d[son[i]].h;
    }
    h += (long long)(son.size() - 1) * spacing + 2 * border;
    w += (long long)2 * border;
  }
}
void adjust(int x) {
  d[x].calc();
  if (d[x].fa.size() == 0) return;
  for (int i = 0; i < d[x].fa.size(); i++) {
    adjust(d[x].fa[i]);
  }
}
char cmd[1111], name[1111], buf[1111];
int x, y;
int main() {
  int n;
  scanf("%d", &n);
  idx = 0;
  for (int i = 0; i < n; i++) {
    gets(buf);
    scanf("%s", cmd);
    if (strcmp(cmd, "Widget") == 0) {
      scanf("%s", buf);
      for (int i = 0; buf[i]; i++) {
        if (buf[i] == '(' || buf[i] == ',' || buf[i] == ')') buf[i] = ' ';
      }
      sscanf(buf, "%s%d%d", name, &x, &y);
      mp[name] = idx;
      d[idx].w = x;
      d[idx].h = y;
      d[idx].type = Widget;
      d[idx].init();
      d[idx].calc();
      idx++;
    } else if (strcmp(cmd, "VBox") == 0) {
      scanf("%s", name);
      mp[name] = idx;
      d[idx].w = 0;
      d[idx].h = 0;
      d[idx].type = VBox;
      d[idx].init();
      d[idx].calc();
      idx++;
    } else if (strcmp(cmd, "HBox") == 0) {
      scanf("%s", name);
      mp[name] = idx;
      d[idx].w = 0;
      d[idx].h = 0;
      d[idx].type = HBox;
      d[idx].init();
      d[idx].calc();
      idx++;
    } else {
      strcpy(buf, cmd);
      for (int i = 0; buf[i]; i++) {
        if (buf[i] == '.' || buf[i] == '(' || buf[i] == ')') {
          buf[i] = ' ';
        }
      }
      sscanf(buf, "%s %s", name, cmd);
      if (strcmp(cmd, "set_border") == 0) {
        sscanf(buf, "%*s %*s %d", &x);
        int id = mp[name];
        d[id].border = x;
        d[id].calc();
        adjust(id);
      } else if (strcmp(cmd, "set_spacing") == 0) {
        sscanf(buf, "%*s %*s %d", &x);
        int id = mp[name];
        d[id].spacing = x;
        d[id].calc();
        adjust(id);
      } else if (strcmp(cmd, "pack") == 0) {
        sscanf(buf, "%*s %*s %s", cmd);
        int id1 = mp[name];
        int id2 = mp[cmd];
        d[id2].fa.push_back(id1);
        d[id1].son.push_back(id2);
        adjust(id2);
      }
    }
  }
  for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) {
    d[it->second].calc();
    printf("%s %I64d %I64d\n", (it->first).c_str(), d[it->second].w,
           d[it->second].h);
  }
}
