#include <bits/stdc++.h>
using namespace std;
struct widget {
  char type;
  long long w, h, spacing, border;
  vector<int> sons;
  widget() {}
  widget(char type, long long w = 0, long long h = 0)
      : type(type), w(w), h(h), spacing(0), border(0) {}
};
map<string, int> id;
vector<widget> wgt;
vector<bool> used;
void dfs(int &v) {
  used[v] = true;
  for (vector<int>::iterator it = wgt[v].sons.begin(); it != wgt[v].sons.end();
       it++) {
    if (!used[*it]) dfs(*it);
    if (wgt[v].type == 'V') {
      wgt[v].w = max(wgt[v].w, wgt[*it].w);
      wgt[v].h += wgt[*it].h;
    } else {
      wgt[v].h = max(wgt[v].h, wgt[*it].h);
      wgt[v].w += wgt[*it].w;
    }
  }
  if (!wgt[v].sons.empty()) {
    if (wgt[v].type == 'V') {
      wgt[v].w += wgt[v].border * 2;
      wgt[v].h += wgt[v].border * 2 + wgt[v].spacing * (wgt[v].sons.size() - 1);
    } else {
      wgt[v].h += wgt[v].border * 2;
      wgt[v].w += wgt[v].border * 2 + wgt[v].spacing * (wgt[v].sons.size() - 1);
    }
  }
}
int main() {
  int n;
  int namecount = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "HBox" || s == "VBox") {
      string name;
      cin >> name;
      id[name] = namecount++;
      wgt.push_back(widget(s[0]));
    } else if (s == "Widget") {
      string name;
      getchar();
      getline(cin, name, '(');
      int w, h;
      scanf("%d,%d)", &w, &h);
      id[name] = namecount++;
      wgt.push_back(widget('W', w, h));
    } else {
      stringstream ss(s);
      string to;
      getline(ss, to, '.');
      string task;
      getline(ss, task, '(');
      if (task == "pack") {
        string what;
        getline(ss, what, ')');
        wgt[id[to]].sons.push_back(id[what]);
      } else if (task == "set_border") {
        string bdr;
        getline(ss, bdr, ')');
        wgt[id[to]].border = atoi(bdr.c_str());
      } else {
        string sng;
        getline(ss, sng, ')');
        wgt[id[to]].spacing = atoi(sng.c_str());
      }
    }
  }
  used.resize(namecount, false);
  for (int i = 0; i < namecount; i++)
    if (!used[i]) dfs(i);
  for (map<string, int>::iterator it = id.begin(); it != id.end(); it++)
    cout << it->first << " " << wgt[it->second].w << " " << wgt[it->second].h
         << endl;
  return 0;
}
