#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:134217728")
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
int n;
map<string, int> m;
vector<string> vv;
int was[100];
long long h[100], w[100], border[100], spacing[100];
int type[100];
vector<int> v[100];
int pp;
void add(string name, int t, int x, int y) {
  m[name] = pp;
  vv.push_back(name);
  type[pp] = t;
  w[pp] = x;
  h[pp] = y;
  pp++;
}
void go(int p) {
  if (was[p]) return;
  was[p] = 1;
  if (type[p] == 0) return;
  long long ww = 0, hh = 0;
  for (int i = 0; i < (((int)(v[p]).size())); i++) {
    int u = v[p][i];
    go(u);
    if (type[p] == 1) {
      ww += w[u];
      hh = max(hh, h[u]);
    } else {
      hh += h[u];
      ww = max(ww, w[u]);
    }
  }
  if (((int)(v[p]).size())) {
    if (type[p] == 1)
      ww += (((int)(v[p]).size()) - 1) * spacing[p];
    else
      hh += (((int)(v[p]).size()) - 1) * spacing[p];
    ww += 2 * border[p];
    hh += 2 * border[p];
    w[p] = ww;
    h[p] = hh;
  } else
    w[p] = h[p] = 0;
}
int main() {
  cin >> n;
  char str[10000];
  gets(str);
  for (int o = 0; o < (n); o++) {
    gets(str);
    string s = str;
    for (int i = 0; i < (((int)(s).size())); i++)
      if (s[i] == '.' || s[i] == ')' || s[i] == '(' || s[i] == ',') s[i] = ' ';
    stringstream sin(s);
    string s1, s2, s3, s4;
    int x, y;
    sin >> s1;
    if (s1 == "Widget") {
      sin >> s2 >> x >> y;
      add(s2, 0, x, y);
    } else if (s1 == "HBox") {
      sin >> s2;
      add(s2, 1, 0, 0);
    } else if (s1 == "VBox") {
      sin >> s2;
      add(s2, 2, 0, 0);
    } else {
      sin >> s2;
      int p = m[s1];
      if (s2 == "pack") {
        sin >> s3;
        int q = m[s3];
        v[p].push_back(q);
      } else {
        sin >> x;
        if (s2 == "set_border")
          border[p] = x;
        else
          spacing[p] = x;
      }
    }
  }
  sort((vv).begin(), (vv).end());
  for (int i = 0; i < (((int)(vv).size())); i++) {
    int p = m[vv[i]];
    go(p);
    cout << vv[i] << ' ' << w[p] << ' ' << h[p] << endl;
  }
  return 0;
}
