#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int MAX = 2e3 + 5;
const long long MAX2 = 11;
const long long MOD = 998244353;
const long long MOD2 = 1000005329;
const long long INF = 2e18;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 2000;
int q, x, y, cnt;
map<string, pair<long long, long long>> ans, dp;
map<string, bool> jen;
map<string, vector<string>> isi;
string s, ty, c;
pair<long long, long long> solve(string o) {
  if (!jen.count(o)) return ans[o];
  if (isi[o].empty()) return {0, 0};
  if (dp.count(o)) return dp[o];
  int hit = isi[o].size() - 1;
  long long mx = 0, ttl = 0;
  pair<long long, long long> res;
  if (jen[o]) {
    for (auto i : isi[o]) {
      res = solve(i);
      mx = max(mx, res.second);
      ttl += res.first;
    }
    return dp[o] = {ttl + 2 * ans[o].first + hit * ans[o].second,
                    mx + 2 * ans[o].first};
  }
  {
    for (auto i : isi[o]) {
      res = solve(i);
      mx = max(mx, res.first);
      ttl += res.second;
    }
    return dp[o] = {mx + 2 * ans[o].first,
                    ttl + 2 * ans[o].first + hit * ans[o].second};
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> ty;
    x = y = cnt = 0;
    if (ty == "Widget") {
      cin >> s;
      c = "";
      for (auto i : s) {
        if (i == '(' || i == ')' || i == ',')
          ++cnt;
        else if (cnt == 1)
          x = x * 10 + i - '0';
        else if (cnt == 2)
          y = y * 10 + i - '0';
        else
          c.push_back(i);
      }
      ans[c] = {x, y};
    } else if (ty == "VBox") {
      cin >> s;
      ans[s];
      jen[s];
    } else if (ty == "HBox") {
      cin >> s;
      ans[s];
      jen[s] = 1;
    } else {
      s = c = "";
      for (auto i : ty) {
        if (i == '.')
          ++cnt;
        else if (i == '(') {
          if (c == "pack")
            cnt = 2;
          else if (c == "set_border")
            cnt = 3;
          else if (c == "set_spacing")
            cnt = 4;
          c = "";
        } else if (i == ')')
          continue;
        else if (cnt == 0)
          s.push_back(i);
        else if (cnt == 1 || cnt == 2)
          c.push_back(i);
        else if (cnt == 3 || cnt == 4)
          x = x * 10 + i - '0';
        else
          return 1;
      }
      if (cnt == 2)
        isi[s].push_back(c);
      else if (cnt == 3)
        ans[s].first = x;
      else
        ans[s].second = x;
    }
  }
  pair<long long, long long> res;
  for (auto i : ans) {
    res = solve(i.first);
    cout << i.first << ' ' << res.first << ' ' << res.second << '\n';
  }
  return 0;
}
