#include <bits/stdc++.h>
using namespace std;
struct widget {
  long long w, h, type, cnt;
};
map<string, widget> mm;
map<string, vector<string> > g1, g2;
map<string, int> was;
vector<string> top;
vector<pair<string, pair<long long, long long> > > res;
map<string, pair<long long, long long> > szz;
void dfs(string v) {
  if (was[v] != 0) return;
  was[v] = 1;
  for (int i = (0); i < (g1[v].size()); ++i) dfs(g1[v][i]);
  top.push_back(v);
}
int main() {
  int n;
  scanf("%d", &n);
  string s;
  getline(cin, s);
  for (int i = (0); i < (n); ++i) {
    getline(cin, s);
    bool f = false;
    for (int i = (0); i < (s.size()); ++i)
      if (s[i] == '.') f = true;
    if (!f) {
      if (s[0] == 'W') {
        string s1 = "";
        for (int i = (0); i < (s.size()); ++i)
          if ((s[i] == '(') || (s[i] == ')') || (s[i] == ','))
            s1 += ' ';
          else
            s1 += s[i];
        stringstream tmp;
        tmp << s1;
        string s2;
        long long w, h;
        tmp >> s2;
        tmp >> s2 >> w >> h;
        mm[s2].cnt = 0;
        mm[s2].h = h;
        mm[s2].w = w;
        mm[s2].type = 2;
      } else if (s[0] == 'V') {
        stringstream tmp;
        tmp << s;
        string s1;
        tmp >> s1;
        tmp >> s1;
        mm[s1].cnt = 0;
        mm[s1].h = 0;
        mm[s1].w = 0;
        mm[s1].type = 0;
      } else {
        stringstream tmp;
        tmp << s;
        string s1;
        tmp >> s1;
        tmp >> s1;
        mm[s1].cnt = 0;
        mm[s1].h = 0;
        mm[s1].w = 0;
        mm[s1].type = 1;
      }
    } else {
      string s1 = "";
      for (int i = (0); i < (s.size()); ++i)
        if ((s[i] == '(') || (s[i] == ')') || (s[i] == '.'))
          s1 += ' ';
        else
          s1 += s[i];
      stringstream tmp;
      tmp << s1;
      tmp >> s1;
      string s2;
      tmp >> s2;
      if (s2 == "set_border") {
        long long w;
        tmp >> w;
        mm[s1].w = w;
      } else if (s2 == "set_spacing") {
        long long w;
        tmp >> w;
        mm[s1].h = w;
      } else {
        tmp >> s2;
        mm[s1].cnt++;
        g1[s2].push_back(s1);
        g2[s1].push_back(s2);
      }
    }
  }
  for (map<string, widget>::iterator it = mm.begin(); it != mm.end(); ++it)
    dfs(it->first);
  for (int j = top.size() - 1; j >= 0; --j) {
    string s = top[j];
    if (mm[s].type == 2) {
      res.push_back(make_pair(s, make_pair(mm[s].w, mm[s].h)));
      szz[s] = make_pair(mm[s].w, mm[s].h);
    } else {
      if (mm[s].cnt == 0)
        res.push_back(make_pair(s, make_pair(0, 0)));
      else {
        if (mm[s].type == 0) {
          long long w = 0;
          long long h = 0;
          for (int i = (0); i < (g2[s].size()); ++i) {
            long long k = szz[g2[s][i]].first;
            w = ((w) > (k) ? (w) : (k));
            h += szz[g2[s][i]].second;
          }
          w += mm[s].w * 2;
          h += mm[s].w * 2 + mm[s].h * (mm[s].cnt - 1);
          res.push_back(make_pair(s, make_pair(w, h)));
          szz[s] = make_pair(w, h);
        } else {
          long long w = 0;
          long long h = 0;
          for (int i = (0); i < (g2[s].size()); ++i) {
            h = ((h) > (szz[g2[s][i]].second) ? (h) : (szz[g2[s][i]].second));
            w += szz[g2[s][i]].first;
          }
          h += mm[s].w * 2;
          w += mm[s].w * 2 + mm[s].h * (mm[s].cnt - 1);
          res.push_back(make_pair(s, make_pair(w, h)));
          szz[s] = make_pair(w, h);
        }
      }
    }
  }
  sort(res.begin(), res.end());
  for (int i = (0); i < (res.size()); ++i)
    cout << res[i].first << " " << res[i].second.first << " "
         << res[i].second.second << endl;
  return 0;
}
