#include <bits/stdc++.h>
using namespace std;
class Glyph {
 public:
  string name;
  Glyph() {}
  Glyph(string _name) { name = _name; }
  virtual long long getWidth() = 0;
  virtual long long getHeight() = 0;
};
class Widget : public Glyph {
 public:
  int width;
  int height;
  Widget(string _name, int _width, int _height) : Glyph(_name) {
    width = _width;
    height = _height;
  }
  long long getWidth() { return width; }
  long long getHeight() { return height; }
};
class Box : public Glyph {
 public:
  int border;
  int spacing;
  vector<Glyph*> items;
  Box(string _name) : Glyph(_name) {
    border = 0;
    spacing = 0;
    _width = -1;
    _height = -1;
  }
  void pack(Glyph* item) {
    items.push_back(item);
    _width = -1;
    _height = -1;
  }
  void set_border(int _border) {
    border = _border;
    _width = -1;
    _height = -1;
  }
  void set_spacing(int _spacing) {
    spacing = _spacing;
    _width = -1;
    _height = -1;
  }

 protected:
  long long _width;
  long long _height;
};
class HBox : public Box {
 public:
  HBox(string _name) : Box(_name) {}
  long long getWidth() {
    if (_width != -1) return _width;
    if (items.size() == 0) return 0;
    _width = 0;
    for (vector<Glyph*>::iterator item = items.begin(); item != items.end();
         item++) {
      _width += (*item)->getWidth();
    }
    _width += spacing * (items.size() - 1);
    _width += 2 * border;
    return _width;
  }
  long long getHeight() {
    if (_height != -1) return _height;
    if (items.size() == 0) return 0;
    _height = 0;
    for (vector<Glyph*>::iterator item = items.begin(); item != items.end();
         item++) {
      if ((*item)->getHeight() > _height) {
        _height = (*item)->getHeight();
      }
    }
    _height += 2 * border;
    return _height;
  }
};
class VBox : public Box {
 public:
  VBox(string _name) : Box(_name) {}
  long long getWidth() {
    if (_width != -1) return _width;
    if (items.size() == 0) return 0;
    _width = 0;
    for (vector<Glyph*>::iterator item = items.begin(); item != items.end();
         item++) {
      if ((*item)->getWidth() > _width) {
        _width = (*item)->getWidth();
      }
    }
    _width += 2 * border;
    return _width;
  }
  long long getHeight() {
    if (_height != -1) return _height;
    if (items.size() == 0) return 0;
    _height = 0;
    for (vector<Glyph*>::iterator item = items.begin(); item != items.end();
         item++) {
      _height += (*item)->getHeight();
    }
    _height += spacing * (items.size() - 1);
    _height += 2 * border;
    return _height;
  }
};
int main(int argc, char* argv[]) {
  int n;
  cin >> n;
  string line;
  getline(cin, line);
  map<string, Glyph*> items;
  while (n-- > 0) {
    getline(cin, line);
    int space;
    if ((space = line.find(' ')) != string::npos) {
      string type = line.substr(0, space);
      if (type == "Widget") {
        int obracket = line.find('(');
        int cbracket = line.find(')');
        int comma = line.find(',');
        string name = line.substr(space + 1, obracket - space - 1);
        int width = atoi(line.substr(obracket + 1, comma - obracket).c_str());
        int height = atoi(line.substr(comma + 1, cbracket - comma).c_str());
        items.insert(
            pair<string, Glyph*>(name, new Widget(name, width, height)));
      }
      if (type == "HBox") {
        string name = line.substr(space + 1);
        items.insert(pair<string, Glyph*>(name, new HBox(name)));
      }
      if (type == "VBox") {
        string name = line.substr(space + 1);
        items.insert(pair<string, Glyph*>(name, new VBox(name)));
      }
    } else {
      int dot = line.find('.');
      int obracket = line.find('(');
      int cbracket = line.find(')');
      string name = line.substr(0, dot);
      string function = line.substr(dot + 1, obracket - dot - 1);
      string param = line.substr(obracket + 1, cbracket - obracket - 1);
      if (function == "pack") {
        ((Box*)items[name])->pack(items[param]);
      }
      if (function == "set_border") {
        ((Box*)items[name])->set_border(atoi(param.c_str()));
      }
      if (function == "set_spacing") {
        ((Box*)items[name])->set_spacing(atoi(param.c_str()));
      }
    }
  }
  for (map<string, Glyph*>::iterator item = items.begin(); item != items.end();
       item++) {
    cout << item->second->name << " " << item->second->getWidth() << " "
         << item->second->getHeight() << endl;
  }
  return 0;
}
