#include <bits/stdc++.h>
long long mpow(long long a, long long n, long long mod) {
  long long ret = 1;
  long long b = a;
  while (n) {
    if (n & 1) ret = (ret * b) % mod;
    b = (b * b) % mod;
    n >>= 1;
  }
  return (long long)ret;
}
using namespace std;
vector<string> ed;
vector<pair<pair<string, string>, pair<int, int> > > nodes;
int lookUp[(int)(1e3 + 25)];
struct node {
  long long b, s, h, w;
  node() {
    b = 0;
    s = 0;
    h = 0;
    w = 0;
  }
};
node a[102];
map<string, int> ma;
vector<int> G[102];
int indeg[102], outdeg[102];
bool vis[102];
void dfs(int x) {
  vis[x] = 1;
  for (int i = 0; i < G[x].size(); i++) {
    int to = G[x][i];
    if (!vis[to]) {
      dfs(to);
    }
  }
  if (outdeg[x] == 0) {
    return;
  }
  string type = nodes[lookUp[x]].first.second;
  if (type == "Widget") {
    return;
  }
  if (type == "HBox") {
    for (int i = 0; i < G[x].size(); i++) {
      a[x].h = max(a[G[x][i]].h, a[x].h);
      a[x].w += (a[G[x][i]].w + a[x].s);
    }
    if (!G[x].empty()) a[x].w -= (a[x].s);
    a[x].h += (a[x].b + a[x].b);
    a[x].w += (a[x].b + a[x].b);
  } else {
    for (int i = 0; i < G[x].size(); i++) {
      a[x].w = max(a[G[x][i]].w, a[x].w);
      a[x].h += (a[G[x][i]].h + a[x].s);
    }
    if (!G[x].empty()) a[x].h -= (a[x].s);
    a[x].h += (a[x].b + a[x].b);
    a[x].w += (a[x].b + a[x].b);
  }
}
void solve() {
  int n, c = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    string type;
    cin >> type;
    if (type == "Widget") {
      string s;
      cin >> s;
      string name;
      int j;
      for (j = 0; s[j] != '(' && j < s.size(); j++) {
        name += s[j];
      }
      j++;
      string x, y;
      for (; s[j] != ',' && j < s.size(); j++) {
        x += s[j];
      }
      j++;
      for (; s[j] != ')' && j < s.size(); j++) {
        y += s[j];
      }
      int X = atoi(x.c_str());
      int Y = atoi(y.c_str());
      nodes.push_back(make_pair(make_pair(name, type), make_pair(X, Y)));
    } else if (type == "VBox" || type == "HBox") {
      string name;
      cin >> name;
      nodes.push_back(make_pair(make_pair(name, type), make_pair(0, 0)));
    } else {
      ed.push_back(type);
    }
  }
  for (int i = 0; i < nodes.size(); i++) {
    ma[nodes[i].first.first] = ++c;
    lookUp[c] = i;
    a[c].w = nodes[i].second.first;
    a[c].h = nodes[i].second.second;
  }
  for (int i = 0; i < ed.size(); i++) {
    string name, other = ed[i];
    int j;
    for (j = 0; other[j] != '.' && j < other.size(); j++) {
      name += other[j];
    }
    j++;
    string action;
    for (; other[j] != '(' && j < other.size(); j++) {
      action += other[j];
    }
    if (action == "pack") {
      j++;
      string name1;
      for (; other[j] != ')' && j < other.size(); j++) {
        name1 += other[j];
      }
      G[ma[name]].push_back(ma[name1]);
      outdeg[ma[name]]++;
      indeg[ma[name1]]++;
    } else {
      j++;
      string x, y;
      for (; other[j] != ')' && j < other.size(); j++) {
        x += other[j];
      }
      int X = atoi(x.c_str());
      if (action == "set_border") {
        a[ma[name]].b = X;
      } else if (action == "set_spacing") {
        a[ma[name]].s = X;
      }
    }
  }
  for (int i = 1; i <= c; i++) {
    if (indeg[i] == 0) {
      dfs(i);
    }
  }
  for (map<string, int>::iterator it = ma.begin(); it != ma.end(); it++) {
    int x = it->second;
    cout << it->first << " " << a[it->second].w << " " << a[it->second].h
         << endl;
  }
}
int main() {
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
