#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long INF = (long long)1e18;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int mod = (int)1e9 + 7;
struct widget {
  string type, name;
  long long w, h, spacing, border;
  vector<int> children;
  widget() { w = 0, h = 0, spacing = 0, border = 0; }
};
int n;
map<string, int> m;
vector<widget> widgets;
int u[110];
vector<pair<string, pair<long long, long long> > > ans;
void dfs(int ver) {
  if (widgets[ver].type == "Widget") return;
  u[ver] = 1;
  long long real_w = 0, real_h = 0;
  widget par = widgets[ver];
  for (int i = 0; i < (int)widgets[ver].children.size(); ++i) {
    int id_child = widgets[ver].children[i];
    if (!u[id_child]) {
      dfs(id_child);
    }
    if (par.type == "VBox") {
      real_h += widgets[id_child].h;
      real_w = max(real_w, widgets[id_child].w);
    } else {
      real_w += widgets[id_child].w;
      real_h = max(real_h, widgets[id_child].h);
    }
  }
  long long child_cnt = (int)widgets[ver].children.size();
  if (par.type == "VBox") {
    widgets[ver].w =
        max(widgets[ver].w, real_w + 2 * widgets[ver].border * (child_cnt > 0));
    widgets[ver].h = max(
        widgets[ver].h, (child_cnt <= 1 ? 0 : (child_cnt - 1)) *
                                widgets[ver].spacing * (child_cnt > 0) +
                            2 * widgets[ver].border * (child_cnt > 0) + real_h);
  } else if (par.type == "HBox") {
    widgets[ver].h =
        max(widgets[ver].h, real_h + 2 * widgets[ver].border * (child_cnt > 0));
    widgets[ver].w = max(
        widgets[ver].w, (child_cnt <= 1 ? 0 : (child_cnt - 1)) *
                                widgets[ver].spacing * (child_cnt > 0) +
                            2 * widgets[ver].border * (child_cnt > 0) + real_w);
  }
}
int main() {
  cin >> n;
  long long cur_id = 0;
  for (int i = 0; i < n; ++i) {
    string second;
    cin >> second;
    if (second == "Widget") {
      string cur, t = "";
      cin >> cur;
      long long w = 0, h = 0, wait = 0;
      for (int j = 0; j < (int)cur.size(); ++j) {
        if (cur[j] == '(') {
          wait = 1;
          continue;
        }
        if (wait == 0)
          t += cur[j];
        else if ('0' <= cur[j] && cur[j] <= '9') {
          if (wait == 1) {
            w *= 10;
            w += cur[j] - '0';
          } else if (wait == 2) {
            h *= 10;
            h += cur[j] - '0';
          }
        } else if (cur[j] == ',')
          wait = 2;
      }
      widget wid = widget();
      wid.w = w, wid.h = h, wid.type = "Widget", wid.name = t;
      widgets.push_back(wid);
      m[t] = (int)widgets.size() - 1;
    } else if (second == "VBox") {
      string name;
      cin >> name;
      widget wid = widget();
      wid.type = "VBox", wid.name = name;
      widgets.push_back(wid);
      m[name] = (int)widgets.size() - 1;
    } else if (second == "HBox") {
      string name;
      cin >> name;
      widget wid = widget();
      wid.type = "HBox", wid.name = name;
      widgets.push_back(wid);
      m[name] = (int)widgets.size() - 1;
    } else {
      string name = "", child_name = "", command = "";
      long long border = 0, spacing = 0;
      long long wait = 0;
      for (int j = 0; j < (int)second.size(); ++j) {
        if (second[j] == ')') continue;
        if (second[j] == '.') {
          wait = 1;
          continue;
        }
        if (second[j] == '(') {
          wait = 2;
          continue;
        }
        if (wait == 0) {
          name += second[j];
        } else if (wait == 1) {
          command += second[j];
        } else {
          if (command == "pack") {
            child_name += second[j];
          } else if (command == "set_border") {
            border *= 10;
            border += second[j] - '0';
          } else {
            spacing *= 10;
            spacing += second[j] - '0';
          }
        }
      }
      if (command == "pack") {
        int id_par = m[name];
        int id_ch = m[child_name];
        widgets[id_par].children.push_back(id_ch);
      } else if (command == "set_border") {
        int id_par = m[name];
        widgets[id_par].border = border;
      } else {
        int id_par = m[name];
        widgets[id_par].spacing = spacing;
      }
    }
  }
  for (int i = 0; i < (int)widgets.size(); ++i) {
    if (!u[i]) {
      dfs(i);
    }
  }
  for (int i = 0; i < (int)widgets.size(); ++i) {
    ans.push_back(
        make_pair(widgets[i].name, make_pair(widgets[i].w, widgets[i].h)));
  }
  sort(ans.begin(), ans.end());
  for (int i = 0; i < (int)ans.size(); ++i) {
    cout << ans[i].first << " " << ans[i].second.first << " "
         << ans[i].second.second << "\n";
  }
  return 0;
}
