#include <bits/stdc++.h>
using namespace std;
int n;
map<string, int> mp;
int cnt = 0;
long long w[200], h[200], par[200], space[200], border[200];
char type[200];
bool mark[200];
string cmd, name1, name2, other;
vector<int> child[200];
void dfs(int a) {
  mark[a] = true;
  if (child[a].size() == 0) return;
  for (int i = 0; i < child[a].size(); i++) {
    int id = child[a][i];
    if (!mark[id]) dfs(id);
    if (type[a] == 'V') {
      w[a] = max(w[a], w[id]);
      h[a] += h[id] + space[a];
    }
    if (type[a] == 'H') {
      h[a] = max(h[a], h[id]);
      w[a] += w[id] + space[a];
    }
  }
  w[a] += 2 * border[a] - (type[a] == 'H' ? space[a] : 0),
      h[a] += 2 * border[a] - (type[a] == 'V' ? space[a] : 0);
}
int main() {
  memset(par, -1, sizeof par);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    if (cmd == "Widget") {
      cin >> other;
      for (int j = 0; j < other.length(); j++)
        if (other[j] == '(' || other[j] == ')' || other[j] == ',')
          other[j] = ' ';
      istringstream sin(other);
      sin >> name1;
      mp[name1] = cnt;
      sin >> w[cnt] >> h[cnt];
      mark[cnt] = true;
      cnt++;
    } else if (cmd == "VBox" || cmd == "HBox") {
      cin >> name1;
      mp[name1] = cnt;
      type[cnt] = cmd[0];
      cnt++;
    } else {
      other = cmd;
      for (int j = 0; j < other.length(); j++)
        if (other[j] == '(' || other[j] == ')' || other[j] == ',' ||
            other[j] == '.')
          other[j] = ' ';
      istringstream sin(other);
      sin >> name1 >> cmd;
      if (cmd == "pack") {
        sin >> name2;
        child[mp[name1]].push_back(mp[name2]);
      } else if (cmd == "set_border") {
        sin >> border[mp[name1]];
      } else {
        sin >> space[mp[name1]];
      }
    }
  }
  for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) {
    int id = it->second;
    if (!mark[id]) dfs(id);
    cout << it->first << ' ' << w[id] << ' ' << h[id] << endl;
  }
  return 0;
}
