#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
string names[maxn];
long long w[maxn], h[maxn];
int border[maxn], spacing[maxn];
int vh[maxn];
int inside[maxn];
string q;
struct st {
  string name;
  long long w, h;
  st() {}
  st(string name1, long long wn, long long hn) {
    name = name1;
    w = wn;
    h = hn;
  }
};
inline bool operator<(const st &s1, const st &s2) { return s1.name < s2.name; };
st str[maxn];
int main() {
  int n;
  while (scanf("%d\n", &n) >= 1) {
    memset(w, 0, sizeof(w));
    memset(h, 0, sizeof(h));
    memset(border, 0, sizeof(border));
    memset(spacing, 0, sizeof(spacing));
    memset(inside, 0, sizeof(inside));
    vector<vector<int> > es(n, vector<int>());
    int r = 0;
    for (int wer = 0; wer < n; wer++) {
      getline(cin, q);
      int m = q.length();
      int space = -1;
      for (int i = 0; i < m; i++) {
        if (q[i] == ' ') {
          space = i;
          break;
        }
      }
      if (space != -1) {
        switch (q[0]) {
          case 'W': {
            int end = -1;
            for (int i = space + 1; i < m; i++) {
              if (q[i] == '(') {
                end = i;
                break;
              }
            }
            names[r] = string(q, space + 1, end - space - 1);
            int wn = 0, hn = 0;
            int i;
            for (i = end + 1; q[i] != ','; i++) {
              wn *= 10;
              wn += q[i] - '0';
            }
            i++;
            for (; q[i] != ')'; i++) {
              hn *= 10;
              hn += q[i] - '0';
            }
            w[r] = wn, h[r] = hn;
            r++;
          } break;
          case 'V':
            names[r] = string(q, space + 1);
            w[r] = h[r] = 0;
            vh[r] = 1;
            r++;
            break;
          case 'H':
            names[r] = string(q, space + 1);
            w[r] = h[r] = 0;
            vh[r] = 0;
            r++;
            break;
        }
      } else {
        int dot = -1;
        for (int i = 0; i < m; i++)
          if (q[i] == '.') {
            dot = i;
            break;
          }
        int pr = -1;
        string namepr(q, 0, dot);
        for (int i = 0; i < r; i++) {
          if (namepr == names[i]) {
            pr = i;
            break;
          }
        }
        assert(pr != -1);
        switch (q[dot + 1]) {
          case 's': {
            int mins = -1;
            for (int i = dot + 1; i < m; i++) {
              if (q[i] == '_') {
                mins = i;
                break;
              }
            }
            assert(mins != -1);
            int opened = -1;
            for (int i = mins; i < m; i++) {
              if (q[i] == '(') {
                opened = i;
                break;
              }
            }
            assert(opened != -1);
            int x = 0;
            for (int i = opened + 1; q[i] != ')'; i++) {
              x *= 10;
              x += q[i] - '0';
            }
            switch (q[mins + 1]) {
              case 'b':
                border[pr] = x;
                break;
              case 's':
                spacing[pr] = x;
                break;
            }
          } break;
          case 'p':
            int opened = dot + 5;
            assert(q[opened] == '(');
            string namen(q, opened + 1, m - opened - 2);
            int numn = -1;
            for (int i = 0; i < r; i++) {
              if (namen == names[i]) {
                numn = i;
                break;
              }
            }
            assert(numn != -1);
            inside[pr]++;
            es[pr].push_back(numn);
            break;
        }
      }
      int rn = 0;
      vector<bool> counted(r, 0);
      int lss = r;
      for (int i = 0; i < r; i++) {
        if (!inside[i]) {
          str[rn++] = st(names[i], w[i], h[i]);
          counted[i] = 1;
          lss--;
        }
      }
      while (lss > 0) {
        for (int i = 0; i < r; i++) {
          if (counted[i]) continue;
          bool ok = 1;
          for (int it = 0; it < (int)es[i].size(); it++)
            if (!counted[es[i][it]]) {
              ok = 0;
              break;
            }
          if (!ok) continue;
          long long wn = 0, hn = 0;
          if (vh[i]) {
            hn += spacing[i] * (inside[i] - 1);
            for (int it = 0; it < (int)es[i].size(); it++) {
              hn += h[es[i][it]];
              wn = max(wn, w[es[i][it]]);
            }
          } else {
            wn += spacing[i] * (inside[i] - 1);
            for (int it = 0; it < (int)es[i].size(); it++) {
              wn += w[es[i][it]];
              hn = max(hn, h[es[i][it]]);
            }
          }
          wn += 2 * border[i], hn += 2 * border[i];
          w[i] = wn;
          h[i] = hn;
          str[rn++] = st(names[i], wn, hn);
          counted[i] = 1;
          lss--;
        }
      }
      sort(str, str + r);
    }
    for (int i = 0; i < r; i++) {
      cout << str[i].name;
      printf(" %I64d %I64d\n", str[i].w, str[i].h);
    }
  }
  return 0;
}
