#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-12;
const int INF = 999999999;
struct Widget {
  int type;
  string name;
  long long w, h;
  int border, space;
  vector<int> next;
};
int na;
Widget a[100];
map<string, int> id;
bool flag[100];
int pbuf;
char buf[1000], buf2[1000];
string ReadTo(char c) {
  int i;
  for (i = pbuf; buf[i] != c; ++i)
    ;
  string res;
  for (int j = pbuf; j < i; ++j) res += buf[j];
  pbuf = i + 1;
  return res;
}
void Go(int x) {
  if (flag[x]) return;
  flag[x] = 1;
  if (a[x].type == 0) return;
  a[x].w = a[x].h = 0;
  if (a[x].next.empty()) return;
  for (typeof((a[x].next).begin()) i = (a[x].next).begin();
       i != (a[x].next).end(); i++) {
    Go(*i);
    if (a[x].type == 2) {
      a[x].h += a[*i].h;
      a[x].h += a[x].space;
      a[x].w = max(a[x].w, a[*i].w);
    } else {
      a[x].w += a[*i].w;
      a[x].w += a[x].space;
      a[x].h = max(a[x].h, a[*i].h);
    }
  }
  if (a[x].type == 2) {
    a[x].h -= a[x].space;
  } else {
    a[x].w -= a[x].space;
  }
  a[x].w += a[x].border * 2;
  a[x].h += a[x].border * 2;
}
void Com() {
  memset(flag, 0, sizeof(flag));
  for (int i = 0; i < na; ++i) Go(i);
}
void Pack(int p, int q) {
  a[p].next.push_back(q);
  Com();
}
void Solve() {
  int T;
  string s;
  scanf("%d", &T);
  fgets(buf, sizeof(buf), stdin);
  na = 0;
  while (T--) {
    fgets(buf, sizeof(buf), stdin);
    pbuf = 0;
    if (buf[0] == 'W') {
      ReadTo(' ');
      s = ReadTo('(');
      int w, h;
      sscanf(buf + pbuf, "%d,%d", &w, &h);
      a[na].name = s;
      a[na].type = 0;
      id[s] = na;
      a[na].w = w, a[na].h = h;
      a[na].border = a[na].space = 0;
      a[na].next.clear();
      ++na;
    } else if (buf[0] == 'V' || buf[0] == 'H') {
      if (buf[0] == 'H')
        a[na].type = 1;
      else
        a[na].type = 2;
      ReadTo(' ');
      sscanf(buf + pbuf, "%s", buf2);
      s = buf2;
      a[na].name = s;
      id[s] = na;
      a[na].w = a[na].h = 0;
      a[na].border = a[na].space = 0;
      a[na].next.clear();
      ++na;
    } else {
      s = ReadTo('.');
      int id1 = id[s];
      s = ReadTo('(');
      if (s[0] == 'p') {
        s = ReadTo(')');
        int id2 = id[s];
        Pack(id1, id2);
      } else if (s[4] == 'b') {
        sscanf(buf + pbuf, "%d", &a[id1].border);
        Com();
      } else {
        sscanf(buf + pbuf, "%d", &a[id1].space);
        Com();
      }
    }
  }
}
void Output() {
  for (typeof((id).begin()) i = (id).begin(); i != (id).end(); i++) {
    const Widget& p = a[i->second];
    printf("%s %I64d %I64d\n", p.name.c_str(), p.w, p.h);
  }
}
int main() {
  Solve();
  Output();
  return 0;
}
