#include <bits/stdc++.h>
using namespace std;
const int oo = 1e9;
struct HBox {
  long long space, border, widgets;
  long long width, height;
  vector<string> packed;
  string name;
};
struct VBox {
  long long space, border, widgets;
  long long width, height;
  vector<string> packed;
  string name;
};
struct Widget {
  long long width, height;
  string name;
};
unordered_map<string, HBox> hboxes;
vector<pair<string, int>> all;
unordered_map<string, VBox> vboxes;
unordered_map<string, Widget> wdgts;
unordered_map<string, pair<long long, long long>> containers;
void calcdims(string name) {
  if (containers.count(name)) return;
  long long width = 0, height = 0;
  if (hboxes.count(name)) {
    for (int i = 0; i < hboxes[name].packed.size(); i++) {
      if (hboxes.count(hboxes[name].packed[i]) ||
          vboxes.count(hboxes[name].packed[i])) {
        calcdims(hboxes[name].packed[i]);
        width += containers[hboxes[name].packed[i]].first;
        height = max(height, containers[hboxes[name].packed[i]].second);
      } else {
        width += wdgts[hboxes[name].packed[i]].width;
        height = max(height, wdgts[hboxes[name].packed[i]].height);
      }
    }
    if (hboxes[name].packed.size()) {
      width += 2 * hboxes[name].border;
      height += 2 * hboxes[name].border;
      width += (hboxes[name].packed.size() - 1) * hboxes[name].space;
    }
  } else {
    for (int i = 0; i < vboxes[name].packed.size(); i++) {
      if (hboxes.count(vboxes[name].packed[i]) ||
          vboxes.count(vboxes[name].packed[i])) {
        calcdims(vboxes[name].packed[i]);
        height += containers[vboxes[name].packed[i]].second;
        width = max(width, containers[vboxes[name].packed[i]].first);
      } else {
        height += wdgts[vboxes[name].packed[i]].height;
        width = max(width, wdgts[vboxes[name].packed[i]].width);
      }
    }
    if (vboxes[name].packed.size()) {
      width += 2 * vboxes[name].border;
      height += 2 * vboxes[name].border;
      height += (vboxes[name].packed.size() - 1) * vboxes[name].space;
    }
  }
  containers[name] = pair<long long, long long>(width, height);
}
int main() {
  int n;
  cin >> n;
  char xxx;
  scanf("%c", &xxx);
  for (int i = 0; i < n; i++) {
    string s;
    getline(cin, s);
    string firstWord;
    int j = 0;
    for (j = 0; s[j] != ' ' && s[j] != '\0'; j++) {
      firstWord += s[j];
    }
    if (firstWord == "Widget") {
      Widget x;
      string name;
      j++;
      for (; s[j] != '('; j++) {
        name += s[j];
      }
      x.name = name;
      long long width = 0;
      j++;
      for (; s[j] != ','; j++) width = width * 10 + s[j] - '0';
      x.width = width;
      long long height = 0;
      j++;
      for (; s[j] != ')'; j++) height = height * 10 + s[j] - '0';
      x.height = height;
      all.push_back(pair<string, int>(name, 0));
      wdgts[name] = x;
    } else if (firstWord == "HBox") {
      HBox x;
      string name;
      j++;
      for (; s[j] != '\0'; j++) {
        name += s[j];
      }
      x.name = name;
      x.width = 0;
      x.space = 0;
      x.border = 0;
      x.height = 0;
      x.widgets = 0;
      all.push_back(pair<string, int>(name, 1));
      hboxes[name] = x;
    } else if (firstWord == "VBox") {
      VBox x;
      string name;
      j++;
      for (; s[j] != '\0'; j++) {
        name += s[j];
      }
      x.name = name;
      x.width = 0;
      x.height = 0;
      x.space = 0;
      x.border = 0;
      x.widgets = 0;
      all.push_back(pair<string, int>(name, 2));
      vboxes[name] = x;
    } else {
      if (s.find(".pack(") != string::npos) {
        string name;
        int j = 0;
        for (j = 0; s[j] != '.'; j++) {
          name += s[j];
        }
        string packed;
        while (s[j] != '(') j++;
        j++;
        for (; s[j] != ')'; j++) packed += s[j];
        if (hboxes.count(name)) {
          hboxes[name].widgets++;
          hboxes[name].packed.push_back(packed);
        } else {
          vboxes[name].packed.push_back(packed);
          vboxes[name].widgets++;
        }
      } else if (s.find(".set_border(") != string::npos) {
        string name;
        int j = 0;
        for (j = 0; s[j] != '.'; j++) {
          name += s[j];
        }
        while (s[j] != '(') j++;
        j++;
        int border = 0;
        for (; s[j] != ')'; j++) border = border * 10 + s[j] - '0';
        if (hboxes.count(name))
          hboxes[name].border = border;
        else
          vboxes[name].border = border;
      } else if (s.find(".set_spacing(")) {
        string name;
        int j = 0;
        for (j = 0; s[j] != '.'; j++) {
          name += s[j];
        }
        while (s[j] != '(') j++;
        j++;
        int spacing = 0;
        for (; s[j] != ')'; j++) spacing = spacing * 10 + s[j] - '0';
        if (hboxes.count(name))
          hboxes[name].space = spacing;
        else
          vboxes[name].space = spacing;
      }
    }
  }
  sort((all).begin(), (all).end(), less<pair<string, int>>());
  for (int i = 0; i < all.size(); i++)
    if (all[i].second != 0) calcdims(all[i].first);
  for (int i = 0; i < all.size(); i++) {
    cout << all[i].first << " ";
    if (all[i].second == 0) {
      cout << wdgts[all[i].first].width << " " << wdgts[all[i].first].height
           << "\n";
    } else if (all[i].second == 1) {
      cout << containers[all[i].first].first << " "
           << containers[all[i].first].second << "\n";
    } else {
      cout << containers[all[i].first].first << " "
           << containers[all[i].first].second << "\n";
    }
  }
}
