#include <bits/stdc++.h>
using namespace std;
struct Wid {
  long long w, h;
  Wid(long long _w = 0, long long _h = 0) : w(_w), h(_h) {}
  virtual void pack(Wid* w) {}
  virtual void set_border(long long x) {}
  virtual void set_spacing(long long x) {}
  virtual void calc() {}
};
struct Box : Wid {
  long long b, s;
  bool vis;
  vector<Wid*> v;
  Box() : b(0), s(0), vis(false) {}
  void pack(Wid* w) { v.push_back(w); }
  void set_border(long long x) { b = x; }
  void set_spacing(long long x) { s = x; }
};
struct HBox : Box {
  virtual void calc() {
    if (vis) return;
    vis = true;
    if (v.size() == 0) {
      h = 0, w = 0;
      return;
    }
    h = 0;
    w = 0;
    for (int i = 0; i < v.size(); ++i) {
      v[i]->calc();
      w += v[i]->w;
      if (i) w += s;
      h = max(h, v[i]->h);
    }
    h += 2 * b;
    w += 2 * b;
  }
};
struct VBox : Box {
  virtual void calc() {
    if (vis) return;
    vis = true;
    if (v.size() == 0) {
      h = 0, w = 0;
      return;
    }
    h = 0;
    w = 0;
    for (int i = 0; i < v.size(); ++i) {
      v[i]->calc();
      if (i) h += s;
      h += v[i]->h;
      w = max(w, v[i]->w);
    }
    w += 2 * b;
    h += 2 * b;
  }
};
char cmd[100000], name[100000], fun[100000], para[100000];
int main() {
  int n;
  while (scanf("%d ", &n) != EOF) {
    map<string, Wid*> mp;
    for (int i = 0; i < n; ++i) {
      scanf("%s", cmd);
      if (string(cmd) == "Widget") {
        int w, h;
        scanf(" %[^(](%d,%d)", name, &w, &h);
        mp[name] = new Wid(w, h);
      } else if (string(cmd) == "HBox") {
        scanf("%s", name);
        mp[name] = new HBox;
      } else if (string(cmd) == "VBox") {
        scanf("%s", name);
        mp[name] = new VBox;
      } else {
        sscanf(cmd, "%[^.].%[^(](%[^)])", name, fun, para);
        if (string(fun) == "pack") {
          mp[name]->pack(mp[para]);
        } else if (string(fun) == "set_border") {
          int d;
          sscanf(para, "%d", &d);
          mp[name]->set_border(d);
        } else {
          int d;
          sscanf(para, "%d", &d);
          mp[name]->set_spacing(d);
        }
      }
    }
    map<string, Wid*>::iterator it;
    for (it = mp.begin(); it != mp.end(); ++it) {
      it->second->calc();
      printf("%s %I64d %I64d\n", it->first.c_str(), it->second->w,
             it->second->h);
    }
  }
  return 0;
}
