#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int inf = 1 << 28;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
long long In() {
  long long x;
  cin >> x;
  return x;
}
double inreal() {
  double x;
  scanf("%lf", &x);
  return x;
}
struct vertex {
  vector<vertex*> edge;
  string name;
  int i;
  int wid_type;
  long long width, height;
  long long border, spacing;
};
int V;
vertex G[11111];
bool vis[11111];
map<string, int> mp;
int get(string str) {
  if (mp.count(str)) return mp[str];
  G[V].i = V;
  G[V].height = 0;
  G[V].width = 0;
  G[V].border = 0;
  G[V].spacing = 0;
  return mp[str] = V++;
}
void solve(int u) {
  if (vis[u]) return;
  vis[u] = 1;
  for (int i = 0; i < G[u].edge.size(); i++) solve(G[u].edge[i]->i);
  long long wid = 0, hei = 0;
  if (G[u].wid_type == 1) {
    if (G[u].edge.size() == 0) return;
    for (int i = 0; i < G[u].edge.size(); i++) {
      hei += G[u].edge[i]->height;
      wid = max(wid, G[u].edge[i]->width);
    }
    hei += (G[u].edge.size() - 1) * G[u].spacing + G[u].border * 2;
    wid += G[u].border * 2;
    G[u].width = wid, G[u].height = hei;
  }
  if (G[u].wid_type == 2) {
    if (G[u].edge.size() == 0) return;
    for (int i = 0; i < G[u].edge.size(); i++) {
      wid += G[u].edge[i]->width;
      hei = max(hei, G[u].edge[i]->height);
    }
    wid += (G[u].edge.size() - 1) * G[u].spacing + G[u].border * 2;
    hei += G[u].border * 2;
    G[u].width = wid, G[u].height = hei;
  }
  return;
}
struct ress {
  string name;
  long long w, h;
};
struct cmp {
  bool operator()(const ress& a, const ress& b) { return a.name < b.name; }
};
int main() {
  long long i, j, k;
  long long n = in();
  string str, name, cmd;
  long long h, w, b, s, u, v;
  V = 0;
  for ((i) = (0); (i) < (int)(n); (i)++) {
    cin >> str;
    if (str == "Widget") {
      cin >> str;
      for ((j) = (0); (j) < (int)(str.size()); (j)++)
        if (!isalnum(str[j])) str[j] = ' ';
      stringstream ss(str);
      ss >> str >> w >> h;
      u = get(str);
      G[u].wid_type = 0;
      G[u].height = h;
      G[u].width = w;
      G[u].name = str;
    } else if (str == "VBox" or str == "HBox") {
      cin >> name, u = get(name), G[u].name = name;
      if (str == "VBox")
        G[u].wid_type = 1;
      else
        G[u].wid_type = 2;
    } else {
      for ((j) = (0); (j) < (int)(str.size()); (j)++)
        if (!isalnum(str[j]) and str[j] != '_') str[j] = ' ';
      stringstream ss(str);
      ss >> name >> cmd, u = get(name);
      if (cmd == "pack") {
        ss >> name, v = get(name), G[u].edge.push_back(&G[v]);
      }
      if (cmd == "set_border") ss >> b, G[u].border = b;
      if (cmd == "set_spacing") ss >> s, G[u].spacing = s;
    }
  }
  for ((i) = (0); (i) < (int)(V); (i)++) vis[i] = 0;
  for ((i) = (0); (i) < (int)(V); (i)++)
    if (vis[i] == 0) solve(i);
  vector<ress> A(V);
  for ((i) = (0); (i) < (int)(V); (i)++) {
    A[i].name = G[i].name;
    A[i].w = G[i].width;
    A[i].h = G[i].height;
  }
  sort((A).begin(), (A).end(), cmp());
  for ((i) = (0); (i) < (int)(A.size()); (i)++)
    cout << A[i].name << ' ' << A[i].w << ' ' << A[i].h << endl;
  return 0;
}
