#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const double PI = 2 * acos(0.0);
const double EPS = 1e-8;
const int INF = 1000000000;
struct tw {
  string name;
  string type;
  long long w, h;
  bool eval;
  int spacing, border;
  int nchild;
  string child[100];
};
int n, cnt;
string s, ss;
map<string, int> w;
tw t[1000];
vector<pair<string, pair<long long, long long> > > ans;
int parse(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); ++i)
    if (s[i] >= '0' && s[i] <= '9') res = 10 * res + (s[i] - '0');
  return res;
};
void makew(string s) {
  string name = s.substr(0, s.find("("));
  s = s.erase(0, s.find("(") + 1);
  s = s.erase(s.size() - 1, 1);
  int ww = parse(s.substr(0, s.find(",")));
  s = s.erase(0, s.find(",") + 1);
  int h = parse(s);
  t[cnt].name = name;
  t[cnt].h = h;
  t[cnt].w = ww;
  t[cnt].type = "w";
  w[name] = cnt;
  ++cnt;
};
void makeW(string name, string tp) {
  t[cnt].name = name;
  t[cnt].type = tp;
  w[name] = cnt;
  ++cnt;
};
void calcsize(int v) {
  if (t[v].eval) return;
  if (t[v].type == "w") return;
  t[v].eval = true;
  if (t[v].type == "H") {
    if (t[v].nchild == 0) {
      t[v].h = t[v].w = 0;
      return;
    };
    t[v].h = t[v].w = 0;
    for (int i = 0; i < t[v].nchild; ++i) {
      int u = w[t[v].child[i]];
      calcsize(u);
      if (t[v].h < t[u].h) t[v].h = t[u].h;
      t[v].w += t[u].w;
    };
    t[v].h += 2 * t[v].border;
    t[v].w += 2 * t[v].border + (t[v].nchild - 1) * t[v].spacing;
    return;
  };
  if (t[v].type == "V") {
    if (t[v].nchild == 0) {
      t[v].h = t[v].w = 0;
      return;
    };
    t[v].h = t[v].w = 0;
    for (int i = 0; i < t[v].nchild; ++i) {
      int u = w[t[v].child[i]];
      calcsize(u);
      if (t[v].w < t[u].w) t[v].w = t[u].w;
      t[v].h += t[u].h;
    };
    t[v].w += 2 * t[v].border;
    t[v].h += 2 * t[v].border + (t[v].nchild - 1) * t[v].spacing;
    return;
  };
};
void doi(string s) {
  string name = s.substr(0, s.find("."));
  s.erase(0, s.find(".") + 1);
  string op = s.substr(0, s.find("("));
  s.erase(0, s.find("(") + 1);
  s.erase(s.size() - 1, 1);
  if (op == "pack") {
    int u = w[name];
    t[u].child[t[u].nchild] = s;
    ++t[u].nchild;
  };
  if (op == "set_border") {
    int u = w[name];
    t[u].border = parse(s);
  };
  if (op == "set_spacing") {
    int u = w[name];
    t[u].spacing = parse(s);
  };
};
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    if (s == "Widget") {
      cin >> ss;
      makew(ss);
    } else if (s == "HBox") {
      cin >> ss;
      makeW(ss, "H");
    } else if (s == "VBox") {
      cin >> ss;
      makeW(ss, "V");
    } else
      doi(s);
  };
  for (int i = 0; i < cnt; ++i) t[i].eval = false;
  for (int i = 0; i < cnt; ++i) {
    calcsize(i);
    ans.push_back(make_pair(t[i].name, make_pair(t[i].w, t[i].h)));
  };
  sort(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); ++i)
    cout << ans[i].first << " " << ans[i].second.first << " "
         << ans[i].second.second << endl;
  return 0;
};
