#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long linf = (long long)1e18;
const long double pi = acos(-1.0);
const int prime = 748297;
inline void finclude() { srand(time(NULL)); }
inline long long max(long long a, long long b) {
  if (a > b) return a;
  return b;
}
struct state {
  long long x, y, b, s;
  vector<string> par;
  vector<string> child;
};
map<string, state> m;
map<string, int> v;
pair<long long, long long> dfs(string name) {
  map<string, state>::iterator fm = m.find(name);
  map<string, int>::iterator fv = v.find(name);
  if (fv->second == 1)
    return pair<long long, long long>(fm->second.x, fm->second.y);
  if (fm->second.x != 0)
    return pair<long long, long long>(fm->second.x, fm->second.y);
  pair<long long, long long> ans = pair<long long, long long>(0, 0);
  for (int i = 0; i < fm->second.child.size(); ++i) {
    pair<long long, long long> h = dfs(fm->second.child[i]);
    if (fv->second == 2) {
      ans.first = max(ans.first, h.first);
      ans.second += h.second;
    } else {
      ans.first += h.first;
      ans.second = max(ans.second, h.second);
    }
  }
  if (fv->second == 2) {
    fm->second.x = ans.first + 2 * fm->second.b;
    fm->second.y = ans.second + 2 * fm->second.b;
    if (!fm->second.child.empty())
      fm->second.y += (fm->second.child.size() - 1) * fm->second.s;
    else
      fm->second.x = fm->second.y = 0;
  } else {
    fm->second.y = ans.second + 2 * fm->second.b;
    fm->second.x = ans.first + 2 * fm->second.b;
    if (!fm->second.child.empty())
      fm->second.x += (fm->second.child.size() - 1) * fm->second.s;
    else
      fm->second.x = fm->second.y = 0;
  }
  return pair<long long, long long>(fm->second.x, fm->second.y);
}
int main() {
  finclude();
  int n;
  cin >> n;
  string s;
  for (int i = 0; i < n; ++i) {
    cin >> s;
    string h;
    state g;
    g.x = g.y = g.s = g.b = 0;
    if (s == "Widget") {
      cin >> h;
      string p;
      int j = 0;
      long long x = 0;
      while (h[j] != '(') {
        p += h[j];
        ++j;
      }
      ++j;
      while (h[j] != ',') {
        x = x * 10 + h[j] - '0';
        ++j;
      }
      ++j;
      g.x = x;
      x = 0;
      while (h[j] != ')') {
        x = x * 10 + h[j] - '0';
        ++j;
      }
      g.y = x;
      g.b = g.s = 0;
      m.insert(pair<string, state>(p, g));
      v.insert(pair<string, int>(p, 1));
    } else if (s == "VBox") {
      cin >> h;
      m.insert(pair<string, state>(h, g));
      v.insert(pair<string, int>(h, 2));
    } else if (s == "HBox") {
      cin >> h;
      m.insert(pair<string, state>(h, g));
      v.insert(pair<string, int>(h, 3));
    } else {
      int j = 0;
      while (s[j] != '.') {
        h += s[j];
        ++j;
      }
      ++j;
      map<string, state>::iterator ff = m.find(h);
      if (s[j] == 'p') {
        string p;
        j += 5;
        while (s[j] != ')') {
          p += s[j];
          ++j;
        }
        ff->second.child.push_back(p);
      } else if (s[j + 4] == 'b') {
        long long x = 0;
        j += 11;
        while (s[j] != ')') {
          x = x * 10 + s[j] - '0';
          ++j;
        }
        ff->second.b = x;
      } else {
        long long x = 0;
        j += 12;
        while (s[j] != ')') {
          x = x * 10 + s[j] - '0';
          ++j;
        }
        ff->second.s = x;
      }
    }
  }
  for (map<string, state>::iterator it = m.begin(); it != m.end(); ++it)
    pair<long long, long long> g = dfs(it->first);
  for (map<string, state>::iterator it = m.begin(); it != m.end(); ++it)
    cout << it->first << ' ' << it->second.x << ' ' << it->second.y << '\n';
}
