#include <bits/stdc++.h>
using namespace std;
char buf[1000000];
vector<vector<int> > g;
vector<bool> used;
vector<long long> border, space, w, h;
vector<int> tp;
void dfs(int v) {
  used[v] = true;
  for (int i = (0); i < (int((g[v]).size())); ++i)
    if (!used[g[v][i]]) dfs(g[v][i]);
  if (tp[v] == 0) {
    long long W = 0, H = 0;
    for (int i = (0); i < (int((g[v]).size())); ++i) {
      W += w[g[v][i]];
      H = max(H, h[g[v][i]]);
    }
    if (int((g[v]).size()) == 0) return;
    W += (int((g[v]).size()) - 1) * space[v] + 2 * border[v];
    H += 2 * border[v];
    w[v] = W;
    h[v] = H;
    return;
  }
  if (tp[v] == 1) {
    long long W = 0, H = 0;
    for (int i = (0); i < (int((g[v]).size())); ++i) {
      H += h[g[v][i]];
      W = max(W, w[g[v][i]]);
    }
    if (int((g[v]).size()) == 0) return;
    H += (int((g[v]).size()) - 1) * space[v] + 2 * border[v];
    W += 2 * border[v];
    w[v] = W;
    h[v] = H;
    return;
  }
}
int main() {
  g.resize(110);
  map<string, int> M;
  int it = 0;
  int n;
  cin >> n;
  vector<string> names;
  for (int i = (0); i < (n); ++i) {
    string s;
    scanf("%s", buf);
    s = buf;
    if (s == "HBox") {
      scanf("%s", buf);
      s = buf;
      M[s] = it;
      names.push_back(s);
      tp.push_back(0);
      w.push_back(0);
      h.push_back(0);
      border.push_back(0);
      space.push_back(0);
      it++;
      continue;
    }
    if (s == "VBox") {
      scanf("%s", buf);
      s = buf;
      M[s] = it;
      names.push_back(s);
      tp.push_back(1);
      w.push_back(0);
      h.push_back(0);
      border.push_back(0);
      space.push_back(0);
      it++;
      continue;
    }
    if (s == "Widget") {
      scanf("%s", buf);
      s = buf;
      string nm = "";
      int j = 0;
      while (s[j] != '(') {
        nm.push_back(s[j]);
        j++;
      }
      M[nm] = it;
      names.push_back(nm);
      long long W, H;
      W = H = 0;
      j++;
      while (s[j] != ',') {
        W = W * 10 + (s[j] - '0');
        j++;
      }
      j++;
      while (s[j] != ')') {
        H = H * 10 + (s[j] - '0');
        j++;
      }
      tp.push_back(2);
      w.push_back(W);
      h.push_back(H);
      border.push_back(0);
      space.push_back(0);
      it++;
      continue;
    }
    for (int j = (0); j < (int((s).size())); ++j)
      if (s[j] == ',' || s[j] == '(' || s[j] == '.' || s[j] == ')') s[j] = ' ';
    stringstream sin;
    sin << s;
    vector<string> q;
    while (sin >> s) q.push_back(s);
    if (q[1] == "pack") {
      g[M[q[0]]].push_back(M[q[2]]);
    } else {
      long long t = 0;
      for (int j = (0); j < (int((q[2]).size())); ++j)
        t = t * 10 + (q[2][j] - '0');
      if (q[1] == "set_border")
        border[M[q[0]]] = t;
      else
        space[M[q[0]]] = t;
    }
  }
  used.resize(it, false);
  for (int i = (0); i < (it); ++i) {
    if (!used[i]) dfs(i);
  }
  vector<pair<string, int> > res;
  for (int i = (0); i < (it); ++i) res.push_back(make_pair(names[i], i));
  sort((res).begin(), (res).end());
  for (int i = (0); i < (it); ++i) {
    int j = res[i].second;
    cout << names[j] << " " << w[j] << " " << h[j] << "\n";
  }
  return 0;
}
