#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll NAX = 2e5 + 5, MOD = 1000000007;
struct Solution {
  Solution() {}
  void solveCase();
};
const ll HBox = 0, VBox = 1, Widget = 2;
struct My_widget {
  ll curr_type;
  ll border, spacing;
  ll w, h;
  My_widget(ll type, ll w = 0, ll h = 0) {
    this->curr_type = type;
    border = spacing = 0;
    this->w = w;
    this->h = h;
  }
  void set_border(ll x) { this->border = x; }
  void set_spacing(ll x) { this->spacing = x; }
};
ostream &operator<<(ostream &out, const My_widget &w) {
  if (w.curr_type == HBox)
    out << "HBox ";
  else if (w.curr_type == VBox)
    out << "VBox ";
  else
    out << "Widger ";
  out << w.w << ' ' << w.h << ' ' << w.border << ' ' << w.spacing << '\n';
  return out;
}
vector<My_widget> my_widget;
vector<ll> adj[NAX];
ll degree[NAX];
vector<ll> make_int_list(string t) {
  ll num = -1;
  vector<ll> ret;
  for (auto &x : t) {
    if ('0' <= x && x <= '9') {
      if (num == -1)
        num = x - '0';
      else
        num = num * 10 + x - '0';
    } else {
      if (num != -1) ret.push_back(num);
      num = -1;
    }
  }
  if (num != -1)
    ret.push_back(num);
  else
    num = -1;
  return ret;
}
ll width[NAX], height[NAX];
void dfs(ll node, ll par) {
  for (auto &x : adj[node]) {
    if (x == par) continue;
    dfs(x, node);
    if (my_widget[node].curr_type == Widget) {
      exit(1);
    } else if (my_widget[node].curr_type == HBox) {
      height[node] = max(height[node], height[x]);
      width[node] += width[x];
    } else {
      width[node] = max(width[node], width[x]);
      height[node] += height[x];
    }
  }
  if (my_widget[node].curr_type == Widget) {
    width[node] = my_widget[node].w;
    height[node] = my_widget[node].h;
  } else if (my_widget[node].curr_type == HBox) {
    if (adj[node].size())
      width[node] += my_widget[node].spacing * (adj[node].size() - 1LL);
  } else {
    if (adj[node].size())
      height[node] += my_widget[node].spacing * (adj[node].size() - 1LL);
  }
  height[node] += my_widget[node].border * 2;
  width[node] += my_widget[node].border * 2;
}
void Solution::solveCase() {
  ll n;
  cin >> n;
  vector<string> name_from_idx;
  map<string, ll> idx_from_name;
  string str;
  cin.ignore();
  while (getline(cin, str)) {
    vector<string> expression;
    if (count(str.begin(), str.end(), ' ')) {
      auto it = find(str.begin(), str.end(), ' ') - str.begin();
      expression.push_back(str.substr(0, it));
      expression.push_back(str.substr(it + 1));
    } else
      expression.push_back(str);
    if (expression.size() == 0) continue;
    if (expression.size() == 1) {
      auto it = find(expression[0].begin(), expression[0].end(), '.');
      auto identifier = expression[0].substr(0, it - expression[0].begin());
      auto intermediate = expression[0].substr(it - expression[0].begin() + 1);
      it = find(intermediate.begin(), intermediate.end(), '(');
      auto operation = intermediate.substr(0, it - intermediate.begin());
      auto params = intermediate.substr(it - intermediate.begin());
      auto idxx = idx_from_name[identifier];
      if (operation == "set_border")
        my_widget[idxx].set_border(make_int_list(params)[0]);
      else if (operation == "set_spacing")
        my_widget[idxx].set_spacing(make_int_list(params)[0]);
      else if (operation == "pack") {
        params = params.substr(1);
        params.pop_back();
        ;
        adj[idx_from_name[params]].push_back(idxx);
      }
    } else {
      auto type = expression[0];
      auto identifier = expression[1];
      auto it = find(identifier.begin(), identifier.end(), '(');
      auto params = identifier.substr(it - identifier.begin());
      auto numer_params = make_int_list(params);
      if (type[0] == 'W')
        my_widget.push_back(
            My_widget(Widget, numer_params[0], numer_params[1]));
      else if (type[0] == 'H')
        my_widget.push_back(My_widget(HBox));
      else
        my_widget.push_back(My_widget(VBox));
      string temp = "";
      for (auto &x : identifier) {
        if (x == '(') break;
        temp += x;
      }
      identifier = temp;
      ;
      idx_from_name[identifier] = name_from_idx.size();
      name_from_idx.push_back(identifier);
    }
  };
  ;
  ;
  queue<ll> Q;
  vector<ll> degree2(n);
  for (size_t i = 0; i < my_widget.size(); i++) {
    for (auto &x : adj[i]) {
      degree[x]++;
      degree2[x]++;
    }
  }
  for (size_t i = 0; i < my_widget.size(); i++) {
    ;
    if (degree[i] == 0) Q.push(i);
  }
  while (Q.size()) {
    auto top = Q.front();
    Q.pop();
    ;
    if (my_widget[top].curr_type == Widget) {
      width[top] = my_widget[top].w;
      height[top] = my_widget[top].h;
    } else if (my_widget[top].curr_type == HBox) {
      if (degree2[top])
        width[top] += my_widget[top].spacing * (degree2[top] - 1LL);
    } else {
      if (degree2[top])
        height[top] += my_widget[top].spacing * (degree2[top] - 1LL);
    }
    if (degree2[top]) {
      height[top] += my_widget[top].border * 2;
      width[top] += my_widget[top].border * 2;
    };
    for (auto &x : adj[top]) {
      degree[x]--;
      if (degree[x] == 0) Q.push(x);
      if (my_widget[x].curr_type == Widget) {
        exit(1);
      } else if (my_widget[x].curr_type == HBox) {
        height[x] = max(height[x], height[top]);
        width[x] += width[top];
      } else {
        width[x] = max(width[x], width[top]);
        height[x] += height[top];
      }
    }
  };
  for (auto &x : idx_from_name) {
    cout << x.first << ' ' << width[x.second] << ' ' << height[x.second]
         << '\n';
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll t = 1;
  Solution mySolver;
  for (ll i = 1; i <= t; ++i) {
    mySolver.solveCase();
  }
  return 0;
}
