#include <bits/stdc++.h>
using namespace std;
const int N = 105;
const long long MOD = 1e9 + 7;
const double eps = 1e-9;
const int H = 1, V = 2, W = 0;
struct widget {
  string name;
  int type, spacing, border;
  long long w, h, inside;
  widget() {}
  bool operator<(const widget& w) const { return name < w.name; }
} wid[N];
int n, m, ind[N];
vector<vector<int> > g;
map<string, int> idx;
char t[N];
int main() {
  scanf("%d", &n);
  g.resize(n);
  for (int i = 0; i < n; i++) {
    scanf("%s", t);
    string s = string(t);
    if (s == "Widget") {
      scanf("%s", t);
      s = string(t);
      int j = 0;
      while (j < s.size() && s[j] != '(') wid[m].name += s[j++];
      sscanf(s.substr(j).c_str(), "(%d,%d)", &wid[m].w, &wid[m].h);
      idx[wid[m].name] = m++;
    } else if (s == "VBox" || s == "HBox") {
      scanf("%s", t);
      wid[m].name = string(t);
      wid[m].type = (s[0] == 'V' ? V : H);
      idx[wid[m].name] = m++;
    } else {
      string name = "", method = "";
      int j = 0;
      while (j < s.size() && s[j] != '.') name += s[j++];
      j++;
      while (j < s.size() && s[j] != '(') method += s[j++];
      int wnum = idx[name];
      if (method == "pack") {
        string name2 = "";
        j++;
        while (j < s.size() && s[j] != ')') name2 += s[j++];
        int w2num = idx[name2];
        g[w2num].push_back(wnum);
        wid[wnum].inside++;
        ind[wnum]++;
      } else if (method == "set_border")
        sscanf(s.substr(j).c_str(), "(%d)", &wid[wnum].border);
      else
        sscanf(s.substr(j).c_str(), "(%d)", &wid[wnum].spacing);
    }
  }
  queue<int> q;
  for (int i = 0; i < m; i++)
    if (!ind[i]) q.push(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (wid[u].type != W && wid[u].inside) {
      if (wid[u].type == H)
        wid[u].w += (wid[u].inside - 1) * wid[u].spacing;
      else
        wid[u].h += (wid[u].inside - 1) * wid[u].spacing;
      wid[u].h += 2 * wid[u].border;
      wid[u].w += 2 * wid[u].border;
    }
    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      ind[v]--;
      if (wid[v].type == W) {
        wid[v].w += wid[u].w;
        wid[v].h += wid[u].h;
      } else if (wid[v].type == H) {
        wid[v].w += wid[u].w;
        wid[v].h = max(wid[v].h, wid[u].h);
      } else {
        wid[v].h += wid[u].h;
        wid[v].w = max(wid[v].w, wid[u].w);
      }
      if (!ind[g[u][i]]) q.push(g[u][i]);
    }
  }
  sort(wid, wid + m);
  for (int i = 0; i < m; i++)
    printf("%s %lld %lld\n", wid[i].name.c_str(), wid[i].w, wid[i].h);
  return 0;
}
