#include <bits/stdc++.h>
using namespace std;
void dprintf(char* format, ...) {
  fprintf(stderr, "DEBUG: ");
  va_list argp;
  va_start(argp, format);
  vfprintf(stderr, format, argp);
  va_end(argp);
  fprintf(stderr, "\n");
}
struct Exception {
  const char* message;
  const int line;
  Exception(const char* m, int l) : message(m), line(l) {}
};
static char stringReader[2097152];
static inline bool ReadLine() {
  char* ptr = fgets(stringReader, sizeof(stringReader), stdin);
  if (ptr == 0) return false;
  int N = strlen(stringReader);
  if (stringReader[N - 1] == '\n') stringReader[N - 1] = 0;
  return true;
}
const long long ooLL = 0x3f3f3f3f3f3f3f3fLL;
const int oo = 0x3f3f3f3f;
const double eps = 1e-9;
enum eType { WIDGET, HBOX, VBOX };
struct Widget {
  vector<int> children;
  string name;
  mutable long long w, h;
  int s, b;
  eType type;
  mutable bool fixed;
  Widget(string const& n, long long _w, long long _h)
      : children(),
        name(n),
        w(_w),
        h(_h),
        s(0),
        b(0),
        type(WIDGET),
        fixed(true) {}
  Widget(string const& n, eType t)
      : children(), name(n), w(0), h(0), s(0), b(0), type(t), fixed(false) {}
  void FindSize(vector<Widget> const& widgets) const {
    if (fixed) return;
    if (children.size() != 0) {
      w = 0;
      h = 0;
      for (int i = 0, (N) = static_cast<int>((children).size()); i < N; ++i) {
        widgets[children[i]].FindSize(widgets);
        if (type == HBOX) {
          w += widgets[children[i]].w;
          h = max(h, widgets[children[i]].h);
        } else {
          h += widgets[children[i]].h;
          w = max(w, widgets[children[i]].w);
        }
      }
      h += 2 * b;
      w += 2 * b;
      if (type == HBOX)
        w += (children.size() - 1) * s;
      else
        h += (children.size() - 1) * s;
    }
    fixed = true;
  }
  bool operator<(Widget const& o) const { return name < o.name; }
};
int main() {
  try {
    for (int n; scanf("%d ", &n) == 1;) {
      vector<Widget> widgets;
      map<string, unsigned int> widgetMap;
      for (int i = 0; i < n; ++i) {
        scanf("%s", stringReader);
        if (strcmp(stringReader, "Widget") == 0) {
          scanf("%s", stringReader);
          char* ptr = strchr(stringReader, '(');
          *ptr++ = 0;
          string name = stringReader;
          int w, h;
          sscanf(ptr, "%d,%d", &w, &h);
          widgetMap[name] = widgets.size();
          widgets.push_back(Widget(name, w, h));
        } else if (strcmp(stringReader, "HBox") == 0) {
          scanf("%s", stringReader);
          string name = stringReader;
          widgetMap[name] = widgets.size();
          widgets.push_back(Widget(name, HBOX));
        } else if (strcmp(stringReader, "VBox") == 0) {
          scanf("%s", stringReader);
          string name = stringReader;
          widgetMap[name] = widgets.size();
          widgets.push_back(Widget(name, VBOX));
        } else {
          char* ptr = strchr(stringReader, '.');
          *ptr++ = 0;
          int a = widgetMap[stringReader];
          char* ptr2 = strchr(ptr, '(');
          *ptr2++ = 0;
          if (strcmp(ptr, "pack") == 0) {
            *(strchr(ptr2, ')')) = 0;
            int b = widgetMap[ptr2];
            widgets[a].children.push_back(b);
          } else if (strcmp(ptr, "set_border") == 0) {
            sscanf(ptr2, "%d", &widgets[a].b);
          } else if (strcmp(ptr, "set_spacing") == 0) {
            sscanf(ptr2, "%d", &widgets[a].s);
          } else
            assert(0);
        }
      }
      for (int i = 0, (N) = static_cast<int>((widgets).size()); i < N; ++i)
        widgets[i].FindSize(widgets);
      sort((widgets).begin(), (widgets).end());
      for (int i = 0, (N) = static_cast<int>((widgets).size()); i < N; ++i)
        printf("%s %lld %lld\n", widgets[i].name.c_str(), widgets[i].w,
               widgets[i].h);
    }
    return 0;
  } catch (const Exception& exception) {
    fprintf(stderr, "******************************\n");
    fprintf(stderr, "ERROR.%d: %s\n", exception.line, exception.message);
    fprintf(stderr, "******************************\n");
    throw exception;
  }
}
