#include <bits/stdc++.h>
using namespace std;
struct win {
  enum { HB, VB, W } type;
  unsigned long long w, h;
  unsigned long long border, spacing;
  vector<string> packed;
};
int main(void) {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  cin.ignore();
  map<string, win> table;
  map<string, bool> packed;
  for (unsigned int i = 0U; i < n; ++i) {
    string s;
    getline(cin, s);
    size_t spos = s.find_first_of(' ');
    if (spos == -1) {
      size_t ppos = s.find_first_of('.');
      string box = s.substr(0, ppos);
      size_t lpos = s.find_first_of('(', ppos);
      string command = s.substr(ppos + 1, lpos - ppos - 1);
      if (command == "set_border") {
        unsigned long long val = 0;
        size_t pos = lpos + 1;
        while (pos < s.size()) {
          if (s[pos] == ')') break;
          val = val * 10 + s[pos] - '0';
          ++pos;
        }
        table[box].border = val;
      } else if (command == "set_spacing") {
        unsigned long long val = 0;
        size_t pos = lpos + 1;
        while (pos < s.size()) {
          if (s[pos] == ')') break;
          val = val * 10 + s[pos] - '0';
          ++pos;
        }
        table[box].spacing = val;
      } else {
        size_t rpos = s.find_first_of(')', lpos);
        string wi = s.substr(lpos + 1, rpos - lpos - 1);
        table[box].packed.push_back(wi);
        packed[wi] = true;
      }
    } else {
      string type = s.substr(0, spos);
      if (type == "Widget") {
        size_t lpos = s.find_first_of('(', spos);
        string name = s.substr(spos + 1, lpos - spos - 1);
        unsigned long long x = 0, y = 0;
        int st = 0;
        size_t pos = lpos + 1;
        while (pos < s.size()) {
          if (s[pos] == ')') break;
          if (s[pos] == ',') {
            st = 1;
            ++pos;
            continue;
          }
          if (st)
            y = y * 10 + s[pos] - '0';
          else
            x = x * 10 + s[pos] - '0';
          ++pos;
        }
        win w = {win::W, x, y, 0, 0};
        packed[name] = false;
        table.insert(make_pair(name, w));
      } else {
        string name = s.substr(spos + 1);
        win w = {type == "HBox" ? win::HB : win::VB};
        packed[name] = false;
        table.insert(make_pair(name, w));
      }
    }
  }
  map<string, bool> checked;
  std::function<void(const string &)> func = [&](const string &s) {
    if (checked[s]) return;
    win &target = table[s];
    if (target.type == win::W) return;
    if (target.type == win::HB) {
      if (target.packed.size() == 0) {
        target.w = target.h = 0;
        return;
      }
      unsigned long long maxh = 0, sumw = 0;
      for (auto &v : target.packed) {
        func(v);
        maxh = max(maxh, table[v].h);
        sumw += table[v].w;
      }
      target.w = sumw + target.border * 2 +
                 target.spacing * (target.packed.size() - 1);
      target.h = maxh + target.border * 2;
    } else {
      if (target.packed.size() == 0) {
        target.w = target.h = 0;
        return;
      }
      unsigned long long maxw = 0, sumh = 0;
      for (auto &v : target.packed) {
        func(v);
        maxw = max(maxw, table[v].w);
        sumh += table[v].h;
      }
      target.h = sumh + target.border * 2 +
                 target.spacing * (target.packed.size() - 1);
      target.w = maxw + target.border * 2;
    }
    checked[s] = true;
  };
  vector<string> key;
  for (auto it = packed.begin(); it != packed.end(); ++it) {
    key.push_back(it->first);
    if (packed[it->first]) continue;
    func(it->first);
  }
  sort((key).begin(), (key).end());
  for (auto &v : key) {
    cout << v << ' ' << table[v].w << ' ' << table[v].h << endl;
  }
  return 0;
}
