#include <bits/stdc++.h>
using namespace std;
char buf[1024];
map<string, int> type;
map<string, long long> dimx, dimy, border, spacing;
map<string, vector<string> > contain;
map<string, pair<long long, long long> > dp;
pair<long long, long long> calcdim(string s) {
  if (dp.count(s)) return dp[s];
  int t = type[s];
  long long x, y;
  vector<string> &v = contain[s];
  int m = v.size();
  long long mx = 0;
  long long sum = 0;
  if (t == 0) {
    x = dimx[s];
    y = dimy[s];
  } else if (t == 1) {
    if (m == 0)
      x = y = 0;
    else {
      for (int i = 0; i < m; i++) {
        pair<long long, long long> p = calcdim(v[i]);
        mx = max(mx, p.second);
        sum += p.first;
      }
      y = mx + 2 * border[s];
      x = sum + (m - 1) * spacing[s] + 2 * border[s];
    }
  } else {
    if (m == 0)
      x = y = 0;
    else {
      for (int i = 0; i < m; i++) {
        pair<long long, long long> p = calcdim(v[i]);
        mx = max(mx, p.first);
        sum += p.second;
      }
      x = mx + 2 * border[s];
      y = sum + (m - 1) * spacing[s] + 2 * border[s];
    }
  }
  return dp[s] = pair<long long, long long>(x, y);
}
int main() {
  int T;
  scanf("%d\n", &T);
  while (T--) {
    string s;
    getline(cin, s);
    for (int i = 0; i < s.length(); i++)
      if (s[i] == '.' || s[i] == '(' || s[i] == ')' || s[i] == ',') s[i] = ' ';
    stringstream ss(s);
    string t, name;
    ss >> t;
    if (t == "Widget") {
      int x, y;
      ss >> name >> x >> y;
      type[name] = 0;
      dimx[name] = x;
      dimy[name] = y;
    } else if (t == "HBox") {
      ss >> name;
      type[name] = 1;
      border[name] = 0;
      spacing[name] = 0;
      contain[name] = vector<string>();
    } else if (t == "VBox") {
      ss >> name;
      type[name] = 2;
      border[name] = 0;
      spacing[name] = 0;
      contain[name] = vector<string>();
    } else {
      string method, name2;
      int x;
      name = t;
      ss >> method;
      if (method == "pack") {
        ss >> name2;
        contain[name].push_back(name2);
      } else if (method == "set_border") {
        ss >> x;
        border[name] = x;
      } else {
        ss >> x;
        spacing[name] = x;
      }
    }
  }
  for (map<string, int>::iterator it = type.begin(); it != type.end(); it++) {
    string name = it->first;
    int t = it->second;
    pair<long long, long long> p = calcdim(name);
    cout << name << ' ' << p.first << ' ' << p.second << endl;
  }
}
