#include <bits/stdc++.h>
using namespace std;
char name[105][20];
long long type[105], w[105], h[105], border[105], space[105];
int link[105][105];
bool caled[105];
int n, m;
void cal(int x) {
  if (caled[x]) return;
  caled[x] = true;
  if (link[x][0] == 0) return;
  int i, k;
  if (type[x] == 2) {
    for (i = 1; i <= link[x][0]; i++) {
      k = link[x][i];
      cal(k);
      w[x] += w[k];
      if (h[k] > h[x]) h[x] = h[k];
    }
    w[x] += border[x] * 2;
    h[x] += border[x] * 2;
    w[x] += space[x] * (link[x][0] - 1);
    return;
  }
  for (i = 1; i <= link[x][0]; i++) {
    k = link[x][i];
    cal(k);
    h[x] += h[k];
    if (w[k] > w[x]) w[x] = w[k];
  }
  w[x] += border[x] * 2;
  h[x] += border[x] * 2;
  h[x] += space[x] * (link[x][0] - 1);
}
void exchange(int x, int y) {
  int i, k;
  char c;
  k = max(strlen(name[x]), strlen(name[y]));
  for (i = 0; i <= k; i++) {
    c = name[x][i];
    name[x][i] = name[y][i];
    name[y][i] = c;
  }
}
int main() {
  int i, j, k, l, t;
  char s[1000], tmp[1000];
  scanf("%d", &m);
  n = 0;
  for (i = 1; i <= m; i++) {
    scanf("%s", s);
    if (!strcmp(s, "Widget")) {
      scanf("%s", s);
      n++;
      type[n] = 1;
      caled[n] = true;
      j = 0;
      while (s[j] != '(') {
        name[n][j] = s[j];
        j++;
      }
      name[n][j] = '\0';
      j++;
      w[n] = 0;
      h[n] = 0;
      while (s[j] != ',') {
        w[n] = w[n] * 10 + (s[j] - '0');
        j++;
      }
      j++;
      while (s[j] != ')') {
        h[n] = h[n] * 10 + (s[j] - '0');
        j++;
      }
      continue;
    }
    if ((!strcmp(s, "HBox")) || (!strcmp(s, "VBox"))) {
      n++;
      caled[n] = false;
      w[n] = 0;
      h[n] = 0;
      border[n] = 0;
      space[n] = 0;
      link[n][0] = 0;
      if (!strcmp(s, "HBox"))
        type[n] = 2;
      else
        type[n] = 3;
      scanf("%s", s);
      strcpy(name[n], s);
      continue;
    }
    j = 0;
    while (s[j] != '.') {
      tmp[j] = s[j];
      j++;
    }
    tmp[j] = '\0';
    j++;
    k = 1;
    while (strcmp(tmp, name[k]) != 0) k++;
    t = 0;
    while (s[j] != '(') {
      tmp[t] = s[j];
      j++;
      t++;
    }
    j++;
    tmp[t] = '\0';
    if (!strcmp(tmp, "pack")) {
      t = 0;
      while (s[j] != ')') {
        tmp[t] = s[j];
        t++;
        j++;
      }
      tmp[t] = '\0';
      l = 1;
      while (strcmp(tmp, name[l]) != 0) l++;
      if (k == l) continue;
      link[k][0]++;
      link[k][link[k][0]] = l;
      continue;
    }
    if (!strcmp(tmp, "set_border")) {
      border[k] = 0;
      while (s[j] != ')') {
        border[k] = border[k] * 10 + (s[j] - '0');
        j++;
      }
      continue;
    }
    if (!strcmp(tmp, "set_spacing")) {
      space[k] = 0;
      while (s[j] != ')') {
        space[k] = space[k] * 10 + (s[j] - '0');
        j++;
      }
      continue;
    }
  }
  for (i = 1; i <= n; i++) cal(i);
  for (i = 1; i <= n - 1; i++)
    for (j = i + 1; j <= n; j++)
      if (strcmp(name[i], name[j]) > 0) {
        swap(w[i], w[j]);
        swap(h[i], h[j]);
        exchange(i, j);
      }
  for (i = 1; i <= n; i++) printf("%s %I64d %I64d\n", name[i], w[i], h[i]);
  return 0;
}
