#include <bits/stdc++.h>
using namespace std;
const int Widget = 0, HBox = 1, VBox = 2;
const int _N = 110;
struct Wtype {
  string name;
  int type;
  long long width, height, border, spacing;
  int subN, sub[_N];
  bool operator<(const Wtype &b) const { return name < b.name; }
};
int n, wN;
Wtype w[_N];
string cmd;
bool vis[_N];
int findWidget(const string &s) {
  for (int i = 0; i < wN; i++)
    if (w[i].name == s) return i;
  return -1;
}
void dfs(int i) {
  vis[i] = true;
  if (w[i].type != Widget) {
    if (w[i].subN == 0) {
      w[i].height = w[i].width = 0;
      return;
    }
    for (int j = 0; j < w[i].subN; j++)
      if (!vis[w[i].sub[j]]) dfs(w[i].sub[j]);
    long long sumHeight = 0, sumWidth = 0, maxHeight = 0, maxWidth = 0;
    for (int j = 0; j < w[i].subN; j++) {
      sumHeight += w[w[i].sub[j]].height;
      sumWidth += w[w[i].sub[j]].width;
      maxHeight = max(maxHeight, w[w[i].sub[j]].height);
      maxWidth = max(maxWidth, w[w[i].sub[j]].width);
    }
    if (w[i].type == HBox) {
      w[i].width = sumWidth + (w[i].subN - 1) * w[i].spacing + w[i].border * 2;
      w[i].height = maxHeight + w[i].border * 2;
    } else {
      w[i].height =
          sumHeight + (w[i].subN - 1) * w[i].spacing + w[i].border * 2;
      w[i].width = maxWidth + w[i].border * 2;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int _n = 0; _n < n; _n++) {
    cin >> cmd;
    if (cmd == "Widget") {
      cin >> cmd;
      int p = cmd.find('(');
      w[wN].name = cmd.substr(0, p);
      w[wN].type = Widget;
      cmd.erase(0, p + 1);
      cmd.erase(cmd.length() - 1, 1);
      int x, y;
      sscanf(cmd.c_str(), "%d,%d", &x, &y);
      w[wN].width = x;
      w[wN].height = y;
      wN++;
    } else if (cmd == "HBox") {
      cin >> w[wN].name;
      w[wN].type = HBox;
      w[wN].border = 0;
      w[wN].spacing = 0;
      w[wN].subN = 0;
      wN++;
    } else if (cmd == "VBox") {
      cin >> w[wN].name;
      w[wN].type = VBox;
      w[wN].border = 0;
      w[wN].spacing = 0;
      w[wN].subN = 0;
      wN++;
    } else {
      int p = cmd.find('.');
      int u = findWidget(cmd.substr(0, p));
      cmd.erase(0, p + 1);
      p = cmd.find('(');
      string subcmd = cmd.substr(0, p);
      cmd.erase(0, p + 1);
      cmd.erase(cmd.length() - 1, 1);
      if (subcmd == "pack") {
        int v = findWidget(cmd);
        w[u].sub[w[u].subN++] = v;
      } else if (subcmd == "set_border") {
        int x;
        sscanf(cmd.c_str(), "%d", &x);
        w[u].border = x;
      } else if (subcmd == "set_spacing") {
        int x;
        sscanf(cmd.c_str(), "%d", &x);
        w[u].spacing = x;
      }
    }
  }
  memset(vis, false, sizeof(vis));
  for (int i = 0; i < wN; i++)
    if (!vis[i]) dfs(i);
  sort(w, w + wN);
  for (int i = 0; i < wN; i++)
    cout << w[i].name << ' ' << w[i].width << ' ' << w[i].height << endl;
  return 0;
}
