#include <bits/stdc++.h>
using namespace std;
map<string, long long> width, height, border, spacing, type, done;
map<string, vector<string> > packed;
set<pair<string, pair<long long, long long> > > globalAns;
set<string> widgets;
pair<long long, long long> go(string widget) {
  if (done[widget]) return make_pair(width[widget], height[widget]);
  done[widget] = 1;
  if (type[widget] == -1) return make_pair(width[widget], height[widget]);
  long long w = 0, h = 0;
  if (packed[widget].size() == 0) return make_pair(0, 0);
  for (long long i = 0; i < packed[widget].size(); i++) {
    pair<long long, long long> x = go(packed[widget][i]);
    if (type[widget] == 0) {
      w += x.first;
      h = max(h, x.second);
    } else {
      h += x.second;
      w = max(w, x.first);
    }
  }
  long long n = packed[widget].size();
  if (type[widget] == 0)
    w += spacing[widget] * (n - 1);
  else
    h += spacing[widget] * (n - 1);
  w += 2 * border[widget], h += 2 * border[widget];
  width[widget] = w, height[widget] = h;
  return make_pair(w, h);
}
string readName(char delim) {
  string ans = "";
  char c;
  do {
    scanf("%c", &c);
    if (c != delim) ans += c;
  } while (c != delim);
  return ans;
}
long long readNum(char delim) {
  long long res = 0;
  char c;
  do {
    scanf("%c", &c);
    if (c != delim) res = res * 10 + (c - '0');
  } while (c != delim);
  return res;
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    string cmd;
    cin >> cmd;
    string buf;
    if (cmd == "Widget") {
      char c;
      scanf("%c", &c);
      string name = readName('(');
      long long w = readNum(','), h = readNum(')');
      width[name] = w, height[name] = h;
      widgets.insert(name);
      type[name] = -1;
      continue;
    }
    if (cmd == "HBox") {
      cin >> buf;
      border[buf] = spacing[buf] = type[buf] = 0;
      widgets.insert(buf);
      continue;
    }
    if (cmd == "VBox") {
      cin >> buf;
      border[buf] = spacing[buf] = 0;
      type[buf] = 1;
      widgets.insert(buf);
      continue;
    }
    string name = "";
    long long pos = 0;
    for (; cmd[pos] != '.'; pos++) name += cmd[pos];
    pos++;
    string act = "";
    for (; cmd[pos] != '('; pos++) act += cmd[pos];
    pos++;
    if (act == "pack") {
      string name2 = "";
      for (; cmd[pos] != ')'; pos++) name2 += cmd[pos];
      packed[name].push_back(name2);
      continue;
    }
    long long proc = 0;
    for (; cmd[pos] != ')'; pos++) proc = proc * 10 + (cmd[pos] - '0');
    if (act == "set_border") {
      border[name] = proc;
    } else {
      spacing[name] = proc;
    }
  }
  for (set<string>::iterator it = widgets.begin(); it != widgets.end(); it++) {
    globalAns.insert(make_pair((*it), go(*it)));
  }
  for (set<pair<string, pair<long long, long long> > >::iterator it =
           globalAns.begin();
       it != globalAns.end(); it++)
    cout << (*it).first << " " << (*it).second.first << " "
         << (*it).second.second << "\n";
  return 0;
}
