#include <bits/stdc++.h>
using namespace std;
const int maxn = 200;
const int max_len = 200;
struct widget;
struct Box {
  string name;
  long long x, y;
  vector<Box *> box_inside;
  vector<widget *> widget_inside;
  long long border, space;
  bool type;
  vector<Box *> parent;
  void init(const string s, const bool t) {
    name = s;
    x = y = 0;
    border = space = 0;
    type = t;
  }
  void update();
  void pack(Box *new_parent) {
    parent.push_back(new_parent);
    new_parent->box_inside.push_back(this);
    (*new_parent).update();
  }
  void set_space(const int new_space) {
    space = new_space;
    update();
  }
  void set_border(const int new_border) {
    border = new_border;
    update();
  }
};
struct widget {
  string name;
  long long x, y;
  void pack(Box *new_parent) {
    new_parent->widget_inside.push_back(this);
    (*new_parent).update();
  }
  void init(const string s, const int x1, const int y1) {
    name = s;
    x = x1;
    y = y1;
  }
};
void Box::update() {
  if (type) {
    x = 0;
    y = 0;
    for (size_t i = 0; i < box_inside.size(); ++i) {
      x += box_inside[i]->x;
      y = max(y, box_inside[i]->y);
    }
    for (size_t i = 0; i < widget_inside.size(); ++i) {
      x += widget_inside[i]->x;
      y = max(y, widget_inside[i]->y);
    }
    if (widget_inside.size() + box_inside.size() != 0) {
      x += 2 * border + (box_inside.size() + widget_inside.size() - 1) * space;
      y += 2 * border;
    }
  } else {
    x = 0;
    y = 0;
    for (size_t i = 0; i < box_inside.size(); ++i) {
      y += box_inside[i]->y;
      x = max(x, box_inside[i]->x);
    }
    for (size_t i = 0; i < widget_inside.size(); ++i) {
      y += widget_inside[i]->y;
      x = max(x, widget_inside[i]->x);
    }
    if (widget_inside.size() + box_inside.size() != 0) {
      y += 2 * border + (box_inside.size() + widget_inside.size() - 1) * space;
      x += 2 * border;
    }
  }
  for (size_t i = 0; i < parent.size(); ++i) (*parent[i]).update();
}
struct res {
  string name;
  long long x, y;
};
bool comp(const res a, const res b) {
  if (a.name < b.name)
    return 1;
  else
    return 0;
}
Box ar_of_box[maxn];
widget ar_of_widget[maxn];
int n, last_b, last_w;
char buf[max_len];
string s;
res res_ar[maxn];
int main() {
  last_b = last_w = 0;
  scanf("%d\n", &n);
  for (int i = 0; i < n; ++i) {
    gets(buf);
    s = buf;
    if (s.substr(0, 4) == "VBox")
      ar_of_box[last_b++].init(s.substr(5, s.size() - 5), false);
    if (s.substr(0, 4) == "HBox")
      ar_of_box[last_b++].init(s.substr(5, s.size() - 5), true);
    if (s.substr(0, 6) == "Widget") {
      int x, y;
      x = y = 0;
      s.erase(0, 7);
      string name = "";
      int k = 0;
      while (s[k] != '(') {
        name += s[k];
        ++k;
      }
      ++k;
      while (s[k] >= '0' && s[k] <= '9') {
        x = x * 10 + s[k] - '0';
        ++k;
      }
      ++k;
      while (s[k] >= '0' && s[k] <= '9') {
        y = y * 10 + s[k] - '0';
        ++k;
      }
      ar_of_widget[last_w++].init(name, x, y);
    }
    if (s.find(".pack") != s.npos) {
      string name1, name2;
      name1 = name2 = "";
      int k = 0;
      while (s[k] != '.') {
        name1 += s[k];
        ++k;
      }
      while (s[k] != '(') ++k;
      ++k;
      while (s[k] != ')') {
        name2 += s[k];
        ++k;
      }
      Box *parent = NULL;
      for (int j = 0; j < last_b; ++j)
        if (name1 == ar_of_box[j].name) parent = &ar_of_box[j];
      for (int j = 0; j < last_b; ++j)
        if (name2 == ar_of_box[j].name) ar_of_box[j].pack(parent);
      for (int j = 0; j < last_w; ++j)
        if (name2 == ar_of_widget[j].name) ar_of_widget[j].pack(parent);
    }
    if (s.find(".set_border") != s.npos) {
      string name = "";
      int k = 0;
      int border = 0;
      while (s[k] != '.') {
        name += s[k];
        ++k;
      }
      while (s[k] != '(') ++k;
      ++k;
      while (s[k] >= '0' && s[k] <= '9') {
        border = border * 10 + s[k] - '0';
        ++k;
      }
      for (int j = 0; j < last_b; ++j)
        if (name == ar_of_box[j].name) ar_of_box[j].set_border(border);
    }
    if (s.find(".set_spacing") != s.npos) {
      string name = "";
      int k = 0;
      int space = 0;
      while (s[k] != '.') {
        name += s[k];
        ++k;
      }
      while (s[k] != '(') ++k;
      ++k;
      while (s[k] >= '0' && s[k] <= '9') {
        space = space * 10 + s[k] - '0';
        ++k;
      }
      for (int j = 0; j < last_b; ++j)
        if (name == ar_of_box[j].name) ar_of_box[j].set_space(space);
    }
  }
  for (int i = 0; i < last_b; ++i) {
    res_ar[i].name = ar_of_box[i].name;
    res_ar[i].x = ar_of_box[i].x;
    res_ar[i].y = ar_of_box[i].y;
  }
  for (int i = 0; i < last_w; ++i) {
    res_ar[i + last_b].name = ar_of_widget[i].name;
    res_ar[i + last_b].x = ar_of_widget[i].x;
    res_ar[i + last_b].y = ar_of_widget[i].y;
  }
  sort(&res_ar[0], &res_ar[last_b + last_w], comp);
  for (int i = 0; i < last_b + last_w; ++i)
    printf("%s %I64d %I64d\n", res_ar[i].name.c_str(), res_ar[i].x,
           res_ar[i].y);
  fclose(stdin);
  fclose(stdout);
}
