#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int n;
struct Widget {
  long long x, y;
  int type;
  long long border, spacing;
  vector<Widget *> a;
  bool calced;
  Widget() {
    calced = false;
    border = spacing = 0;
    x = y = 0;
  }
  void calc() {
    if (type == -1) return;
    if (calced) return;
    calced = true;
    for (int i = 0, _n = ((int)(a).size()); i < _n; i++) a[i]->calc();
    x = 0, y = 0;
    if ((int)(a).size() == 0) return;
    if (type == 0) {
      for (int i = 0, _n = ((int)(a).size()); i < _n; i++) {
        x += a[i]->x;
        y = max(y, a[i]->y);
      }
      x += ((int)(a).size() - 1) * spacing;
    } else {
      for (int i = 0, _n = ((int)(a).size()); i < _n; i++) {
        y += a[i]->y;
        x = max(x, a[i]->x);
      }
      y += ((int)(a).size() - 1) * spacing;
    }
    x += 2 * border;
    y += 2 * border;
  }
};
char buf[10240];
map<string, Widget *> a;
int main() {
  gets(buf);
  sscanf(buf, "%d", &n);
  for (int i = 0, _n = (n); i < _n; i++) {
    gets(buf);
    char cmd[1024], w[1024], arg[1024];
    sscanf(buf, "%s", cmd);
    if (strchr(buf, '.') != 0) {
      for (int i = 0, _n = (strlen(buf)); i < _n; i++)
        if (buf[i] == '.' || buf[i] == '(' || buf[i] == ')' || buf[i] == ',')
          buf[i] = ' ';
      sscanf(buf, "%s %s %s", w, cmd, arg);
      if (a[string(w)]->type == -1) printf("BAD");
      if (strcmp(cmd, "pack") == 0) {
        a[string(w)]->a.push_back(a[string(arg)]);
      } else {
        int x;
        sscanf(arg, "%d", &x);
        if (strcmp(cmd, "set_border") == 0)
          a[string(w)]->border = x;
        else if (strcmp(cmd, "set_spacing") == 0)
          a[string(w)]->spacing = x;
        else
          printf("BAD!");
      }
    } else {
      for (int i = 0, _n = (strlen(buf)); i < _n; i++)
        if (buf[i] == '.' || buf[i] == '(' || buf[i] == ')' || buf[i] == ',')
          buf[i] = ' ';
      Widget *q = new Widget();
      sscanf(buf, "%s %s", cmd, w);
      if (strcmp(cmd, "Widget") == 0) {
        q->type = -1;
        sscanf(buf, "%s %s %lld %lld", cmd, w, &q->x, &q->y);
      } else if (strcmp(cmd, "HBox") == 0)
        q->type = 0;
      else if (strcmp(cmd, "VBox") == 0)
        q->type = 1;
      else
        printf("NOTGOOD!");
      a[string(w)] = q;
    }
  }
  for (map<string, Widget *>::iterator it = a.begin(); it != a.end(); ++it) {
    it->second->calc();
    cout << it->first << " " << it->second->x << " " << it->second->y << endl;
  }
  return 0;
}
