#include <bits/stdc++.h>
using namespace std;
int n;
struct widget {
  string name;
  long long type, boarder, spacing;
  long long n, m;
  vector<widget *> parent;
  vector<widget *> P;
};
map<string, widget> M;
void i_just_change(widget *w) {
  if (w->type == 1) {
    if (w->P.size() == 0) {
      w->n = 0;
      w->m = 0;
    } else {
      long long curn = 0;
      long long curm = -1;
      for (int i = 0; i < w->P.size(); i++) curm = max(curm, w->P[i]->m);
      for (int i = 0; i < w->P.size(); i++) curn += w->P[i]->n;
      curn += (int(w->P.size()) - 1) * w->spacing;
      curn += 2 * w->boarder;
      curm += 2 * w->boarder;
      w->n = curn;
      w->m = curm;
    }
  } else if (w->type == 2) {
    if (w->P.size() == 0) {
      w->n = 0;
      w->m = 0;
    } else {
      long long curn = -1;
      long long curm = 0;
      for (int i = 0; i < w->P.size(); i++) curn = max(curn, w->P[i]->n);
      for (int i = 0; i < w->P.size(); i++) curm += w->P[i]->m;
      curm += (int(w->P.size()) - 1) * w->spacing;
      curn += 2 * w->boarder;
      curm += 2 * w->boarder;
      w->n = curn;
      w->m = curm;
    }
  }
  for (int i = 0; i < w->parent.size(); i++) i_just_change(w->parent[i]);
}
int main() {
  scanf("%d\n", &n);
  string s;
  for (int metac = 1; metac <= n; metac++) {
    getline(cin, s);
    for (int i = 0; i < s.size(); i++)
      if (s[i] == '.' || s[i] == '(' || s[i] == ')' || s[i] == ',') s[i] = ' ';
    string ss[5];
    int sz = 0;
    for (int i = 0; i < s.size(); i++) {
      while (s[i] != ' ' && i < s.size()) ss[sz] += s[i], i++;
      sz++;
    }
    if (ss[0] == "Widget") {
      string name = ss[1];
      long long n = atoi(ss[2].c_str());
      long long m = atoi(ss[3].c_str());
      swap(n, m);
      widget w;
      w.n = n;
      w.m = m;
      w.name = name;
      w.type = 0;
      M[name] = w;
    } else if (ss[0] == "VBox") {
      string name = ss[1];
      widget w;
      w.n = 0;
      w.m = 0;
      w.name = name;
      w.type = 1;
      w.boarder = 0;
      w.spacing = 0;
      M[name] = w;
    } else if (ss[0] == "HBox") {
      string name = ss[1];
      widget w;
      w.n = 0;
      w.m = 0;
      w.name = name;
      w.type = 2;
      w.boarder = 0;
      w.spacing = 0;
      M[name] = w;
    } else if (ss[1] == "pack") {
      string pack = ss[0];
      string packest = ss[2];
      M[packest].parent.push_back(&M[pack]);
      M[pack].P.push_back(&M[packest]);
      i_just_change(&M[packest]);
    } else if (ss[1] == "set_border") {
      string name = ss[0];
      long long a = atoi(ss[2].c_str());
      M[name].boarder = a;
      i_just_change(&M[name]);
    } else if (ss[1] == "set_spacing") {
      string name = ss[0];
      long long a = atoi(ss[2].c_str());
      M[name].spacing = a;
      i_just_change(&M[name]);
    }
  }
  for (map<string, widget>::iterator it = M.begin(); it != M.end(); it++)
    cout << it->first << " " << it->second.m << " " << it->second.n << endl;
  return 0;
}
