#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
map<string, int> mp;
map<string, int>::iterator it;
int kind[(5000 + 10)];
long long x[(5000 + 10)], y[(5000 + 10)], bor[(5000 + 10)], spa[(5000 + 10)];
bool vis[(5000 + 10)];
int num[(5000 + 10)];
int cnt;
string s;
int fst[(5000 + 10)], nxt[(200000 + 10)], vv[(200000 + 10)], e;
void init() {
  memset(fst, -1, sizeof fst);
  e = 0;
}
void add(int u, int v) { vv[e] = v, nxt[e] = fst[u], fst[u] = e++; }
int getnum(string s, int &i) {
  int ret = 0;
  while (!('0' <= s[i] && s[i] <= '9')) ++i;
  ret = s[i] - '0';
  ++i;
  while ('0' <= s[i] && s[i] <= '9') ret = ret * 10 + s[i] - '0', ++i;
  return ret;
}
void dfs(int u, int p) {
  vis[u] = 1;
  num[u] = 0;
  for (int i = fst[u]; ~i; i = nxt[i]) {
    int v = vv[i];
    if (v == p) continue;
    if (!vis[v]) dfs(v, u);
    num[u]++;
    if (kind[u] == 0) {
      x[u] += x[v];
      y[u] = max(y[u], y[v]);
    } else if (kind[u] == 1) {
      x[u] = max(x[u], x[v]);
      y[u] += y[v];
    }
  }
  if (kind[u] == 2) return;
  x[u] += 2 * bor[u];
  y[u] += 2 * bor[u];
  if (kind[u] == 0)
    x[u] += (num[u] - 1) * spa[u];
  else if (kind[u] == 1)
    y[u] += (num[u] - 1) * spa[u];
  if (num[u] == 0) x[u] = y[u] = 0;
}
int main() {
  init();
  int n;
  scanf("%d", &n);
  while (n--) {
    cin >> s;
    if (s == "Widget") {
      cin >> s;
      int i;
      string name;
      for (i = 0; i < s.length(); ++i)
        if (s[i] == '(')
          break;
        else
          name += s[i];
      if (!mp.count(name)) {
        mp[name] = ++cnt;
      }
      int u = mp[name];
      x[u] = getnum(s, i);
      y[u] = getnum(s, i);
      kind[u] = 2;
    } else if (s == "HBox") {
      cin >> s;
      if (!mp.count(s)) {
        mp[s] = ++cnt;
      }
      int u = mp[s];
      kind[u] = 0;
    } else if (s == "VBox") {
      cin >> s;
      if (!mp.count(s)) {
        mp[s] = ++cnt;
      }
      int u = mp[s];
      kind[u] = 1;
    } else {
      string name;
      int i;
      for (i = 0; i < s.length(); ++i) {
        if (s[i] == '.') break;
        name += s[i];
      }
      ++i;
      ;
      int u = mp[name];
      if (s[i] == 'p') {
        string name1;
        for (; i < s.length(); ++i)
          if (s[i] == '(') break;
        ++i;
        for (; i < s.length(); ++i) {
          if (s[i] == ')') break;
          name1 += s[i];
        }
        int v = mp[name1];
        add(u, v);
      } else {
        if (s[i + 4] == 'b') {
          bor[u] = getnum(s, i);
        } else {
          spa[u] = getnum(s, i);
        }
      }
    }
  }
  for (int i = 1; i <= cnt; ++i) {
    if (!vis[i]) {
      dfs(i, -1);
    }
  }
  for (it = mp.begin(); it != mp.end(); ++it) {
    int u = it->second;
    cout << it->first;
    printf(" %I64d %I64d\n", x[u], y[u]);
  }
}
