#include <bits/stdc++.h>
using namespace std;
long long getnum(string str) {
  long long x = 0, p = 1;
  for (long long i = str.size() - 1; i >= 0; i--) {
    x += p * (str[i] - '0');
    p *= 10;
  }
  return x;
}
string getobj(string str) {
  for (long long i = 0; i < str.size(); i++) {
    if (str[i] == '.') {
      return str.substr(0, i);
    }
  }
  return "";
}
string getobjdec(string str) {
  for (long long i = 0; i < str.size(); i++) {
    if (str[i] == '(') {
      return str.substr(0, i);
    }
  }
  return "";
}
pair<long long, long long> getparamdec(string str) {
  for (long long i = 0; i < str.size(); i++) {
    if (str[i] == '(') {
      for (long long j = i + 1; j < str.size(); j++) {
        if (str[j] == ',') {
          return pair<long long, long long>(
              getnum(str.substr(i + 1, j - i - 1)),
              getnum(str.substr(j + 1, str.size() - j - 2)));
        }
      }
    }
  }
  return pair<long long, long long>(0, 0);
}
string getfunc(string str) {
  for (long long i = 0; i < str.size(); i++) {
    if (str[i] == '.') {
      for (long long j = i + 1; j < str.size(); j++) {
        if (str[j] == '(') return str.substr(i + 1, j - i - 1);
      }
    }
  }
  return "";
}
string getparam(string str) {
  for (long long i = 0; i < str.size(); i++) {
    if (str[i] == '.') {
      for (long long j = i + 1; j < str.size(); j++) {
        if (str[j] == '(') {
          return str.substr(j + 1, str.size() - j - 2);
        }
      }
    }
  }
  return "";
}
map<string, pair<long long, long long> > mp;
map<string, long long> typ;
map<string, vector<string> > pck;
map<string, long long> vis;
map<string, long long> border;
map<string, long long> spacing;
void dfs(string name) {
  if (vis[name] == 1) return;
  if (typ[name] == 1) {
    vis[name] = 1;
  } else if (typ[name] == 3) {
    vis[name] = 1;
    if (pck.find(name) == pck.end()) return;
    vector<string> v = pck[name];
    long long w = 0, h = 0;
    for (long long i = 0; i < v.size(); i++) {
      dfs(v[i]);
      pair<long long, long long> p = mp[v[i]];
      w += spacing[name] + p.first;
      h = max(h, p.second);
    }
    if (w == 0 && h == 0)
      ;
    else
      w -= spacing[name];
    if (v.size())
      mp[name] = pair<long long, long long>(w + 2 * border[name],
                                            h + 2 * border[name]);
  } else if (typ[name] == 2) {
    vis[name] = 1;
    if (pck.find(name) == pck.end()) return;
    vector<string> v = pck[name];
    long long w = 0, h = 0;
    for (long long i = 0; i < v.size(); i++) {
      dfs(v[i]);
      pair<long long, long long> p = mp[v[i]];
      h += spacing[name] + p.second;
      w = max(w, p.first);
    }
    if (w == 0 && h == 0)
      ;
    else
      h -= spacing[name];
    if (v.size())
      mp[name] = pair<long long, long long>(w + 2 * border[name],
                                            h + 2 * border[name]);
  }
}
int main() {
  char str[100];
  long long n;
  gets(str);
  sscanf(str, "%I64d", &n);
  while (n--) {
    gets(str);
    stringstream ss;
    string st[10];
    ss << str;
    long long x = 0;
    while (ss >> st[x]) {
      x++;
    }
    if (x == 2) {
      if (st[0] == "Widget") {
        string name = getobjdec(st[1]);
        pair<long long, long long> p = getparamdec(st[1]);
        mp[name] = p;
        typ[name] = 1;
      } else if (st[0] == "VBox") {
        mp[st[1]] = pair<long long, long long>(0, 0);
        typ[st[1]] = 2;
      } else if (st[0] == "HBox") {
        mp[st[1]] = pair<long long, long long>(0, 0);
        typ[st[1]] = 3;
      }
    } else {
      string name = getobj(st[0]);
      string func = getfunc(st[0]);
      string param = getparam(st[0]);
      if (func == "pack") {
        pck[name].push_back(param);
      } else if (func == "set_border") {
        border[name] = getnum(param);
      } else if (func == "set_spacing") {
        spacing[name] = getnum(param);
      }
    }
  }
  map<string, pair<long long, long long> >::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    dfs((*it).first);
    cout << (*it).first << " " << (*it).second.first << " "
         << (*it).second.second << endl;
  }
  return 0;
}
