#include <bits/stdc++.h>
using namespace std;
class Widget {
 public:
  long long int w, h, border, space, type;
  bool cw, ch;
  multiset<string> son;
  long long int getw();
  long long int geth();
};
map<string, Widget> Map;
long long int Widget::getw() {
  long long int& res = w;
  if (cw) return res;
  cw = true;
  if (type == 0) return res = w;
  if (type == 1) {
    if (son.empty()) return res = 0;
    res = w + border * 2;
    for (multiset<string>::iterator it = son.begin(); it != son.end(); it++) {
      res += Map[*it].getw();
      if (it != son.begin()) res += space;
    }
    return res;
  } else {
    if (son.empty()) return res = 0;
    res = 0;
    for (multiset<string>::iterator it = son.begin(); it != son.end(); it++)
      res = max(res, Map[*it].getw());
    return res += border * 2;
  }
}
long long int Widget::geth() {
  long long int& res = h;
  if (ch) return res;
  ch = true;
  if (type == 0) return res = h;
  if (type == 2) {
    if (son.empty()) return res = 0;
    res = h + border * 2;
    for (multiset<string>::iterator it = son.begin(); it != son.end(); it++) {
      res += Map[*it].geth();
      if (it != son.begin()) res += space;
    }
    return res;
  } else {
    if (son.empty()) return res = 0;
    res = 0;
    for (multiset<string>::iterator it = son.begin(); it != son.end(); it++)
      res = max(res, Map[*it].geth());
    return res += border * 2;
  }
}
int main() {
  int _;
  string s;
  for (cin >> _; _--;) {
    cin >> s;
    if (s == "Widget") {
      Widget tmp;
      cin >> s;
      string name;
      int now = 0;
      for (int i = 0; i < s.length(); i++) {
        if (isdigit(s[i]))
          now = now * 10 + s[i] - '0';
        else if (s[i] == ',') {
          tmp.w = now;
          now = 0;
        } else if (s[i] == ')') {
          tmp.h = now;
        } else if (s[i] != '(')
          name += s[i];
      }
      tmp.border = 0;
      tmp.space = 0;
      tmp.son.clear();
      tmp.type = 0;
      tmp.cw = false;
      tmp.ch = false;
      Map[name] = tmp;
    } else if (s == "HBox") {
      Widget tmp;
      tmp.border = 0;
      tmp.space = 0;
      tmp.w = 0;
      tmp.h = 0;
      tmp.son.clear();
      tmp.type = 1;
      tmp.cw = false;
      tmp.ch = false;
      cin >> s;
      Map[s] = tmp;
    } else if (s == "VBox") {
      Widget tmp;
      tmp.border = 0;
      tmp.space = 0;
      tmp.w = 0;
      tmp.h = 0;
      tmp.son.clear();
      tmp.type = 2;
      tmp.cw = false;
      tmp.ch = false;
      cin >> s;
      Map[s] = tmp;
    } else {
      for (int i = 0; i < s.length(); i++)
        if (s[i] == '.') {
          string name = s.substr(0, i);
          for (int i2 = i + 1; i2 < s.length(); i2++)
            if (s[i2] == '(') {
              string action = s.substr(i + 1, i2 - (i + 1)),
                     para = s.substr(i2 + 1, s.length() - (i2 + 1) - 1);
              if (action == "pack") {
                Map[name].son.insert(para);
              } else if (action == "set_border") {
                sscanf(para.c_str(), "%I64d", &Map[name].border);
              } else if (action == "set_spacing") {
                sscanf(para.c_str(), "%I64d", &Map[name].space);
              }
              break;
            }
          break;
        }
    }
  }
  for (map<string, Widget>::iterator it = Map.begin(); it != Map.end(); it++) {
    cout << it->first << " " << it->second.getw() << " " << it->second.geth()
         << endl;
  }
  return 0;
}
