#include <bits/stdc++.h>
using namespace std;
char s[1005], tmp[1005];
string a[1005];
vector<int> r[1005];
int n = 0, u[1005];
struct Obj {
  int type;
  long long x, y, b, s;
} obj[1005];
int getid(string &t) {
  for (int i = 0; i < n; i++)
    if (a[i] == t) return i;
  return -1;
}
void handle() {
  gets(s);
  string ss(s);
  for (int i = 0; s[i]; i++)
    if (s[i] == '.') {
      string t = ss.substr(0, i);
      int id = getid(t);
      if (s[i + 1] == 'p') {
        sscanf(s + i + 1, "pack(%[^)])", tmp);
        t = string(tmp);
        int id2 = getid(t);
        r[id].push_back(id2);
        u[id2] = 1;
      } else if (s[i + 5] == 'b') {
        int b;
        sscanf(s + i + 1, "set_border(%d)", &b);
        obj[id].b = b;
      } else if (s[i + 5] == 's') {
        int is;
        sscanf(s + i + 1, "set_spacing(%d)", &is);
        obj[id].s = is;
      } else
        puts("QQ");
    } else if (s[i] == ' ') {
      string t = ss.substr(0, i);
      if (t == "Widget") {
        int x, y;
        sscanf(s + i + 1, "%[^ (](%d,%d)", tmp, &x, &y);
        a[n] = tmp;
        obj[n].type = 0;
        obj[n].x = x;
        obj[n].y = y;
        ++n;
      } else if (t == "HBox") {
        sscanf(s + i + 1, "%s", tmp);
        a[n] = tmp;
        obj[n].type = 1;
        obj[n].b = obj[n].s = 0;
        ++n;
      } else if (t == "VBox") {
        sscanf(s + i + 1, "%s", tmp);
        a[n] = tmp;
        obj[n].type = 2;
        obj[n].b = obj[n].s = 0;
        ++n;
      } else
        puts("QQ");
    }
}
vector<pair<string, pair<long long, long long> > > ans;
void go(int x) {
  if (u[x] == 2) return;
  u[x] = 2;
  if (obj[x].type == 0) {
    ans.push_back(make_pair(a[x], make_pair(obj[x].x, obj[x].y)));
    return;
  }
  if (obj[x].type == 1) {
    long long xsum = 0, ymax = 0;
    for (int i = 0; i < (int)r[x].size(); i++) {
      go(r[x][i]);
      xsum += (i > 0) * obj[x].s + obj[r[x][i]].x;
      ymax = max(ymax, obj[r[x][i]].y);
    }
    obj[x].x = xsum + (r[x].size() > 0) * 2 * obj[x].b;
    obj[x].y = ymax + (r[x].size() > 0) * 2 * obj[x].b;
  } else {
    long long xmax = 0, ysum = 0;
    for (int i = 0; i < (int)r[x].size(); i++) {
      go(r[x][i]);
      ysum += (i > 0) * obj[x].s + obj[r[x][i]].y;
      xmax = max(xmax, obj[r[x][i]].x);
    }
    obj[x].x = xmax + (r[x].size() > 0) * 2 * obj[x].b;
    obj[x].y = ysum + (r[x].size() > 0) * 2 * obj[x].b;
  }
  ans.push_back(make_pair(a[x], make_pair(obj[x].x, obj[x].y)));
}
void solve() {
  for (int i = 0; i < n; i++) {
    if (!u[i]) {
      go(i);
    }
  }
  sort(ans.begin(), ans.end());
  for (int i = 0; i < (int)ans.size(); i++)
    printf("%s %I64d %I64d\n", ans[i].first.c_str(), ans[i].second.first,
           ans[i].second.second);
}
int main(void) {
  int nt;
  scanf("%d", &nt);
  gets(s);
  while (nt--) handle();
  solve();
  return 0;
}
