#include <bits/stdc++.h>
using namespace std;
struct widget {
  string name;
  unsigned long long w;
  unsigned long long h;
  int type;
  vector<string> children;
  int border;
  int spacing;
  bool knownw;
  bool knownh;
  widget() {
    name = "";
    w = 0;
    h = 0;
    type = 0;
    border = 0;
    spacing = 0;
    knownw = knownh = false;
  }
  widget(const widget& other) {
    name = other.name;
    w = other.w;
    h = other.h;
    type = other.type;
    children.clear();
    for (int i = 0; i < other.children.size(); i++)
      children.push_back(other.children[i]);
    border = other.border;
    spacing = other.spacing;
    knownw = other.knownw;
    knownh = other.knownh;
  }
  unsigned long long get_width();
  unsigned long long get_height();
};
map<std::string, widget> W;
set<std::string> names;
unsigned long long widget::get_width() {
  if (type == 0 || knownw) {
    return w;
  } else if (type == 1) {
    knownw = true;
    if (children.size() == 0) return w = 0;
    unsigned long long ans = border;
    bool space = false;
    for (int i = 0; i < children.size(); i++) {
      if (space) ans += spacing;
      space = true;
      ans += W[children[i]].get_width();
    }
    ans += border;
    return w = ans;
  } else if (type == 2) {
    knownw = true;
    if (children.size() == 0) return w = 0;
    unsigned long long ans = 0;
    for (int i = 0; i < children.size(); i++) {
      ans = max(ans, W[children[i]].get_width());
    }
    ans += 2 * border;
    return w = ans;
  }
}
unsigned long long widget::get_height() {
  if (type == 0 || knownh) {
    return h;
  } else if (type == 2) {
    knownh = true;
    if (children.size() == 0) return h = 0;
    unsigned long long ans = border;
    bool space = false;
    for (int i = 0; i < children.size(); i++) {
      if (space) ans += spacing;
      space = true;
      ans += W[children[i]].get_height();
    }
    ans += border;
    return h = ans;
  } else if (type == 1) {
    knownh = true;
    if (children.size() == 0) return h = 0;
    unsigned long long ans = 0;
    for (int i = 0; i < children.size(); i++) {
      ans = max(ans, W[children[i]].get_height());
    }
    ans += 2 * border;
    return h = ans;
  }
}
int main() {
  int N;
  cin >> N;
  cin.ignore();
  for (int i = 0; i < N; i++) {
    stringstream sst;
    string line;
    getline(cin, line);
    sst << line;
    if (line[6] == ' ' || line[4] == ' ') {
      char type[60], name[60];
      int w, h;
      w = 0;
      h = 0;
      sscanf(line.data(), "%s %s", type, name);
      int tp = 0;
      widget new_widget;
      if (strcmp(type, "Widget") == 0) {
        sst >> type;
        memset(name, 0, 50);
        sst.ignore();
        sst.getline(name, 50, '(');
        char c;
        sst >> w >> c >> h;
        new_widget.w = w;
        new_widget.h = h;
      } else if (type[0] == 'H') {
        tp = 1;
      } else {
        tp = 2;
      }
      new_widget.name = name;
      new_widget.type = tp;
      new_widget.knownw = new_widget.knownh = false;
      W[new_widget.name] = new_widget;
      names.insert(new_widget.name);
    } else {
      string name1, command, name2;
      int x = 0;
      getline(sst, name1, '.');
      getline(sst, command, '(');
      if (command == "pack") {
        getline(sst, name2, ')');
        W[name1].children.push_back(name2);
      } else {
        sst >> x;
        if (command == "set_border") {
          W[name1].border = x;
        } else {
          W[name1].spacing = x;
        }
      }
    }
  }
  for (set<string>::iterator i = names.begin(); i != names.end(); i++) {
    widget& w = W[(*i)];
    cout << w.name << " " << w.get_width() << " " << w.get_height() << endl;
  }
}
