#include <bits/stdc++.h>
using namespace std;
const long long simple = 0, hor = 1, vert = 2;
struct Widget {
  long long type;
  long long width;
  long long height;
  long long border;
  long long space;
  vector<string> child;
  bool into;
  bool packed;
  Widget() {
    type = simple;
    width = 0;
    height = 0;
    border = 0;
    space = 0;
    into = false;
    packed = false;
  }
};
long long n;
map<string, Widget> lib;
long long from_string(string s) {
  long long res = 0;
  for (long long i = 0; i < s.size(); i++) {
    res = res * 10 + s[i] - '0';
  }
  return res;
}
void pack(string name) {
  long long w = 0, h = 0;
  if (lib[name].child.size() == 0) return;
  if (lib[name].packed) return;
  for (long long i = 0; i < lib[name].child.size(); i++) {
    pack(lib[name].child[i]);
    if (lib[name].type == hor) {
      w += lib[lib[name].child[i]].width + lib[name].space;
      h = max(h, lib[lib[name].child[i]].height);
    } else {
      w = max(w, lib[lib[name].child[i]].width);
      h += lib[lib[name].child[i]].height + lib[name].space;
    }
  }
  if (lib[name].type == hor) {
    w -= lib[name].space;
  }
  if (lib[name].type == vert) {
    h -= lib[name].space;
  }
  w += 2 * lib[name].border;
  h += 2 * lib[name].border;
  lib[name].width = w;
  lib[name].height = h;
  lib[name].packed = true;
}
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    string temp;
    Widget widget;
    string name;
    string arg;
    string func;
    cin >> temp;
    if (temp == "Widget") {
      widget.type = simple;
      cin >> temp;
      name = temp.substr(0, temp.find("(", 0));
      arg = temp.substr(temp.find("(", 0) + 1,
                        temp.find(",", 0) - temp.find("(", 0) - 1);
      widget.width = from_string(arg);
      arg = temp.substr(temp.find(",", 0) + 1,
                        temp.find(")", 0) - temp.find(",", 0) - 1);
      widget.height = from_string(arg);
      lib[name] = widget;
    } else if (temp == "HBox") {
      cin >> name;
      widget.type = hor;
      lib[name] = widget;
    } else if (temp == "VBox") {
      cin >> name;
      widget.type = vert;
      lib[name] = widget;
    } else {
      name = temp.substr(0, temp.find(".", 0));
      func = temp.substr(temp.find(".", 0) + 1,
                         temp.find("(", 0) - temp.find(".", 0) - 1);
      arg = temp.substr(temp.find("(", 0) + 1,
                        temp.find(")", 0) - temp.find("(", 0) - 1);
      if (func == "set_border") {
        lib[name].border = from_string(arg);
      } else if (func == "set_spacing") {
        lib[name].space = from_string(arg);
      } else if (func == "pack") {
        lib[name].child.push_back(arg);
      }
    }
  }
  for (map<string, Widget>::iterator i = lib.begin(); i != lib.end(); i++) {
    for (long long j = 0; j < i->second.child.size(); j++) {
      lib[i->second.child[j]].into = true;
    }
  }
  for (map<string, Widget>::iterator i = lib.begin(); i != lib.end(); i++) {
    if (!i->second.into) pack(i->first);
  }
  for (map<string, Widget>::iterator i = lib.begin(); i != lib.end(); i++) {
    cout << i->first << " " << i->second.width << " " << i->second.height
         << endl;
  }
  return 0;
}
