#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
string nam[MAXN];
struct Widget {
  long long w, h;
} t0[MAXN];
struct VBox {
  long long s, b, w, h, th, tw;
  vector<pair<int, int> > vec;
} t1[MAXN];
struct HBox {
  long long s, b, w, h, tw, th;
  vector<pair<int, int> > vec;
} t2[MAXN];
int cs[3], top = 0, use[MAXN][3];
map<string, int> mp1, mp2;
void fid(string str, int f) {
  mp2[str] = f;
  mp1[str] = cs[f]++;
  nam[top++] = str;
}
void input() {
  vector<string> vec;
  string st1, st2, st3;
  cin >> st1;
  int g = 0;
  if (st1 == "Widget") {
    cin >> st2;
    st3.clear();
    while (st2[g] != '(') {
      st3.push_back(st2[g]);
      ++g;
    }
    fid(st3, 0);
    long long w, h;
    w = h = 0;
    ++g;
    while (st2[g] != ',') {
      w = w * 10 + st2[g] - '0';
      ++g;
    }
    ++g;
    while (st2[g] != ')') {
      h = h * 10 + st2[g] - '0';
      ++g;
    }
    t0[cs[0] - 1].w = w;
    t0[cs[0] - 1].h = h;
  } else if (st1 == "VBox") {
    cin >> st2;
    fid(st2, 1);
    t1[cs[1] - 1].b = t1[cs[1] - 1].s = 0;
  } else if (st1 == "HBox") {
    cin >> st2;
    fid(st2, 2);
    t2[cs[2] - 1].b = t2[cs[2] - 1].s = 0;
  } else
    vec.push_back(st1);
  for (int i = 0; i < vec.size(); ++i) {
    string st = vec[i];
    st1.clear();
    st2.clear(), st3.clear();
    int j = 0;
    while (st[j] != '.') {
      st1.push_back(st[j]);
      ++j;
    }
    ++j;
    while (st[j] != '(') {
      st2.push_back(st[j]);
      ++j;
    }
    ++j;
    if (st2 == "set_border") {
      int f = mp2[st1], id = mp1[st1];
      long long num = 0;
      while (st[j] != ')') {
        num = num * 10 + st[j] - '0';
        ++j;
      }
      if (f == 1)
        t1[id].b = num;
      else
        t2[id].b = num;
    } else if (st2 == "set_spacing") {
      int f = mp2[st1], id = mp1[st1];
      long long num = 0;
      while (st[j] != ')') {
        num = num * 10 + st[j] - '0';
        ++j;
      }
      if (f == 1)
        t1[id].s = num;
      else
        t2[id].s = num;
    } else {
      while (st[j] != ')') {
        st3.push_back(st[j]);
        ++j;
      }
      int id1 = mp1[st1], f1 = mp2[st1];
      int id2 = mp1[st3], f2 = mp2[st3];
      if (f1 == 1)
        t1[id1].vec.push_back(make_pair(id2, f2));
      else
        t2[id1].vec.push_back(make_pair(id2, f2));
    }
  }
}
void dfs(int id, int f) {
  if (use[id][f]) return;
  use[id][f] = 1;
  if (f == 0) return;
  long long w, h;
  if (f == 1) {
    for (int i = 0; i < t1[id].vec.size(); ++i) {
      int tid = t1[id].vec[i].first;
      int tf = t1[id].vec[i].second;
      dfs(tid, tf);
      if (tf == 0) {
        w = t0[tid].w;
        h = t0[tid].h;
      } else if (tf == 1) {
        w = t1[tid].w;
        h = t1[tid].h;
      } else {
        w = t2[tid].w;
        h = t2[tid].h;
      }
      t1[id].tw = max(t1[id].tw, w);
      t1[id].th += h;
    }
  } else {
    for (int i = 0; i < t2[id].vec.size(); ++i) {
      int tid = t2[id].vec[i].first;
      int tf = t2[id].vec[i].second;
      dfs(tid, tf);
      if (tf == 0) {
        w = t0[tid].w;
        h = t0[tid].h;
      } else if (tf == 1) {
        w = t1[tid].w;
        h = t1[tid].h;
      } else {
        w = t2[tid].w;
        h = t2[tid].h;
      }
      t2[id].tw += w;
      t2[id].th = max(t2[id].th, h);
    }
  }
  if (f == 1) {
    if (t1[id].vec.size() == 0) return;
    t1[id].w = t1[id].tw + t1[id].b * 2;
    t1[id].h = t1[id].th + t1[id].b * 2 + t1[id].s * (t1[id].vec.size() - 1);
  } else {
    if (t2[id].vec.size() == 0) return;
    t2[id].w = t2[id].tw + t2[id].b * 2 + t2[id].s * (t2[id].vec.size() - 1);
    t2[id].h = t2[id].th + t2[id].b * 2;
  }
}
int main() {
  int m;
  scanf("%d", &m);
  while (m--) input();
  sort(nam, nam + top);
  for (int i = 0; i < top; ++i) {
    cout << nam[i] << ' ';
    int f = mp2[nam[i]], id = mp1[nam[i]];
    dfs(id, f);
    if (f == 0)
      printf("%I64d %I64d\n", t0[id].w, t0[id].h);
    else if (f == 1)
      printf("%I64d %I64d\n", t1[id].w, t1[id].h);
    else
      printf("%I64d %I64d\n", t2[id].w, t2[id].h);
  }
  return 0;
}
