#include <bits/stdc++.h>
using namespace std;
class widget {
 public:
  string name;
  long long w;
  long long h;
  virtual long long height() { return h; }
  virtual long long width() { return w; }
  widget(string _name, long long x, long long y) {
    name = _name;
    w = x;
    h = y;
  }
  virtual void pack(widget* ob){};
  virtual void set_border(long long x){};
  virtual void set_spacing(long long x){};
};
class HBox : public widget {
 public:
  long long border;
  long long spacing;
  vector<widget*> list;
  long long width() {
    if (w > -1) return w;
    long long s = 0;
    for (long long i = 0; i < list.size(); ++i) {
      s += list[i]->width();
      s += spacing;
    }
    if (list.size() > 0) {
      s -= spacing;
      s += 2 * border;
    }
    w = s;
    return s;
  }
  long long height() {
    if (h > -1) return h;
    long long s = 0;
    for (long long i = 0; i < list.size(); ++i)
      if (list[i]->height() > s) s = list[i]->height();
    if (list.size() > 0) s += 2 * border;
    h = s;
    return s;
  }
  HBox(string _name) : widget(_name, -1, -1) {
    border = 0;
    spacing = 0;
  }
  void pack(widget* ob) { list.push_back(ob); }
  void set_border(long long x) { border = x; };
  void set_spacing(long long x) { spacing = x; };
};
class VBox : public widget {
 public:
  long long n;
  long long border;
  long long spacing;
  vector<widget*> list;
  long long height() {
    if (h > -1) return h;
    long long s = 0;
    for (long long i = 0; i < list.size(); ++i) {
      s += list[i]->height();
      s += spacing;
    }
    if (list.size() > 0) {
      s -= spacing;
      s += 2 * border;
    }
    h = s;
    return s;
  }
  long long width() {
    if (w > -1) return w;
    long long s = 0;
    for (long long i = 0; i < list.size(); ++i)
      if (list[i]->width() > s) s = list[i]->width();
    if (list.size() > 0) s += 2 * border;
    w = s;
    return s;
  }
  VBox(string _name) : widget(_name, -1, -1) {
    border = 0;
    spacing = 0;
  }
  void pack(widget* ob) { list.push_back(ob); }
  void set_border(long long x) { border = x; };
  void set_spacing(long long x) { spacing = x; };
};
void sort(vector<widget*>& wl) {
  widget* t;
  for (long long i = 0; i < wl.size(); ++i)
    for (long long j = wl.size() - 1; j > i; --j)
      if ((wl[j]->name).compare(wl[j - 1]->name) < 0) {
        t = wl[j];
        wl[j] = wl[j - 1];
        wl[j - 1] = t;
      }
}
int main(int argc, char** argv) {
  int n;
  cin >> n;
  char c;
  vector<widget*> wl;
  string cur = "";
  getchar();
  for (int i = 0; i < n; ++i) {
    cur = "";
    c = getchar();
    if ((int)c == 10 || (int)c == 13) c = getchar();
    while (c != '.' && c != ' ') {
      cur += c;
      c = getchar();
    }
    if (c == ' ') {
      if (cur == "Widget") {
        cur = "";
        c = getchar();
        while (c != '(') {
          cur += c;
          c = getchar();
        }
        int x, y;
        scanf("%d,%d)", &x, &y);
        widget* t1 = new widget(cur, x, y);
        wl.push_back(t1);
        getchar();
      }
      if (cur == "HBox") {
        cur = "";
        c = getchar();
        while (c != '\n') {
          cur += c;
          c = getchar();
        }
        widget* t1 = new HBox(cur);
        wl.push_back(t1);
      }
      if (cur == "VBox") {
        cur = "";
        c = getchar();
        while (c != '\n') {
          cur += c;
          c = getchar();
        }
        widget* t1 = new VBox(cur);
        wl.push_back(t1);
      }
    } else {
      widget* t;
      for (int i = 0; i < wl.size(); ++i)
        if (wl[i]->name == cur) {
          t = wl[i];
          break;
        }
      cur = "";
      c = getchar();
      while (c != '(') {
        cur += c;
        c = getchar();
      }
      if (cur == "pack") {
        c = getchar();
        cur = "";
        while (c != ')') {
          cur += c;
          c = getchar();
        }
        widget* t1;
        for (int i = 0; i < wl.size(); ++i)
          if (wl[i]->name == cur) {
            t1 = wl[i];
            break;
          }
        t->pack(t1);
      }
      if (cur == "set_border") {
        int x;
        scanf("%d)", &x);
        t->set_border(x);
      }
      if (cur == "set_spacing") {
        int x;
        scanf("%d)", &x);
        t->set_spacing(x);
      }
      getchar();
    }
  }
  sort(wl);
  for (int i = 0; i < wl.size(); ++i) {
    cout << ((wl[i]->name)) << " " << (wl[i]->width()) << " "
         << (wl[i]->height()) << endl;
  }
  return 0;
}
