#include <bits/stdc++.h>
using namespace std;
string elimina(string s) {
  for (int i = 0; i < int(s.size()); i++) {
    if (s[i] == ')' or s[i] == '(' or s[i] == ',' or s[i] == '.') s[i] = ' ';
  }
  return s;
}
int main() {
  string ss;
  int n;
  cin >> n;
  getline(cin, ss);
  map<string, vector<string> > g;
  map<string, vector<string> > pare;
  map<string, string> tipus;
  map<string, long long int> border;
  map<string, long long int> spacing;
  map<string, long long int> width;
  map<string, long long int> height;
  map<string, long long int> aritat;
  for (int pas = 0; pas < n; pas++) {
    getline(cin, ss);
    ss = elimina(ss);
    istringstream ci(ss);
    string name;
    ci >> name;
    if (name == "VBox") {
      ci >> name;
      tipus[name] = "VBox";
    } else if (name == "HBox") {
      ci >> name;
      tipus[name] = "HBox";
    } else if (name == "Widget") {
      ci >> name;
      tipus[name] = "Widget";
      long long int w, h;
      ci >> w >> h;
      width[name] = w;
      height[name] = h;
    } else {
      string inst;
      ci >> inst;
      if (inst == "pack") {
        string name2;
        ci >> name2;
        g[name].push_back(name2);
        aritat[name]++;
        pare[name2].push_back(name);
      } else if (inst == "set_border") {
        long long int b;
        ci >> b;
        border[name] = b;
      } else if (inst == "set_spacing") {
        long long int spa;
        ci >> spa;
        spacing[name] = spa;
      }
    }
  }
  stack<string> s;
  for (map<string, string>::iterator it = tipus.begin(); it != tipus.end();
       it++) {
    string name = it->first;
    if (aritat[name] == 0) s.push(name);
  }
  while (not s.empty()) {
    string name = s.top();
    s.pop();
    if (tipus[name] == "HBox") {
      long long int ample = 0;
      long long int alt = 0;
      vector<string> &fill = g[name];
      for (int i = 0; i < int(fill.size()); i++) {
        string f = fill[i];
        alt = max(alt, height[f]);
        ample += width[f];
      }
      if (int(fill.size()) == 0) {
        ample = 0;
        alt = 0;
      } else if (int(fill.size()) == 1) {
        alt += 2 * border[name];
        ample += 2 * border[name];
      } else {
        alt += 2 * border[name];
        ample += 2 * border[name] + (int(fill.size()) - 1) * spacing[name];
      }
      height[name] = alt;
      width[name] = ample;
    } else if (tipus[name] == "VBox") {
      long long int ample = 0;
      long long int alt = 0;
      vector<string> &fill = g[name];
      for (int i = 0; i < int(fill.size()); i++) {
        string f = fill[i];
        alt += height[f];
        ample = max(ample, width[f]);
      }
      if (int(fill.size()) == 0) {
        ample = 0;
        alt = 0;
      } else if (int(fill.size()) == 1) {
        alt += 2 * border[name];
        ample += 2 * border[name];
      } else {
        alt += 2 * border[name] + (int(fill.size()) - 1) * spacing[name];
        ample += 2 * border[name];
      }
      height[name] = alt;
      width[name] = ample;
    }
    vector<string> &p = pare[name];
    for (int i = 0; i < int(p.size()); i++) {
      string nam = p[i];
      aritat[nam]--;
      if (aritat[nam] == 0) {
        s.push(nam);
      }
    }
  }
  for (map<string, string>::iterator it = tipus.begin(); it != tipus.end();
       it++) {
    string name = it->first;
    cout << name << " " << width[name] << " " << height[name] << endl;
  }
}
