#include <bits/stdc++.h>
const int inf = (int)1e9;
const int maxn = (int)1e5 + 10;
using namespace std;
int n;
map<string, int> id;
int sz;
vector<int> g[1111];
vector<int> gr[1111];
int type[1111];
int C[1111];
int D[1111];
string a[1111];
int get_id(string x) {
  if (id.count(x) == 0) {
    id[x] = sz;
    a[sz] = x;
    sz++;
  }
  return id[x];
}
int toNumber(string x) {
  int n = 0;
  for (int i = 0; i < x.size(); i++) {
    n = n * 10 + x[i] - '0';
  }
  return n;
}
void addWidget(string second) {
  int ind = 0;
  while (second[ind] != '(') ind++;
  int ind2 = ind;
  while (second[ind2] != ',') ind2++;
  string name = second.substr(0, ind);
  string x = second.substr(ind + 1, ind2 - ind - 1);
  string y = second.substr(ind2 + 1, second.size() - ind2 - 2);
  int I = get_id(name);
  type[I] = 0;
  C[I] = toNumber(x);
  D[I] = toNumber(y);
  return;
}
void addHBox(string second) {
  int I = get_id(second);
  type[I] = 1;
}
void addVBox(string second) {
  int I = get_id(second);
  type[I] = 2;
}
void addPack(string second, string x) {
  int a = get_id(second);
  int b = get_id(x);
  g[a].push_back(b);
  gr[b].push_back(a);
}
void setBorder(string second, string x) {
  int y = toNumber(x);
  int I = get_id(second);
  D[I] = y;
}
void setSpacing(string second, string x) {
  int y = toNumber(x);
  int I = get_id(second);
  C[I] = y;
}
vector<pair<string, pair<long long, long long> > > ans;
long long A[1111];
long long B[1111];
int used[1111];
void dfs(int v) {
  if (used[v]) return;
  used[v] = 1;
  if (type[v] == 0) {
    A[v] = C[v];
    B[v] = D[v];
  } else if (type[v] == 1) {
    A[v] = 0;
    B[v] = 0;
    long long sum = 0;
    long long mx = 0;
    for (int i = 0; i < g[v].size(); i++) {
      int to = g[v][i];
      dfs(to);
      sum += A[to];
      mx = max(B[to], mx);
    }
    if (g[v].size() > 0) {
      A[v] = C[v] * (g[v].size() - 1) + sum + 2 * D[v];
      B[v] = mx + 2 * D[v];
    }
  } else if (type[v] == 2) {
    A[v] = 0;
    B[v] = 0;
    long long sum = 0;
    long long mx = 0;
    for (int i = 0; i < g[v].size(); i++) {
      int to = g[v][i];
      dfs(to);
      sum += B[to];
      mx = max(A[to], mx);
    }
    if (g[v].size() > 0) {
      B[v] = C[v] * (g[v].size() - 1) + sum + 2 * D[v];
      A[v] = mx + 2 * D[v];
    }
  }
  ans.push_back(make_pair(a[v], make_pair(A[v], B[v])));
}
void solve() {
  scanf("%d\n", &n);
  for (int i = 0; i < n; i++) {
    string second;
    getline(cin, second);
    int ok = 0;
    for (int j = 0; j < second.size(); j++) {
      if (second[j] == '.') ok = 1;
    }
    if (ok) {
      int ind = 0;
      while (second[ind] != '.') ind++;
      int ind2 = 0;
      while (second[ind2] != '(') ind2++;
      string type = second.substr(ind + 1, ind2 - ind - 1);
      string ope = second.substr(ind2 + 1, second.size() - ind2 - 2);
      if (type == "pack") {
        addPack(second.substr(0, ind), ope);
      } else if (type == "set_border") {
        setBorder(second.substr(0, ind), ope);
      } else {
        setSpacing(second.substr(0, ind), ope);
      }
    } else {
      int ind = 0;
      while (second[ind] != ' ') ind++;
      if (second.substr(0, ind) == "Widget") {
        addWidget(second.substr(ind + 1));
      } else if (second.substr(0, ind) == "HBox") {
        addHBox(second.substr(ind + 1));
      } else {
        addVBox(second.substr(ind + 1));
      }
    }
  }
  for (int i = 0; i < sz; i++) {
    if (gr[i].size() == 0) {
      dfs(i);
    }
  }
  sort(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].first << " " << ans[i].second.first << " "
         << ans[i].second.second << endl;
  }
}
int main() {
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
