#include <bits/stdc++.h>
using namespace std;
const std::string DelimitersString = " \t\n\r";
const std::string Letters =
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
enum AddPattern { NotInPattern = 1, InPattern = 3, Both = 2 };
std::vector<std::string> split_string(
    const std::string& strSource,
    const std::string& strPattern = DelimitersString,
    const AddPattern addPattern = NotInPattern) {
  std::vector<std::string> result;
  if (strSource.length() == 0) return result;
  bool bInPattern = strPattern.find(strSource[0]) != strPattern.npos;
  size_t st = 0;
  for (size_t i = 1; i < strSource.length(); ++i) {
    if ((strPattern.find(strSource[i]) != strPattern.npos) != bInPattern) {
      if ((bInPattern && addPattern >= 2) || (!bInPattern && addPattern <= 2))
        result.push_back(strSource.substr(st, i - st));
      bInPattern = !bInPattern;
      st = i;
    }
  }
  if ((bInPattern && addPattern >= 2) || (!bInPattern && addPattern <= 2))
    result.push_back(strSource.substr(st));
  return result;
}
struct widget {
  bool hbox;
  bool vbox;
  long long w;
  long long h;
  long long border, spacing;
  string name;
  widget() {
    hbox = vbox = false;
    w = h = -1;
    border = spacing = 0;
  };
  vector<widget*> children;
  long long width() {
    if (w != -1) return w;
    if (!vbox && !hbox) {
      return w;
    }
    if (vbox) {
      if (children.size() == 0) return w = 0;
      w = border * 2;
      long long max_w = 0;
      for (int i = 0; i < children.size(); ++i)
        max_w = max<long long>(max_w, children[i]->width());
      w += max_w;
      return w;
    }
    if (hbox) {
      if (children.size() == 0) return w = 0;
      w = border * 2 + spacing * (children.size() - 1);
      long long sum_w = 0;
      for (int i = 0; i < children.size(); ++i) sum_w += children[i]->width();
      w += sum_w;
      return w;
    }
  }
  long long height() {
    if (h != -1) return h;
    if (!vbox && !hbox) {
      return h;
    }
    if (hbox) {
      if (children.size() == 0) return h = 0;
      h = border * 2;
      long long max_h = 0;
      for (int i = 0; i < children.size(); ++i)
        max_h = max<long long>(max_h, children[i]->height());
      h += max_h;
      return h;
    }
    if (vbox) {
      if (children.size() == 0) return h = 0;
      h = border * 2 + spacing * (children.size() - 1);
      long long sum_h = 0;
      for (int i = 0; i < children.size(); ++i) sum_h += children[i]->height();
      h += sum_h;
      return h;
    }
  }
};
int main(int argc, char* argv[]) {
  int n;
  string str;
  getline(cin, str);
  n = atoi(str.c_str());
  map<string, widget*> widgets;
  for (int i = 0; i < (int)(n); ++i) {
    getline(cin, str);
    vector<string> vs = split_string(str, "(),. ");
    if (vs[0] == "Widget") {
      widget* wg = new widget();
      wg->name = vs[1];
      wg->w = atoi(vs[2].c_str());
      wg->h = atoi(vs[3].c_str());
      widgets[wg->name] = wg;
    } else if (vs[0] == "VBox") {
      widget* wg = new widget();
      wg->name = vs[1];
      wg->vbox = true;
      widgets[wg->name] = wg;
    } else if (vs[0] == "HBox") {
      widget* wg = new widget();
      wg->name = vs[1];
      wg->hbox = true;
      widgets[wg->name] = wg;
    } else if (vs[1] == "pack") {
      widgets[vs[0]]->children.push_back(widgets[vs[2]]);
    } else if (vs[1] == "set_border") {
      widgets[vs[0]]->border = atoi(vs[2].c_str());
    } else if (vs[1] == "set_spacing") {
      widgets[vs[0]]->spacing = atoi(vs[2].c_str());
    }
  }
  for (map<string, widget*>::iterator it = widgets.begin(); it != widgets.end();
       ++it) {
    cout << it->second->name << " " << it->second->width() << " "
         << it->second->height() << "\n";
  }
  int temp;
  std::cin >> temp;
}
