#include <bits/stdc++.h>
using namespace std;
long long n;
struct Node {
  long long x, y, type;
  long long border, space;
  set<pair<string, int> > Son;
};
map<string, Node> Ref;
void dfs(string i) {
  Node *p = &Ref[i];
  if (p->Son.size() == 0) {
    p->x = p->y = 0;
    return;
  }
  if (p->type == 1) {
    long long Max = 0;
    long long Sum = 0;
    for (set<pair<string, int> >::iterator it = p->Son.begin();
         it != p->Son.end(); it++) {
      Node *q = &Ref[it->first];
      if (q->x == -1) dfs(it->first);
      Max = max(Max, q->y);
      Sum += q->x;
    }
    p->x = 2 * p->border + p->space * (p->Son.size() - 1) + Sum;
    p->y = Max + 2 * p->border;
  } else {
    long long Max = 0;
    long long Sum = 0;
    for (set<pair<string, int> >::iterator it = p->Son.begin();
         it != p->Son.end(); it++) {
      Node *q = &Ref[it->first];
      if (q->x == -1) dfs(it->first);
      Max = max(Max, q->x);
      Sum += q->y;
    }
    p->x = Max + 2 * p->border;
    p->y = 2 * p->border + p->space * (p->Son.size() - 1) + Sum;
  }
}
void gao() {
  for (map<string, Node>::iterator it = Ref.begin(); it != Ref.end(); it++)
    if (it->second.x == -1) dfs(it->first);
  for (map<string, Node>::iterator it = Ref.begin(); it != Ref.end(); it++)
    cout << it->first << " " << it->second.x << " " << it->second.y << endl;
}
void Change(string &s) {
  for (long long j = 0; j < s.size(); j++)
    if (s[j] == '.' || s[j] == '(' || s[j] == ')' || s[j] == ',') s[j] = ' ';
}
int main() {
  Ref.clear();
  string s;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> s;
    if (s == "VBox") {
      string Name;
      cin >> Name;
      Ref[Name].type = 2;
      Ref[Name].border = Ref[Name].space = 0;
      Ref[Name].Son.clear();
      Ref[Name].x = -1;
    } else if (s == "HBox") {
      string Name;
      cin >> Name;
      Ref[Name].type = 1;
      Ref[Name].border = Ref[Name].space = 0;
      Ref[Name].Son.clear();
      Ref[Name].x = -1;
    } else if (s == "Widget") {
      cin >> s;
      Change(s);
      stringstream sin(s);
      string Name;
      long long x, y;
      sin >> Name >> x >> y;
      Node *t = &Ref[Name];
      t->x = x;
      t->y = y;
      t->type = 0;
    } else {
      Change(s);
      stringstream sin(s);
      string Name, op;
      sin >> Name >> op;
      if (op == "pack") {
        string Name2;
        sin >> Name2;
        Ref[Name].Son.insert(make_pair(Name2, (int)Ref[Name].Son.size()));
      } else if (op == "set_border") {
        long long Num;
        sin >> Num;
        Ref[Name].border = Num;
      } else if (op == "set_spacing") {
        long long Num;
        sin >> Num;
        Ref[Name].space = Num;
      }
    }
  }
  gao();
}
