#include <bits/stdc++.h>
const int BUFFSIZE = 10240;
char BUFF[BUFFSIZE + 1], *p = BUFF;
int CHAR, SIGN, BYTES = 0;
using namespace std;
vector<int> Adj[101];
typedef struct {
  string name;
  long long h;
  long long w;
  int type;
  int br;
  int sp;
} box;
box B[101];
int solved[101];
void solve(int i) {
  solved[i] = 1;
  if (B[i].type == 0)
    return;
  else if (B[i].type == 2) {
    int sz = Adj[i].size();
    if (sz == 0) return;
    long long wmax = 0, hsum = 0;
    for (int z = 0; z < sz; z++) {
      int child = Adj[i][z];
      if (!solved[child]) solve(child);
      wmax = max(wmax, B[child].w);
      hsum += B[child].h;
    }
    B[i].w = wmax + B[i].br * 2;
    B[i].h = hsum + (sz - 1) * B[i].sp + B[i].br * 2;
  } else {
    int sz = Adj[i].size();
    if (sz == 0) return;
    long long hmax = 0, wsum = 0;
    for (int z = 0; z < sz; z++) {
      int child = Adj[i][z];
      if (!solved[child]) solve(child);
      hmax = max(hmax, B[child].h);
      wsum += B[child].w;
    }
    B[i].h = hmax + B[i].br * 2;
    B[i].w = wsum + (sz - 1) * B[i].sp + B[i].br * 2;
  }
}
int main() {
  int N;
  cin >> N;
  map<string, int> M;
  int ids = 0;
  for (int itr = 0; itr < N; itr++) {
    string str;
    cin >> str;
    if (str == "HBox") {
      string name;
      cin >> name;
      M[name] = ids;
      box b;
      b.h = 0, b.w = 0, b.type = 1, b.br = 0, b.sp = 0;
      b.name = name;
      B[ids] = b;
      ids += 1;
    } else if (str == "VBox") {
      string name;
      cin >> name;
      M[name] = ids;
      box b;
      b.h = 0, b.w = 0, b.type = 2, b.br = 0, b.sp = 0;
      b.name = name;
      B[ids] = b;
      ids += 1;
    } else if (str == "Widget") {
      cin >> str;
      for (int i = 0; i < str.length(); i++)
        if (str[i] == '(' || str[i] == ')' || str[i] == ',') str[i] = ' ';
      string name;
      int w, h;
      stringstream sstr(str);
      sstr >> name >> w >> h;
      M[name] = ids;
      box b;
      b.h = h, b.w = w, b.type = 0, b.br = 0, b.sp = 0;
      b.name = name;
      B[ids] = b;
      ids += 1;
    } else {
      for (int i = 0; i < str.length(); i++)
        if (str[i] == '.' || str[i] == '(' || str[i] == ')') str[i] = ' ';
      string u, v;
      stringstream sstr(str);
      sstr >> u >> str >> v;
      if (str == "set_border") {
        stringstream sstr2(v);
        int br;
        sstr2 >> br;
        int id = M[u];
        assert(B[id].type != 0);
        B[id].br = br;
      } else if (str == "set_spacing") {
        stringstream sstr2(v);
        int sp;
        sstr2 >> sp;
        int id = M[u];
        assert(B[id].type != 0);
        B[id].sp = sp;
      } else {
        int id1 = M[u];
        int id2 = M[v];
        Adj[id1].push_back(id2);
      }
    }
  }
  for (int i = 0; i < ids; i++) {
    if (!solved[i]) solve(i);
  }
  map<string, int>::iterator it;
  for (it = M.begin(); it != M.end(); it++) {
    int id = it->second;
    cout << it->first << " " << B[id].w << " " << B[id].h << endl;
  }
}
