#include <bits/stdc++.h>
using namespace std;
map<string, pair<long long, long long> > w, bs;
map<string, vector<string> > pck;
map<string, int> tp, vis;
pair<long long, long long> go(string s) {
  if (vis.count(s)) return w[s];
  vis[s] = 1;
  bool in = 0;
  pair<long long, long long> res(0, 0);
  for (int i = 0; i < pck[s].size(); i++) {
    in = 1;
    pair<long long, long long> tmp = go(pck[s][i]);
    if (tp[s] == 1) {
      if (i) res.first += bs[s].second;
      res.first += tmp.first;
      res.second = max(res.second, tmp.second);
    } else {
      if (i) res.second += bs[s].second;
      res.first = max(res.first, tmp.first);
      res.second += tmp.second;
    }
  }
  if (in) {
    res.first += 2 * bs[s].first;
    res.second += 2 * bs[s].first;
  }
  return w[s] = res;
}
int main() {
  int n;
  while (cin >> n) {
    cin.ignore();
    w.clear();
    bs.clear();
    pck.clear();
    tp.clear();
    vis.clear();
    while (n--) {
      string s;
      getline(cin, s);
      if (s.substr(0, 7) == "Widget ") {
        string name;
        int i;
        for (i = 7; s[i] != '('; i++) name += s[i];
        long long x = 0, y = 0;
        i++;
        while (s[i] != ',') x = x * 10 + s[i] - '0', i++;
        i++;
        while (s[i] != ')') y = y * 10 + s[i] - '0', i++;
        w[name] = {x, y};
        bs[name] = {0, 0};
        tp[name] = 0;
        vis[name] = 1;
      }
      if (s.substr(0, 5) == "HBox " || s.substr(0, 5) == "VBox ") {
        w[s.substr(5, s.size() - 5)] = {0, 0};
        tp[s.substr(5, s.size() - 5)] = (s[0] == 'H' ? 1 : 2);
      }
      int tmp = s.find(".pack(");
      if (tmp != -1) {
        string name, in;
        for (int i = 0; i < tmp; i++) name += s[i];
        for (int i = tmp + 6; s[i] != ')'; i++) in += s[i];
        pck[name].push_back(in);
      }
      tmp = s.find(".set_border(");
      if (tmp != -1) {
        string name;
        long long in = 0;
        for (int i = 0; i < tmp; i++) name += s[i];
        for (int i = tmp + 12; s[i] != ')'; i++) in = in * 10 + s[i] - '0';
        bs[name].first = in;
      }
      tmp = s.find(".set_spacing(");
      if (tmp != -1) {
        string name;
        long long in = 0;
        for (int i = 0; i < tmp; i++) name += s[i];
        for (int i = tmp + 13; s[i] != ')'; i++) in = in * 10 + s[i] - '0';
        bs[name].second = in;
      }
    }
    for (map<string, pair<long long, long long> >::iterator it = w.begin();
         it != w.end(); it++) {
      it->second = go(it->first);
      cout << it->first << ' ' << it->second.first << ' ' << it->second.second
           << endl;
    }
  }
  return 0;
}
