#include <bits/stdc++.h>
using namespace std;
int n;
char buf[100], name[100], op[100], tmp[100];
struct widget {
  long long x, y;
  bool finish;
  int type, border, spacing;
  vector<int> w;
  widget(long long xx = 0, long long yy = 0, int t = 0) {
    x = xx;
    y = yy;
    type = t;
    w = vector<int>();
    finish = false;
    border = spacing = 0;
  }
  void gao(){};
} wid[110];
map<string, int> mp;
void gao(int id) {
  if (wid[id].finish || wid[id].type == 0) return;
  long long &x = wid[id].x;
  long long &y = wid[id].y;
  x = y = 0;
  for (int i = 0; i < ((int)wid[id].w.size()); ++i) {
    int z = wid[id].w[i];
    gao(z);
    if (wid[id].type == 1) {
      x += wid[z].x + wid[id].spacing;
      y = max(y, wid[z].y);
    } else {
      y += wid[z].y + wid[id].spacing;
      x = max(x, wid[z].x);
    }
  }
  wid[id].finish = true;
  if (((int)wid[id].w.size()) == 0) return;
  if (wid[id].type == 1) {
    x += wid[id].border * 2 - wid[id].spacing;
    y += wid[id].border * 2;
  } else {
    y += wid[id].border * 2 - wid[id].spacing;
    x += wid[id].border * 2;
  }
}
void solve() {
  mp.clear();
  int x, y, tot = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%s", buf);
    if (string(buf) == "Widget") {
      scanf("%s", buf);
      sscanf(buf, "%[^(](%d,%d)", name, &x, &y);
      mp[name] = tot;
      wid[tot++] = widget(x, y, 0);
    } else if (string(buf) == "HBox") {
      scanf("%s", buf);
      mp[buf] = tot;
      wid[tot++] = widget(-1, -1, 1);
    } else if (string(buf) == "VBox") {
      scanf("%s", buf);
      mp[buf] = tot;
      wid[tot++] = widget(-1, -1, 2);
    } else {
      sscanf(buf, "%[^.].%[^(](%[^)]", name, op, tmp);
      int id = mp[name];
      if (string(op) == "set_border") {
        sscanf(tmp, "%d", &x);
        wid[id].border = x;
      } else if (string(op) == "set_spacing") {
        sscanf(tmp, "%d", &x);
        wid[id].spacing = x;
      } else {
        wid[id].w.push_back(mp[tmp]);
      }
    }
  }
  map<string, int>::iterator it = mp.begin();
  for (; it != mp.end(); ++it) {
    int id = it->second;
    gao(id);
    printf(
        "%s "
        "%I64d"
        " "
        "%I64d"
        "\n",
        it->first.c_str(), wid[id].x, wid[id].y);
  }
  puts("");
}
int main() {
  while (scanf("%d", &n) != EOF) {
    solve();
  }
  return 0;
}
