#include <bits/stdc++.h>
using namespace std;
struct ww {
  string a;
  int ty;
  long long x, y, b, s;
  inline void print() {
    cout << a;
    printf(" %I64d %I64d\n", x, y);
  }
} a[1010];
int i, j, k, n, m, t, l, r;
int f[1010][1010], in[1010], g[1010], s[1010];
char p[1010];
inline int find(string b) {
  int i;
  for (i = 1; i <= t; i++)
    if (a[i].a == b) return i;
  a[++t].a = b;
  return t;
}
inline void Widget() {
  scanf("%s", p + 1);
  string b;
  int i, j, n = strlen(p + 1), lx = 0, ly = 0;
  for (i = 1; i <= n; i++)
    if (p[i] == '(')
      break;
    else
      b += p[i];
  for (j = i + 1; j <= n; j++)
    if (p[j] == ',')
      break;
    else
      lx = lx * 10 + p[j] - '0';
  for (i = j + 1; i <= n - 1; i++) ly = ly * 10 + p[i] - '0';
  int A = find(b);
  a[A].x = lx, a[A].y = ly;
}
inline void HBox() {
  string b;
  cin >> b;
  int A = find(b);
  a[A].ty = 1;
}
inline void VBox() {
  string b;
  cin >> b;
  int A = find(b);
  a[A].ty = 2;
}
inline bool cc1(const ww &A, const ww &B) { return A.a < B.a; }
void bfs() {
  int l = 1, r = 0, i, j, k;
  for (i = 1; i <= t; i++)
    if (!in[i]) g[++r] = i;
  for (; l <= r; l++) {
    int A = g[l];
    for (i = 1; i <= t; i++)
      if (f[A][i]) {
        in[i] -= f[A][i];
        if (!in[i]) g[++r] = i;
      }
  }
  for (i = r; i; i--) {
    int A = g[i];
    if (!a[A].ty) continue;
    for (j = 1; j <= t; j++)
      for (k = 1; k <= f[A][j]; k++) {
        s[A]++;
        if (a[A].ty == 1) {
          a[A].x += a[j].x;
          a[A].y = max(a[A].y, a[j].y);
        } else {
          a[A].y += a[j].y;
          a[A].x = max(a[A].x, a[j].x);
        }
      }
    if (!s[A])
      a[A].x = a[A].y = 0;
    else if (a[A].ty == 1)
      a[A].x += 2 * a[A].b + (s[A] - 1) * a[A].s, a[A].y += 2 * a[A].b;
    else
      a[A].y += 2 * a[A].b + (s[A] - 1) * a[A].s, a[A].x += 2 * a[A].b;
  }
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%s", p + 1);
    m = strlen(p + 1);
    for (j = 1; j <= m; j++)
      if (p[j] == '.') break;
    if (j <= m) {
      string s;
      for (k = 1; k <= j - 1; k++) s += p[k];
      int A = find(s);
      if (p[j + 1] == 'p') {
        for (k = j + 1; k <= m; k++)
          if (p[k] == '(') break;
        string w;
        int u;
        for (u = k + 1; u <= m - 1; u++) w += p[u];
        int B = find(w);
        f[A][B]++, in[B]++;
      } else {
        for (k = j + 1; k <= m; k++)
          if (p[k] == '(') break;
        int B = 0, u;
        for (u = k + 1; u <= m - 1; u++) B = B * 10 + p[u] - '0';
        if (p[j + 5] == 'b')
          a[A].b = B;
        else
          a[A].s = B;
      }
    } else if (p[1] == 'W')
      Widget();
    else if (p[1] == 'H')
      HBox();
    else
      VBox();
  }
  bfs();
  sort(a + 1, a + t + 1, cc1);
  for (i = 1; i <= t; i++) a[i].print();
  return 0;
}
