#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool gmin(T &a, T b) {
  if (a > b) return a = b, true;
  return false;
}
template <class T>
inline bool gmax(T &a, T b) {
  if (a < b) return a = b, true;
  return false;
}
template <class T>
T exgcd(T a, T b, T &first, T &second) {
  if (!b)
    return first = (T)1, second = (T)0, a;
  else {
    T d = exgcd(b, a % b, second, first);
    return second -= a / b * first, d;
  }
}
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const double DINF = 1e10;
const double EPS = 1e-9;
const double PI = 3.14159265358979323846264338327950288;
const double TAU = 2.0 * PI;
inline int sgn(const double &a) { return a > EPS ? 1 : (a < -EPS ? -1 : 0); }
inline int lowbit(int i) { return i & (-i); }
const int MAXN = 105;
struct Node {
  string name;
  long long w, h;
  int b, s, t;
  Node() {}
  Node(string _name, long long _w, long long _h, int _t)
      : name(_name), w(_w), h(_h), t(_t) {
    b = s = 0;
  }
} node[MAXN];
struct Edge {
  int dest;
  Edge *next;
  Edge() {}
  Edge(int _dest, Edge *_next) : dest(_dest), next(_next) {}
} EdgePool[MAXN], *EP, *e[MAXN];
int n, cnt;
bool root[MAXN], calc[MAXN];
map<string, int> mymap;
void addedge(int a, int b) {
  e[a] = new (EP++) Edge(b, e[a]);
  root[b] = 0;
}
void dfs(int i) {
  if (calc[i]) return;
  calc[i] = 1;
  int cnt = 0;
  long long maxw = 0, maxh = 0, sumw = 0, sumh = 0;
  for (Edge *j = e[i]; j; j = j->next) {
    dfs(j->dest);
    gmax(maxw, node[j->dest].w);
    gmax(maxh, node[j->dest].h);
    sumw += node[j->dest].w;
    sumh += node[j->dest].h;
    cnt++;
  }
  if (node[i].t == 1) {
    if (cnt) {
      node[i].w = node[i].b * 2 + node[i].s * (cnt - 1) + sumw;
      node[i].h = node[i].b * 2 + maxh;
    } else
      node[i].w = node[i].h = 0;
  }
  if (node[i].t == 2) {
    if (cnt) {
      node[i].w = node[i].b * 2 + maxw;
      node[i].h = node[i].b * 2 + node[i].s * (cnt - 1) + sumh;
    } else
      node[i].w = node[i].h = 0;
  }
}
struct cmp {
  bool operator()(const Node &a, const Node &b) { return a.name < b.name; }
};
char st[MAXN];
int find(string s) {
  for (int i = 0; i < cnt; i++)
    if (node[i].name == s) return i;
  return 0;
}
int main() {
  EP = EdgePool;
  memset(e, 0, sizeof(e));
  scanf("%d", &n);
  memset(root, 1, sizeof(root));
  for (int i = 1; i <= n; i++) {
    string op;
    scanf("%s", st);
    op = st;
    if (op == "Widget") {
      string para;
      scanf("%s", st);
      para = st;
      string name;
      int w = 0, h = 0;
      for (int i = 0; i < (int)(para.size()); i++) {
        if (para[i] != '(')
          name += para[i];
        else {
          int j;
          for (j = i + 1; para[j] != ','; j++) w = w * 10 + para[j] - '0';
          for (j = j + 1; para[j] != ')'; j++) h = h * 10 + para[j] - '0';
          break;
        }
      }
      node[cnt++] = Node(name, w, h, 0);
    } else {
      if (op == "HBox") {
        string name;
        scanf("%s", st);
        name = st;
        node[cnt++] = Node(name, 0, 0, 1);
      } else {
        if (op == "VBox") {
          string name;
          scanf("%s", st);
          name = st;
          node[cnt++] = Node(name, 0, 0, 2);
        } else {
          string name1;
          for (int i = 0; i < (int)(op.size()); i++) {
            if (op[i] != '.')
              name1 += op[i];
            else {
              int j;
              string op2;
              for (j = i + 1; op[j] != '('; j++) op2 += op[j];
              if (op2 == "pack") {
                string name2;
                for (j = j + 1; op[j] != ')'; j++) name2 += op[j];
                addedge(find(name1), find(name2));
              }
              if (op2 == "set_border") {
                int b = 0;
                for (j = j + 1; op[j] != ')'; j++) b = b * 10 + op[j] - '0';
                node[find(name1)].b = b;
              }
              if (op2 == "set_spacing") {
                int s = 0;
                for (j = j + 1; op[j] != ')'; j++) s = s * 10 + op[j] - '0';
                node[find(name1)].s = s;
              }
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < cnt; i++)
    if (root[i]) dfs(i);
  sort(node, node + cnt, cmp());
  for (int i = 0; i < cnt; i++)
    cout << node[i].name << " " << node[i].w << " " << node[i].h << endl;
  return 0;
}
