#include <bits/stdc++.h>
using namespace std;
const int M = 100 + 10;
struct NODE {
  char type;
  vector<string> pack;
  long long int width;
  long long int height;
  long long int border;
  long long int space;
  string name;
  int visited;
} node[M];
struct ANS {
  long long int height;
  long long int width;
  string name;
  bool operator<(const ANS &x) const { return name < x.name; }
} ans[M];
map<string, int> rev;
void widget_decom(string s, long long int &w, long long int &h, string &name) {
  name = "";
  int i = 0;
  while (i < s.length() && s[i] != '(') {
    name += s[i];
    i++;
  }
  i++;
  w = h = 0;
  while (i < s.length() && s[i] != ',') {
    w = w * 10 + s[i] - '0';
    i++;
  }
  i++;
  while (i < s.length() && s[i] != ')') {
    h = h * 10 + s[i] - '0';
    i++;
  }
  return;
}
void DFS(int id) {
  if (node[id].visited == 1) {
    return;
  } else {
    if (node[id].type == 'h') {
      if (node[id].pack.size() > 0) {
        node[id].width =
            2 * node[id].border + (node[id].pack.size() - 1) * node[id].space;
        node[id].height = 2 * node[id].border;
        long long int max_h = 0;
        for (int i = 0; i < node[id].pack.size(); i++) {
          int n_node = rev[node[id].pack[i]];
          DFS(n_node);
          node[n_node].visited = 1;
          node[id].width += node[n_node].width;
          if (node[n_node].height > max_h) {
            max_h = node[n_node].height;
          }
        }
        node[id].height += max_h;
        return;
      } else {
        node[id].width = node[id].height = 0;
      }
    } else {
      if (node[id].pack.size() > 0) {
        node[id].height =
            2 * node[id].border + (node[id].pack.size() - 1) * node[id].space;
        node[id].width = 2 * node[id].border;
        long long int max_h = 0;
        for (int i = 0; i < node[id].pack.size(); i++) {
          int n_node = rev[node[id].pack[i]];
          DFS(n_node);
          node[n_node].visited = 1;
          node[id].height += node[n_node].height;
          if (node[n_node].width > max_h) {
            max_h = node[n_node].width;
          }
        }
        node[id].width += max_h;
        return;
      } else {
        node[id].width = node[id].height = 0;
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  int cnt = 0;
  for (int j = 0; j < n; j++) {
    string s;
    cin >> s;
    if (s == "Widget") {
      string t;
      cin >> t;
      string name;
      long long int h, w;
      widget_decom(t, w, h, name);
      rev[name] = cnt;
      node[cnt].type = 'w';
      node[cnt].height = h;
      node[cnt].width = w;
      node[cnt].name = name;
      node[cnt].visited = 1;
      cnt++;
    }
    if (s == "VBox") {
      string t;
      cin >> t;
      rev[t] = cnt;
      node[cnt].type = 'v';
      node[cnt].name = t;
      node[cnt].visited = 0;
      cnt++;
    }
    if (s == "HBox") {
      string t;
      cin >> t;
      rev[t] = cnt;
      node[cnt].type = 'h';
      node[cnt].name = t;
      node[cnt].visited = 0;
      cnt++;
    }
    if (s != "Widget" && s != "VBox" && s != "HBox") {
      int i = 0;
      string name = "";
      while (i < s.length() && s[i] != '.') {
        name += s[i];
        i++;
      }
      i++;
      string action;
      while (i < s.length() && s[i] != '(') {
        action += s[i];
        i++;
      }
      i++;
      int id = rev[name];
      if (action == "pack") {
        string packedname;
        while (i < s.length() && s[i] != ')') {
          packedname += s[i];
          i++;
        }
        node[id].pack.push_back(packedname);
      }
      if (action == "set_border") {
        int border = 0;
        while (i < s.length() && s[i] != ')') {
          border = border * 10 + s[i] - '0';
          i++;
        }
        node[id].border = border;
      }
      if (action == "set_spacing") {
        int border = 0;
        while (i < s.length() && s[i] != ')') {
          border = border * 10 + s[i] - '0';
          i++;
        }
        node[id].space = border;
      }
    }
  }
  for (int i = 0; i < cnt; i++) {
    DFS(i);
    ans[i].name = node[i].name;
    ans[i].height = node[i].height;
    ans[i].width = node[i].width;
  }
  sort(ans, ans + cnt);
  for (int i = 0; i < cnt; i++) {
    cout << ans[i].name << " " << ans[i].width << " " << ans[i].height << endl;
  }
  return 0;
}
