#include <bits/stdc++.h>
using std::abs;
using std::bitset;
using std::cerr;
using std::cin;
using std::cout;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::set;
using std::sort;
using std::string;
using std::swap;
using std::unordered_map;
using std::unordered_set;
using std::vector;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T>
void _dbg(const char* s, T h) {
  cerr << s << " = " << h << "\n";
}
template <typename T, typename... Ts>
void _dbg(const char* s, T h, Ts... t) {
  int b = 0;
  while (((b += *s == '(') -= *s == ')') != 0 || *s != ',') cerr << *s++;
  cerr << " = " << h << ",";
  _dbg(s + 1, t...);
}
struct init {
  init() {
    cin.tie(0);
    std::iostream::sync_with_stdio(0);
    cout << std::fixed << std::setprecision(10);
    cerr << std::fixed << std::setprecision(5);
  }
  ~init() {}
} init;
template <typename T, typename U>
void upx(T& x, U y) {
  if (x < y) x = y;
}
template <typename T, typename U>
void upn(T& x, U y) {
  if (x > y) x = y;
}
const ll N = 111;
string name[N];
vector<ll> g[N];
ll b[N], second[N], t[N], x[N], y[N], u[N], batya[N];
vector<pair<string, pll>> ans;
void dfs(ll v) {
  ;
  u[v] = 1;
  if (t[v] == 1) {
    if (g[v].empty())
      x[v] = y[v] = 0;
    else {
      x[v] = 2 * b[v] + (g[v].size() - 1) * second[v];
      y[v] = 0;
      for (ll to : g[v]) {
        if (!u[to]) dfs(to);
        x[v] += x[to];
        upx(y[v], y[to]);
      }
      y[v] += 2 * b[v];
    }
  } else if (t[v] == 2) {
    if (g[v].empty())
      x[v] = y[v] = 0;
    else {
      y[v] = 2 * b[v] + (g[v].size() - 1) * second[v];
      x[v] = 0;
      for (ll to : g[v]) {
        if (!u[to]) dfs(to);
        y[v] += y[to];
        upx(x[v], x[to]);
      }
      x[v] += 2 * b[v];
    }
  }
  ans.emplace_back(name[v], pll{x[v], y[v]});
}
map<string, ll> num;
int32_t main() {
  ll q;
  cin >> q;
  ll n = 0;
  string com;
  getline(cin, com);
  while (q--) {
    getline(cin, com);
    if (find(com.begin(), com.end(), ' ') != com.end()) {
      string typ;
      ll i = 0;
      while (com[i] != ' ') typ += com[i++];
      i++;
      if (typ == "Widget") {
        while (com[i] != '(') name[n] += com[i++];
        i++;
        while (com[i] != ',') {
          x[n] *= 10;
          x[n] += com[i++] - '0';
        }
        i++;
        while (com[i] != ')') {
          y[n] *= 10;
          y[n] += com[i++] - '0';
        }
      } else if (typ == "HBox") {
        while (i < com.size()) name[n] += com[i++];
        t[n] = 1;
      } else {
        while (i < com.size()) name[n] += com[i++];
        t[n] = 2;
      }
      num[name[n]] = n;
      batya[n] = 1;
      n++;
    } else {
      string nam;
      ll i = 0;
      while (com[i] != '.') nam += com[i++];
      i++;
      ll v = num[nam];
      string wha;
      while (com[i] != '(') wha += com[i++];
      i++;
      if (wha == "pack") {
        string to;
        while (com[i] != ')') to += com[i++];
        ll z = num[to];
        g[v].emplace_back(z);
        batya[z] = 0;
      } else if (wha == "set_border") {
        b[v] = 0;
        while (com[i] != ')') {
          b[v] *= 10;
          b[v] += com[i++] - '0';
        }
      } else {
        second[v] = 0;
        while (com[i] != ')') {
          second[v] *= 10;
          second[v] += com[i++] - '0';
        }
      }
    }
  }
  for (ll i = 0; i < (n); ++i) {
    if (batya[i] && !u[i]) {
      ;
      dfs(i);
    }
  }
  sort(ans.begin(), ans.end());
  for (auto i : ans)
    cout << i.first << ' ' << i.second.first << ' ' << i.second.second << '\n';
  return 0;
}
