#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int max_len = 1e3 + 100;
int G[maxn][maxn], in[maxn];
struct _Widget {
  string name;
  int type, border, spacing;
  long long x, y;
  bool operator<(const _Widget& rhs) const { return name < rhs.name; }
} _widget[maxn];
int n, cnt;
char str[max_len];
int store_id[maxn];
int find_string(string s) {
  for (int i = 1; i <= cnt; i++)
    if (_widget[i].name == s) return i;
  _widget[++cnt].name = s;
  return cnt;
}
void bfs() {
  queue<int> Q;
  int now_len = 0;
  while (!Q.empty()) Q.pop();
  for (int i = 1; i <= cnt; i++)
    if (!in[i]) Q.push(i);
  while (!Q.empty()) {
    int cur = Q.front();
    Q.pop();
    store_id[++now_len] = cur;
    for (int i = 1; i <= cnt; i++)
      if (G[cur][i]) {
        in[i] -= G[cur][i];
        if (!in[i]) Q.push(i);
      }
  }
  for (int i = now_len; i >= 1; i--) {
    int cur = store_id[i];
    if (!_widget[cur].type) continue;
    int num = 0;
    for (int j = 1; j <= cnt; j++)
      for (int k = 1; k <= G[cur][j]; k++) {
        num++;
        if (_widget[cur].type == 1) {
          _widget[cur].x += _widget[j].x;
          _widget[cur].y = max(_widget[cur].y, _widget[j].y);
        } else {
          _widget[cur].y += _widget[j].y;
          _widget[cur].x = max(_widget[cur].x, _widget[j].x);
        }
      }
    if (!num)
      _widget[cur].x = _widget[cur].y = 0;
    else if (_widget[cur].type == 1) {
      _widget[cur].x +=
          (2 * _widget[cur].border + (num - 1) * _widget[cur].spacing);
      _widget[cur].y += (2 * _widget[cur].border);
    } else {
      _widget[cur].y +=
          (2 * _widget[cur].border + (num - 1) * _widget[cur].spacing);
      _widget[cur].x += (2 * _widget[cur].border);
    }
  }
}
void Get_Widget() {
  scanf("%s", str);
  string s;
  int clen = strlen(str);
  int i, j;
  for (i = 0; i < clen; i++)
    if (str[i] == '(')
      break;
    else
      s += str[i];
  int nx = 0, ny = 0;
  for (j = i + 1; j < clen; j++)
    if (str[j] == ',')
      break;
    else
      nx = nx * 10 + str[j] - '0';
  for (i = j + 1; i < clen - 1; i++) ny = ny * 10 + str[i] - '0';
  int cur = find_string(s);
  _widget[cur].x = nx;
  _widget[cur].y = ny;
}
void Get_HBox() {
  string s;
  cin >> s;
  int cur = find_string(s);
  _widget[cur].type = 1;
  _widget[cur].x = _widget[cur].y = 0;
}
void Get_VBox() {
  string s;
  cin >> s;
  int cur = find_string(s);
  _widget[cur].type = 2;
  _widget[cur].x = _widget[cur].y = 0;
}
int main() {
  cnt = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", str);
    int clen = strlen(str);
    bool flag = false;
    int last;
    for (int i = 0; i < clen; i++)
      if (str[i] == '.') {
        flag = true;
        last = i;
      }
    if (flag) {
      string s1;
      for (int i = 0; i < last; i++) s1 += str[i];
      int cur = find_string(s1), k;
      if (str[last + 1] == 'p') {
        for (k = last + 1; k < clen; k++)
          if (str[k] == '(') break;
        string s2;
        for (int l = k + 1; l < clen - 1; l++) s2 += str[l];
        int sub = find_string(s2);
        G[cur][sub]++;
        in[sub]++;
      } else {
        for (k = last + 1; k < clen; k++)
          if (str[k] == '(') break;
        int dgt = 0;
        for (int l = k + 1; l < clen - 1; l++) dgt = dgt * 10 + str[l] - '0';
        if (str[last + 5] == 'b')
          _widget[cur].border = dgt;
        else
          _widget[cur].spacing = dgt;
      }
    } else if (str[0] == 'W')
      Get_Widget();
    else if (str[0] == 'H')
      Get_HBox();
    else
      Get_VBox();
  }
  bfs();
  sort(_widget + 1, _widget + 1 + cnt);
  for (int i = 1; i <= cnt; i++)
    printf("%s %I64d %I64d\n", _widget[i].name.c_str(), _widget[i].x,
           _widget[i].y);
}
