#include <bits/stdc++.h>
using namespace std;
struct Widget;
struct Box;
map<string, Widget> widgets;
map<string, Box> boxes;
map<string, pair<long long, long long> > result;
struct Widget {
  string name;
  long long x, y;
  Widget(string &nm, long long i, long long j) : name(nm), x(i), y(j) {}
  Widget() {}
  pair<long long, long long> getSize() { return {x, y}; }
};
struct Box {
  string name;
  long long border, spacing;
  bool vert;
  Box(long long border, long long spacing, bool vert)
      : border(border), spacing(spacing), vert(vert) {}
  Box() {}
  vector<string> data;
  pair<long long, long long> getSize() {
    if (result.find(name) != result.end()) return result[name];
    long long x = 0;
    long long y = 0;
    for (const auto &to : data) {
      pair<long long, long long> sz = {0, 0};
      if (widgets.find(to) != widgets.end()) {
        sz = widgets[to].getSize();
      } else if (boxes.find(to) != boxes.end()) {
        sz = boxes[to].getSize();
      }
      if (vert) {
        x = max(x, sz.first);
        y += sz.second;
      } else {
        x += sz.first;
        y = max(y, sz.second);
      }
    }
    pair<long long, long long> ans;
    if (data.size() == 0)
      ans = {0, 0};
    else if (vert) {
      ans = {x + 2 * border,
             y + 2 * border + ((long long)data.size() - 1) * spacing};
    } else
      ans = {x + 2 * border + ((long long)data.size() - 1) * spacing,
             y + 2 * border};
    result[name] = ans;
    return ans;
  }
};
signed main() {
  long long q;
  cin >> q;
  cin.ignore();
  for (long long i = 0; i < q; i++) {
    string s;
    getline(cin, s);
    if (s.substr(0, 6) == "Widget") {
      long long f = s.find('('), m = s.find(',');
      string name = s.substr(7, s.find('(') - 7);
      string x = s.substr(f + 1, m - f - 1);
      string y = s.substr(m + 1);
      y.pop_back();
      widgets[name] = Widget(name, atoll(x.c_str()), atoll(y.c_str()));
    } else if (s.substr(0, 4) == "VBox") {
      string name = s.substr(5);
      boxes[name].border = boxes[name].spacing = 0;
      boxes[name].name = name;
      boxes[name].vert = true;
    } else if (s.substr(0, 4) == "HBox") {
      string name = s.substr(5);
      boxes[name].border = boxes[name].spacing = 0;
      boxes[name].name = name;
      boxes[name].vert = false;
    } else {
      long long f = s.find('.'), m = s.find('(');
      string fname = s.substr(0, f);
      string func = s.substr(f + 1, m - f - 1);
      string param = s.substr(m + 1);
      param.pop_back();
      if (func == "pack") {
        boxes[fname].data.push_back(param);
      } else if (func == "set_border") {
        boxes[fname].border = atoll(param.c_str());
      } else if (func == "set_spacing") {
        boxes[fname].spacing = atoll(param.c_str());
      }
    }
  }
  for (auto to : boxes) {
    to.second.getSize();
  }
  for (auto to : widgets) {
    result[to.first] = to.second.getSize();
  }
  for (auto to : result) {
    cout << to.first << ' ' << to.second.first << ' ' << to.second.second
         << endl;
  }
  return 0;
}
