#include <bits/stdc++.h>
using namespace std;
map<string, multiset<string> > Hb;
map<string, multiset<string> > Wb;
map<string, pair<long long, long long> > W;
map<string, pair<long long, long long> > ans;
pair<long long, long long> get_val(string s) {
  if (ans.find(s) != ans.end()) {
    return ans[s];
  }
  pair<long long, long long> ret;
  if (Hb.find(s) != Hb.end()) {
    if (Hb[s].empty()) {
      ret = make_pair(0, 0);
    } else {
      ret = make_pair(2 * W[s].first + (Hb[s].size() - 1) * W[s].second, 0);
      for (multiset<string>::iterator it = Hb[s].begin(); it != Hb[s].end();
           ++it) {
        pair<long long, long long> ans = get_val(*it);
        ret.second = max(ret.second, ans.second);
        ret.first += ans.first;
      }
      ret.second += 2 * W[s].first;
    }
  } else if (Wb.find(s) != Wb.end()) {
    if (Wb[s].empty()) {
      ret = make_pair(0, 0);
    } else {
      ret = make_pair(0, 2 * W[s].first + (Wb[s].size() - 1) * W[s].second);
      for (multiset<string>::iterator it = Wb[s].begin(); it != Wb[s].end();
           ++it) {
        pair<long long, long long> ans = get_val(*it);
        ret.first = max(ret.first, ans.first);
        ret.second += ans.second;
      }
      ret.first += 2 * W[s].first;
    }
  } else {
    ret = W[s];
  }
  return ans[s] = ret;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s1, s2;
    cin >> s1;
    if (s1 == "Widget") {
      cin >> s2;
      s1 = s2.substr(0, s2.find('('));
      s2 = s2.substr(s2.find('('));
      int w, h;
      sscanf(s2.c_str(), "(%d,%d)", &w, &h);
      W[s1] = make_pair(w, h);
    } else if (s1 == "HBox") {
      cin >> s2;
      W[s2] = make_pair(0, 0);
      Hb[s2];
    } else if (s1 == "VBox") {
      cin >> s2;
      W[s2] = make_pair(0, 0);
      Wb[s2];
    } else if (s1.find(".pack") != -1) {
      s2 = s1.substr(0, s1.find('.'));
      s1 = s1.substr(s1.find('(') + 1);
      s1 = s1.substr(0, s1.size() - 1);
      if (Hb.find(s2) != Hb.end()) {
        Hb[s2].insert(s1);
      } else {
        Wb[s2].insert(s1);
      }
    } else if (s1.find(".set_border") != -1) {
      s2 = s1.substr(0, s1.find('.'));
      s1 = s1.substr(s1.find('(') + 1);
      s1 = s1.substr(0, s1.size() - 1);
      int b;
      sscanf(s1.c_str(), "%d", &b);
      W[s2] = make_pair(b, W[s2].second);
    } else {
      s2 = s1.substr(0, s1.find('.'));
      s1 = s1.substr(s1.find('(') + 1);
      s1 = s1.substr(0, s1.size() - 1);
      int b;
      sscanf(s1.c_str(), "%d", &b);
      W[s2] = make_pair(W[s2].first, b);
    }
  }
  for (map<string, pair<long long, long long> >::iterator it = W.begin();
       it != W.end(); ++it) {
    string s1 = it->first;
    pair<long long, long long> ans = get_val(s1);
    cout << s1 << " " << ans.first << " " << ans.second << endl;
  }
  return 0;
}
