#include <bits/stdc++.h>
using namespace std;
string a, b, p1 = "Widget", p2 = "VBox", p3 = "HBox", ans[105];
map<string, int> q;
vector<int> d[105];
int id[105];
long long l[105], r[105], h1[105], h2[105];
void dfs(int x) {
  if (l[x] != -1) return;
  l[x] = 0;
  for (int i = 0; i < (int)d[x].size(); ++i) {
    int v = d[x][i];
    dfs(v);
    if (id[x] == 2) {
      l[x] += l[v];
      r[x] = max(r[x], r[v]);
    } else {
      r[x] += r[v];
      l[x] = max(l[x], l[v]);
    }
  }
  if (d[x].size()) {
    if (id[x] == 2) {
      l[x] = l[x] + (d[x].size() - 1) * h2[x];
      r[x] += 2 * h1[x];
      l[x] += 2 * h1[x];
    } else {
      r[x] = r[x] + (d[x].size() - 1) * h2[x];
      l[x] += 2 * h1[x];
      r[x] += 2 * h1[x];
    }
  }
}
int main() {
  memset(l, -1, sizeof(l));
  int n, tot = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    cin >> a;
    if (a == p1) {
      cin >> a;
      b = "";
      for (int j = 0, k; j < (int)a.size(); ++j) {
        if (a[j] == '(') {
          q[b] = ++tot;
          ans[tot] = b;
          l[tot] = 0;
          for (k = j + 1;; ++k) {
            if (a[k] == ',') break;
            l[tot] = l[tot] * 10 + a[k] - '0';
          }
          for (++k;; ++k) {
            if (a[k] == ')') break;
            r[tot] = r[tot] * 10 + a[k] - '0';
          }
          break;
        }
        b += a[j];
      }
    } else if (a == p2) {
      cin >> a;
      q[a] = ++tot;
      ans[tot] = a;
      id[tot] = 1;
    } else if (a == p3) {
      cin >> a;
      q[a] = ++tot;
      ans[tot] = a;
      id[tot] = 2;
    } else {
      string c = "";
      for (int j = 0; j < (int)a.size(); ++j) {
        if (a[j] == '.') {
          if (a[j + 1] == 'p') {
            b = "";
            for (int k = j + 6; k < (int)a.size() - 1; ++k) b += a[k];
            d[q[c]].push_back(q[b]);
          } else {
            long long tp = 0;
            for (int k = j + 1; k < (int)a.size(); ++k)
              if (a[k] >= '0' && a[k] <= '9') tp = tp * 10 + a[k] - '0';
            if (a[j + 5] == 'b')
              h1[q[c]] = tp;
            else
              h2[q[c]] = tp;
          }
          break;
        }
        c += a[j];
      }
    }
  }
  sort(ans + 1, ans + tot + 1);
  for (int i = 1; i <= tot; ++i) {
    dfs(q[ans[i]]);
    cout << ans[i] << " ";
    printf("%I64d %I64d\n", l[q[ans[i]]], r[q[ans[i]]]);
  }
}
