#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:65777216")
using namespace std;
pair<long long, long long> GETS(string name);
struct W {
  string name;
  int h, w;
  int type;
  int b, sp;
  vector<string> q;
  bool calc;
  long long width;
  long long height;
  W(string s) {
    stringstream ss(s);
    ss >> name >> w >> h;
    type = -1;
    b = sp = 0;
    calc = 0;
  }
  W() {
    type = -1;
    b = sp = 0;
    calc = 0;
  }
  pair<long long, long long> getS() {
    if (calc) return pair<long long, long long>(width, height);
    width = height = 0;
    calc = 1;
    if (type == 1) {
      if (q.size() == 0) return pair<long long, long long>(0, 0);
      width += 2 * b;
      width += sp * (q.size() - 1);
      for (int i(0); i < (q.size()); i++) {
        pair<long long, long long> x = GETS(q[i]);
        width += x.first;
        height = max(height, x.second);
      }
      height += 2 * b;
    } else if (type == 2) {
      if (q.size() == 0) return pair<long long, long long>(0, 0);
      height += 2 * b;
      height += sp * (q.size() - 1);
      for (int i(0); i < (q.size()); i++) {
        pair<long long, long long> x = GETS(q[i]);
        height += x.second;
        width = max(width, x.first);
      }
      width += 2 * b;
    } else
      width = w, height = h;
    return pair<long long, long long>(width, height);
  }
};
int n;
map<string, W> w;
pair<long long, long long> GETS(string name) { return w[name].getS(); }
int main() {
  cin >> n;
  string s;
  getline(cin, s);
  for (int tc(0); tc < (n); tc++) {
    getline(cin, s);
    if (s.size() >= 7 && s.substr(0, 7) == "Widget ") {
      s = s.substr(7);
      for (int i(0); i < (s.size()); i++)
        if (s[i] == ',' || s[i] == '(' || s[i] == ')') s[i] = ' ';
      W wid(s);
      w[wid.name] = wid;
    } else if (s.size() >= 5 && s.substr(0, 5) == "HBox ") {
      s = s.substr(5);
      stringstream ss(s);
      ss >> s;
      W wid;
      wid.name = s;
      wid.type = 1;
      w[wid.name] = wid;
    } else if (s.size() >= 5 && s.substr(0, 5) == "VBox ") {
      s = s.substr(5);
      stringstream ss(s);
      ss >> s;
      W wid;
      wid.name = s;
      wid.type = 2;
      w[wid.name] = wid;
    } else {
      for (int i(0); i < (s.size()); i++)
        if (s[i] == '.' || s[i] == '(' || s[i] == ')') s[i] = ' ';
      stringstream ss(s);
      string name;
      ss >> name;
      ss >> s;
      if (s == "pack") {
        ss >> s;
        w[name].q.push_back(s);
      } else if (s == "set_border") {
        int x;
        ss >> x;
        w[name].b = x;
      } else if (s == "set_spacing") {
        int x;
        ss >> x;
        w[name].sp = x;
      }
    }
  }
  vector<pair<string, pair<long long, long long> > > res;
  for (map<string, W>::iterator it = w.begin(); it != w.end(); it++) {
    W x = it->second;
    pair<long long, long long> d = x.getS();
    res.push_back(make_pair(it->first, d));
  }
  sort((res).begin(), (res).end());
  for (int i(0); i < (res.size()); i++) {
    cout << res[i].first << ' ' << res[i].second.first << ' '
         << res[i].second.second << endl;
  }
  return 0;
}
