#include <bits/stdc++.h>
using namespace std;
map<string, int> type;
map<string, vector<string>> inner;
map<string, pair<long long, long long>> params;
map<string, pair<long long, long long>> sz;
set<string> outer;
void calc_sz(string name) {
  if (sz.count(name)) return;
  if (type[name] == 0)
    sz[name] = params[name];
  else if (type[name] == 1) {
    long long total_x = 0, max_y = 0;
    if (inner[name].empty()) {
      sz[name] = {0, 0};
      return;
    }
    for (auto iname : inner[name]) {
      calc_sz(iname);
      total_x += sz[iname].first;
      max_y = max(max_y, sz[iname].second);
    }
    sz[name] = {2 * params[name].first +
                    (inner[name].size() - 1) * params[name].second + total_x,
                2 * params[name].first + max_y};
  } else {
    long long total_y = 0, max_x = 0;
    if (inner[name].empty()) {
      sz[name] = {0, 0};
      return;
    }
    for (auto iname : inner[name]) {
      calc_sz(iname);
      total_y += sz[iname].second;
      max_x = max(max_x, sz[iname].first);
    }
    sz[name] = {2 * params[name].first + max_x,
                2 * params[name].first +
                    (inner[name].size() - 1) * params[name].second + total_y};
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string s;
  getline(cin, s);
  for (int _ = 0; _ < (int)(n); _++) {
    getline(cin, s);
    if (find(s.begin(), s.end(), '.') != s.end()) {
      string name;
      string cmd;
      string args;
      int i = 0;
      for (; s[i] != '.'; i++) name.push_back(s[i]);
      i++;
      for (; s[i] != '('; i++) cmd.push_back(s[i]);
      i++;
      for (; s[i] != ')'; i++) args.push_back(s[i]);
      if (cmd == "pack") {
        inner[name].push_back(args);
        outer.erase(args);
      } else if (cmd == "set_border") {
        int x;
        istringstream second(args);
        second >> x;
        params[name].first = x;
      } else if (cmd == "set_spacing") {
        int x;
        istringstream second(args);
        second >> x;
        params[name].second = x;
      }
    } else {
      int i = 0;
      string typ;
      string name;
      for (; s[i] != ' '; i++) typ.push_back(s[i]);
      i++;
      for (; i < (int)s.size() && s[i] != '('; i++) name.push_back(s[i]);
      outer.insert(name);
      if (typ == "Widget") {
        string xs, ys;
        i++;
        for (; s[i] != ','; i++) xs.push_back(s[i]);
        i++;
        for (; s[i] != ')'; i++) ys.push_back(s[i]);
        int x, y;
        istringstream xss(xs), yss(ys);
        xss >> x;
        yss >> y;
        type[name] = 0;
        params[name] = {x, y};
      } else {
        type[name] = (typ == "HBox" ? 1 : 2);
        params[name] = {0, 0};
      }
    }
  }
  for (auto name : outer) {
    calc_sz(name);
  }
  for (auto p : type) {
    cout << p.first << " " << sz[p.first].first << " " << sz[p.first].second
         << endl;
  }
  return 0;
}
