#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-8;
template <class T>
T sqr(T x) {
  return x * x;
}
int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }
class widget {
 public:
  string name;
  long long w, h, bd, sp;
  int type;
  vector<string> v;
  widget() {
    name = "";
    w = h = bd = sp = type = 0;
  }
};
map<string, widget> m;
void getwh(widget cur, long long& w, long long& h) {
  if (cur.w || cur.h) {
    w = cur.w;
    h = cur.h;
  } else {
    for (int j = 0; j < cur.v.size(); ++j) {
      widget sub = m[cur.v[j]];
      long long cw = 0, ch = 0;
      getwh(sub, cw, ch);
      if (cur.type == 1) {
        w = max(w, cw);
        if (j) h += cur.sp;
        h += ch;
      } else {
        h = max(h, ch);
        if (j) w += cur.sp;
        w += cw;
      }
    }
    if (cur.v.size() != 0) w += cur.bd * 2;
    if (cur.v.size() != 0) h += cur.bd * 2;
    cur.w = w;
    cur.h = h;
    m[cur.name] = cur;
  }
}
int main(int argc, char** argv) {
  int n;
  scanf("%d", &n);
  string str;
  vector<string> v;
  for (int i = 0; i < n; ++i) {
    cin >> str;
    if (str == "Widget") {
      cin >> str;
      int j;
      widget cur;
      cur.w = cur.h = 0;
      for (j = 0; str[j] != '('; ++j) {
        cur.name += str[j];
      }
      for (++j; str[j] != ','; ++j) {
        cur.w = cur.w * 10 + (str[j] - '0');
      }
      for (++j; str[j] != ')'; ++j) {
        cur.h = cur.h * 10 + (str[j] - '0');
      }
      v.push_back(cur.name);
      m[cur.name] = cur;
    } else if (str == "VBox") {
      widget cur;
      cur.type = 1;
      cin >> str;
      cur.name = str;
      m[str] = cur;
      v.push_back(cur.name);
    } else if (str == "HBox") {
      widget cur;
      cur.type = 2;
      cin >> str;
      cur.name = str;
      m[str] = cur;
      v.push_back(cur.name);
    } else {
      int j;
      string name, method;
      for (j = 0; str[j] != '.'; ++j) {
        name += str[j];
      }
      widget cur = m[name];
      for (++j; str[j] != '('; ++j) {
        method += str[j];
      }
      if (method == "pack") {
        name = "";
        for (++j; str[j] != ')'; ++j) {
          name += str[j];
        }
        cur.v.push_back(name);
      } else if (method == "set_border") {
        cur.bd = 0;
        for (++j; str[j] != ')'; ++j) {
          cur.bd = cur.bd * 10 + str[j] - '0';
        }
      } else if (method == "set_spacing") {
        cur.sp = 0;
        for (++j; str[j] != ')'; ++j) {
          cur.sp = cur.sp * 10 + str[j] - '0';
        }
      }
      m[cur.name] = cur;
    }
  }
  sort((v).begin(), (v).end());
  for (int i = 0; i < v.size(); ++i) {
    widget cur = m[v[i]];
    long long w = 0, h = 0;
    getwh(cur, w, h);
    cout << cur.name << " " << w << " " << h << endl;
  }
  return (EXIT_SUCCESS);
}
