#include <bits/stdc++.h>
using namespace std;
int n;
struct data {
  int type;
  long long x, y, s, b;
  vector<data *> son;
  bool done;
  data(int t = 0, long long xx = 0, long long yy = 0) : type(t) {
    if (t == 0) {
      x = xx;
      y = yy;
    } else {
      x = 0;
      y = 0;
      s = xx;
      b = yy;
    }
    done = 0;
  }
  void pack(data &v) { son.push_back(&v); }
  void set_border(int x) { b = x; }
  void set_spacing(int x) { s = x; }
  void dfs() {
    if (done) return;
    done = 1;
    if (type == 0) return;
    for (vector<data *>::iterator it = son.begin(); it != son.end(); it++)
      (*it)->dfs();
    if (type == 1) {
      for (vector<data *>::iterator it = son.begin(); it != son.end(); it++)
        x += (*it)->x, y = max(y, (*it)->y);
      if (son.size())
        x += (son.size() - 1) * s + b * 2, y += b * 2;
      else
        x = y = 0;
    } else {
      for (vector<data *>::iterator it = son.begin(); it != son.end(); it++)
        y += (*it)->y, x = max(x, (*it)->x);
      if (son.size())
        y += (son.size() - 1) * s + b * 2, x += b * 2;
      else
        x = y = 0;
    }
  }
};
ostream &operator<<(ostream &out, data &now) {
  return out << now.x << ' ' << now.y;
}
map<string, data> a;
int main() {
  cin >> n;
  while (n--) {
    string opt;
    cin >> opt;
    if (opt == "Widget") {
      cin >> opt;
      int i;
      for (i = 0; opt[i] != '('; i++)
        ;
      int j;
      for (j = i + 1; opt[j] != ','; j++)
        ;
      a[opt.substr(0, i)] =
          data(0, atoi(opt.substr(i + 1, j - (i + 1)).c_str()),
               atoi(opt.substr(j + 1, opt.size() - 1 - (j + 1)).c_str()));
    } else if (opt == "HBox") {
      cin >> opt;
      a[opt] = data(1);
    } else if (opt == "VBox") {
      cin >> opt;
      a[opt] = data(2);
    } else {
      int i, j;
      for (i = 0; opt[i] != '.'; i++)
        ;
      for (j = i + 1; opt[j] != '('; j++)
        ;
      string nam = opt.substr(0, i), fun = opt.substr(i + 1, j - (i + 1)),
             par = opt.substr(j + 1, opt.size() - 1 - (j + 1));
      if (fun == "pack") {
        a[nam].pack(a[par]);
      } else if (fun == "set_border") {
        a[nam].set_border(atoi(par.c_str()));
      } else if (fun == "set_spacing") {
        a[nam].set_spacing(atoi(par.c_str()));
      }
    }
  }
  for (map<string, data>::iterator it = a.begin(); it != a.end(); it++) {
    it->second.dfs();
    cout << it->first << ' ' << it->second << endl;
  }
}
