#include <bits/stdc++.h>
using namespace std;
vector<string> tokenize(const std::string &str,
                        const std::string &delimiters = " ",
                        const bool trimEmpty = false) {
  vector<string> tokens;
  std::string::size_type pos, lastPos = 0;
  while (true) {
    pos = str.find_first_of(delimiters, lastPos);
    if (pos == std::string::npos) {
      pos = str.length();
      if (pos != lastPos || !trimEmpty)
        tokens.push_back(string(str.data() + lastPos, pos - lastPos));
      break;
    } else {
      if (pos != lastPos || !trimEmpty)
        tokens.push_back(string(str.data() + lastPos, pos - lastPos));
    }
    lastPos = pos + 1;
  }
  return tokens;
};
static const string WidgetName = "Widget";
static const string HBoxName = "HBox";
static const string VBoxName = "VBox";
class Widget;
vector<Widget *> GC;
map<string, Widget *> storage;
set<string> was;
class Widget {
  static int counter;

 public:
  long long width, height, border, spacing;
  const int id;
  string name;
  Widget()
      : width(0), height(0), id(counter++), border(0), spacing(0), name("") {}
  Widget(const string &name, long long w, long long h)
      : width(w), height(h), id(counter++), border(0), spacing(0), name(name) {}
  Widget(const Widget &W)
      : width(W.width),
        height(W.height),
        id(W.id),
        border(W.border),
        spacing(W.spacing),
        name(W.name) {}
  Widget &operator=(const Widget &in) {
    width = in.width;
    height = in.height;
    border = in.border;
    spacing = in.spacing;
    name = in.name;
  }
  virtual pair<long long, long long> size() { return make_pair(width, height); }
  vector<string> ch;
  void pack(const string ch_name) { ch.push_back(ch_name); }
  void set_border(int b) { border = b; }
  void set_spacing(int s) { spacing = s; }
  virtual ~Widget() {}
};
class VBox : public Widget {
 public:
  VBox(const string &name) : Widget(name, 0, 0) {}
  virtual pair<long long, long long> size() {
    if (ch.size() == 0) return make_pair(0LL, 0LL);
    if (was.count(name)) return make_pair(width, height);
    was.insert(name);
    width = 0;
    height = 0;
    for (int _n(ch.size()), i(0); i < _n; i++) {
      long long w = (storage[ch[i]])->size().first;
      long long h = (storage[ch[i]])->size().second;
      height += h;
      width = max(width, w);
    }
    height += 1LL * (ch.size() - 1) * spacing;
    height += 2LL * border;
    width += 2LL * border;
    return make_pair(width, height);
  }
  virtual ~VBox() {}
};
class HBox : public Widget {
 public:
  HBox(const string &name) : Widget(name, 0, 0) {}
  virtual pair<long long, long long> size() {
    if (ch.size() == 0) return make_pair(0, 0);
    if (was.count(name)) return make_pair(width, height);
    was.insert(name);
    width = 0;
    height = 0;
    for (int _n(ch.size()), i(0); i < _n; i++) {
      long long w = (storage[ch[i]])->size().first;
      long long h = (storage[ch[i]])->size().second;
      height = max(height, h);
      width += w;
    }
    width += 1LL * (ch.size() - 1) * spacing;
    width += 2LL * border;
    height += 2LL * border;
    return make_pair(width, height);
  }
  virtual ~HBox() {}
};
int Widget::counter = 0;
int main() {
  storage.clear();
  GC.clear();
  int cnt;
  cin >> cnt;
  char buff[256];
  vector<string> widgets;
  cin.getline(buff, 256);
  for (int _n(cnt), it(0); it < _n; it++) {
    cin.getline(buff, 256);
    vector<string> tokens =
        tokenize(string(buff, buff + strlen(buff)), ".\n (),", true);
    if (tokens.size() < 2) continue;
    if (tokens[0] == "Widget") {
      Widget *ptr = new Widget(tokens[1], atoi(tokens[2].c_str()),
                               atoi(tokens[3].c_str()));
      storage[tokens[1]] = ptr;
      widgets.push_back(tokens[1]);
    } else if (tokens[0] == "HBox") {
      HBox *ptr = new HBox(tokens[1]);
      storage[tokens[1]] = static_cast<Widget *>(ptr);
      widgets.push_back(tokens[1]);
    } else if (tokens[0] == "VBox") {
      VBox *ptr = new VBox(tokens[1]);
      storage[tokens[1]] = static_cast<Widget *>(ptr);
      widgets.push_back(tokens[1]);
    } else if (tokens[0] != "" || tokens[0] != " ") {
      const string name = tokens[0];
      const string op = tokens[1];
      if (op == "set_border" || op == "set_spacing") {
        int val = atoi(tokens[2].c_str());
        if (op == "set_border") {
          (storage[name])->set_border(val);
        } else {
          (storage[name])->set_spacing(val);
        }
      } else {
        const string whoes = tokens[2];
        (storage[name])->pack(whoes);
      }
    }
  }
  was.clear();
  sort((widgets).begin(), (widgets).end());
  for (int _n(widgets.size()), i(0); i < _n; i++) {
    Widget *ptr = storage[widgets[i]];
    cout << widgets[i] << ' ' << ptr->size().first << ' ' << ptr->size().second
         << endl;
  }
  for (map<string, Widget *>::iterator iter = storage.begin();
       iter != storage.end(); ++iter)
    delete iter->second;
  return 0;
}
