#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
pair<string, pair<long long, long long>> get_pr(string s) {
  long long j = 0;
  string name;
  while (s[j] != '(') {
    name.push_back(s[j++]);
  }
  j++;
  long long x = 0;
  while (s[j] != ',') {
    x = x * 10 + (s[j++] - '0');
  }
  j++;
  long long y = 0;
  while (s[j] != ')') {
    y = y * 10 + (s[j++] - '0');
  }
  return make_pair(name, make_pair(x, y));
}
signed main() {
  long long n;
  cin >> n;
  long long cur_mx = 1;
  map<string, long long> x, y, type, len, border, spacing;
  map<string, vector<string>> lol;
  vector<string> names;
  for (long long i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "Widget") {
      cin >> s;
      auto kek = get_pr(s);
      x[kek.first] = kek.second.first;
      y[kek.first] = kek.second.second;
      type[kek.first] = 0;
      names.push_back(kek.first);
    } else if (s == "HBox") {
      cin >> s;
      type[s] = 1;
      names.push_back(s);
    } else if (s == "VBox") {
      cin >> s;
      type[s] = 2;
      names.push_back(s);
    } else {
      string name1;
      long long j = 0;
      while (s[j] != '.') {
        name1.push_back(s[j++]);
      }
      j++;
      if (s[j] == 'p') {
        while (s[j - 1] != '(') {
          j++;
        }
        string name2;
        while (s[j] != ')') {
          name2.push_back(s[j++]);
        }
        lol[name1].push_back(name2);
        len[name1] = max(len[name1], len[name2] + 1);
        cur_mx = max(cur_mx, len[name1]);
      } else {
        while (s[j - 1] != '_') j++;
        if (s[j] == 'b') {
          while (s[j - 1] != '(') {
            j++;
          }
          long long vl = 0;
          while (s[j] != ')') {
            vl = vl * 10 + (s[j++] - '0');
          }
          border[name1] = vl;
        } else {
          while (s[j - 1] != '(') {
            j++;
          }
          long long vl = 0;
          while (s[j] != ')') {
            vl = vl * 10 + (s[j++] - '0');
          }
          spacing[name1] = vl;
        }
      }
      for (long long j = 1; j <= 100; j++) {
        for (string v : names) {
          if (type[v] == 1) {
            x[v] = y[v] = 0;
            for (string u : lol[v]) {
              x[v] += x[u];
              y[v] = max(y[v], y[u]);
            }
          }
          if (type[v] == 2) {
            x[v] = y[v] = 0;
            for (string u : lol[v]) {
              x[v] = max(x[v], x[u]);
              y[v] += y[u];
            }
          }
          if (!lol[v].empty()) {
            x[v] += 2 * border[v];
            y[v] += 2 * border[v];
            if (type[v] == 1) {
              x[v] += spacing[v] * ((long long)(lol[v]).size() - 1);
            } else {
              y[v] += spacing[v] * ((long long)(lol[v]).size() - 1);
            }
          }
        }
      }
    }
  }
  sort((names).begin(), (names).end());
  for (auto it : names) {
    cout << it << ' ' << x[it] << ' ' << y[it] << '\n';
  }
  cout << '\n';
}
