#include <bits/stdc++.h>
using namespace std;
struct widget {
  int tipe;
  long long p;
  long long l;
  long long space;
  long long border;
  vector<string> kul;
};
int n;
string lho;
map<string, widget> widg;
set<string> oke;
void resize(__typeof(widg.begin()) loh) {
  widget &wget = loh->second;
  if (wget.tipe == 0) return;
  if (oke.find(loh->first) != oke.end()) return;
  oke.insert(loh->first);
  vector<string> &v = wget.kul;
  int z = v.size();
  long long pjg, lbr;
  if (z == 0) return;
  pjg = lbr = 0;
  for (int i = 0; i < (int)z; ++i) {
    __typeof(widg.begin()) t = widg.find(v[i]);
    resize(t);
    widget lha = t->second;
    if (wget.tipe == 1) {
      lbr += lha.l;
      pjg = max(pjg, lha.p);
    } else if (wget.tipe == 2) {
      lbr = max(lbr, lha.l);
      pjg += lha.p;
    }
  }
  wget.l = 2 * wget.border + (z - 1) * wget.space * (wget.tipe == 1) + lbr;
  wget.p = 2 * wget.border + (z - 1) * wget.space * (wget.tipe == 2) + pjg;
}
int main() {
  scanf("%d%*c", &n);
  while (n--) {
    getline(cin, lho);
    int ln = lho.length();
    if (lho[0] == 'W') {
      int st = lho.find(' ');
      ++st;
      int en = lho.find('(');
      string nama = lho.substr(st, en - st);
      int x = en + 1, p = 0, l = 0;
      while (lho[x] != ',') {
        p *= 10;
        p += (int)(lho[x] - '0');
        ++x;
      }
      ++x;
      while (lho[x] != ')') {
        l *= 10;
        l += (int)(lho[x] - '0');
        ++x;
      }
      widget &w = widg[nama];
      w.tipe = w.space = w.border = 0;
      w.p = p;
      w.l = l;
    } else if (lho[0] == 'V') {
      int st = lho.find(' ');
      ++st;
      string nama = lho.substr(st, ln - st);
      widget &w = widg[nama];
      w.tipe = 1;
      w.p = w.l = w.space = w.border = 0;
    } else if (lho[0] == 'H') {
      int st = lho.find(' ');
      ++st;
      string nama = lho.substr(st, ln - st);
      widget &w = widg[nama];
      w.tipe = 2;
      w.p = w.l = w.space = w.border = 0;
    } else {
      int x = lho.find('.');
      if (lho[x + 1] == 'p') {
        int st = x + 6;
        int en = ln - 1;
        string nama1 = lho.substr(0, x);
        string nama2 = lho.substr(st, en - st);
        widget &box = widg[nama1];
        box.kul.push_back(nama2);
      } else if (lho[x + 5] == 'b') {
        int c = lho.find('(');
        ++c;
        int sz = 0;
        while (lho[c] != ')') {
          sz *= 10;
          sz += (int)(lho[c] - '0');
          ++c;
        }
        string nama = lho.substr(0, x);
        widg[nama].border = sz;
      } else if (lho[x + 5] == 's') {
        int c = lho.find('(');
        ++c;
        int sz = 0;
        while (lho[c] != ')') {
          sz *= 10;
          sz += (int)(lho[c] - '0');
          ++c;
        }
        string nama = lho.substr(0, x);
        widg[nama].space = sz;
      }
    }
  }
  __typeof(widg.begin()) it = widg.begin();
  while (it != widg.end()) {
    resize(it);
    ++it;
  }
  it = widg.begin();
  while (it != widg.end()) {
    printf("%s %I64d %I64d\n", it->first.c_str(), it->second.p, it->second.l);
    ++it;
  }
  return 0;
}
