#include <bits/stdc++.h>
using namespace std;
const int N = 5050;
const int M = 200020;
map<string, int> Ma;
map<string, int>::iterator it;
int kind[N];
long long x[N], y[N], bor[N], spa[N];
bool vis[N];
int num[N];
int cnt;
string s;
int ma[N], nxt[M], vv[M], e;
void init() {
  memset(ma, -1, sizeof(ma));
  e = 0;
}
void add(int u, int v) { vv[e] = v, nxt[e] = ma[u], ma[u] = e++; }
int getnum(string s, int &p) {
  int ans;
  ans = 0;
  while (!('0' <= s[p] && s[p] <= '9')) {
    p++;
  }
  ans = s[p] - '0';
  p++;
  while ('0' <= s[p] && s[p] <= '9') {
    ans = ans * 10 + s[p] - '0';
    p++;
  }
  return ans;
}
void dfs(int u, int p) {
  vis[u] = 1;
  num[u] = 0;
  for (int i = ma[u]; ~i; i = nxt[i]) {
    int v;
    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 (!Ma.count(name)) {
        Ma[name] = ++cnt;
      }
      int u = Ma[name];
      x[u] = getnum(s, i);
      y[u] = getnum(s, i);
      kind[u] = 2;
    } else if (s == "HBox") {
      cin >> s;
      if (!Ma.count(s)) {
        Ma[s] = ++cnt;
      }
      int u = Ma[s];
      kind[u] = 0;
    } else if (s == "VBox") {
      cin >> s;
      if (!Ma.count(s)) {
        Ma[s] = ++cnt;
      }
      int u = Ma[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 = Ma[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 = Ma[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 = Ma.begin(); it != Ma.end(); it++) {
    int u = it->second;
    cout << it->first;
    printf(" %lld %lld\n", x[u], y[u]);
  }
}
