#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
vector<int> v[102];
int vis[102];
struct p {
  long long x, y, border, space, type;
} p[102];
void dfs(int m) {
  vis[m] = 1;
  int i, j, k;
  long long x = 0, y = 0;
  for (i = 0; i < v[m].size(); i++) {
    if (!vis[v[m][i]]) {
      dfs(v[m][i]);
    }
    if (p[m].type == 0) {
      if (i > 0) y += p[m].space;
      y += p[v[m][i]].y;
      x = max(x, p[v[m][i]].x);
    } else {
      if (i > 0) x += p[m].space;
      y = max(y, p[v[m][i]].y);
      x += p[v[m][i]].x;
    }
  }
  if (v[m].size() > 0) {
    x += p[m].border * 2;
    y += p[m].border * 2;
  }
  p[m].x = x;
  p[m].y = y;
}
int main() {
  int i, j, k, m, n, q, cas, x, y;
  char s[200], c[200], c2[200];
  n = 0;
  scanf("%d", &cas);
  while (cas--) {
    scanf("%s", s);
    if (strcmp(s, "Widget") == 0) {
      scanf("%s", s);
      for (i = 0; s[i]; i++)
        if (s[i] == '(') break;
      k = i;
      for (i, j = 0; s[i]; i++, j++) c[j] = s[i];
      sscanf(c, "(%d,%d)", &x, &y);
      s[k] = 0;
      p[n].x = x;
      p[n].y = y;
      vis[n] = 1;
      mp[s] = n++;
    } else if (strcmp(s, "VBox") == 0) {
      scanf("%s", s);
      p[n].type = 0;
      mp[s] = n++;
    } else if (strcmp(s, "HBox") == 0) {
      scanf("%s", s);
      p[n].type = 1;
      mp[s] = n++;
    } else {
      char *pc, *pb;
      int a, b, value;
      pc = strstr(s, ".");
      pb = strstr(s, "(");
      *pb = 0;
      *pc = 0;
      strcpy(c2, pb + 1);
      strcpy(c, pc + 1);
      c2[strlen(c2) - 1] = 0;
      if (strcmp(c, "pack") == 0) {
        a = mp[s];
        b = mp[c2];
        v[a].push_back(b);
      } else if (strcmp(c, "set_border") == 0) {
        value = atoi(c2);
        a = mp[s];
        p[a].border = value;
      } else if (strcmp(c, "set_spacing") == 0) {
        value = atoi(c2);
        a = mp[s];
        p[a].space = value;
      }
    }
  }
  for (i = 0; i < n; i++)
    if (!vis[i]) dfs(i);
  for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) {
    k = it->second;
    cout << it->first << ' ' << p[k].x << ' ' << p[k].y << endl;
  }
  scanf("%*d");
}
