#include <bits/stdc++.h>
using namespace std;
class Widget {
 protected:
  vector<Widget*> widgets;
  virtual void initSize(){};

 public:
  long long border, spacing;
  long long height, width;
  Widget() { border = spacing = width = height = 0; }
  pair<long long, long long> getSize() {
    pair<long long, long long> insize;
    if (!height && !width) initSize();
    insize = pair<long long, long long>(height, width);
    if (widgets.size()) insize.first += border * 2;
    if (widgets.size()) insize.second += border * 2;
    return insize;
  }
  void pack(Widget* w) { widgets.push_back(w); }
};
class HBox : public Widget {
  void initSize() {
    for (__typeof(widgets.size()) i = 0; i < (widgets.size()); i++) {
      pair<long long, long long> chsize = widgets[i]->getSize();
      height = max(height, chsize.first);
      width += chsize.second;
      if (i) width += spacing;
    }
  }
};
class VBox : public Widget {
  void initSize() {
    for (__typeof(widgets.size()) i = 0; i < (widgets.size()); i++) {
      pair<long long, long long> chsize = widgets[i]->getSize();
      width = max(width, chsize.second);
      height += chsize.first;
      if (i) height += spacing;
    }
  }
};
class LeafBox : public Widget {
 public:
  LeafBox(int h, int w) : Widget() { width = w, height = h; }
};
int main() {
  string line;
  getline(cin, line);
  long long n = atoi(line.c_str());
  map<string, Widget*> widgets;
  for (__typeof(n) i = 0; i < (n); i++) {
    getline(cin, line);
    stringstream sin(line);
    string temp, name;
    if (line[0] == 'W' || line[0] == 'H' || line[0] == 'V') {
      getline(sin, temp, ' ');
      getline(sin, name, '(');
      if (line[0] == 'H')
        widgets[name] = new HBox;
      else if (line[0] == 'V')
        widgets[name] = new VBox;
      else {
        int w, h;
        getline(sin, temp, ',');
        w = atoi(temp.c_str());
        getline(sin, temp, ')');
        h = atoi(temp.c_str());
        widgets[name] = new LeafBox(h, w);
      }
    } else {
      getline(sin, name, '.');
      getline(sin, temp, '(');
      if (temp[0] == 'p') {
        getline(sin, temp, ')');
        widgets[name]->pack(widgets[temp]);
      } else if (temp[4] == 'b') {
        getline(sin, temp, ')');
        widgets[name]->border = atoi(temp.c_str());
      } else {
        getline(sin, temp, ')');
        widgets[name]->spacing = atoi(temp.c_str());
      }
    }
  }
  __typeof(widgets.end()) it;
  for (it = widgets.begin(); it != widgets.end(); it++) {
    pair<long long, long long> s = it->second->getSize();
    cout << it->first << ' ' << s.second << ' ' << s.first << endl;
  }
  for (it = widgets.begin(); it != widgets.end(); it++) {
    delete it->second;
  }
  return 0;
}
