#include <bits/stdc++.h>
using namespace std;
int avail;
enum wtype { NONE = 0, NORMAL = 1, VBOX = 2, HBOX = 3 } WTYPE;
struct widget {
  wtype type;
  int myIndex;
  long long int height;
  long long int width;
  long long int spacing;
  long long int border;
  int added;
  int childHave;
  int chieldIndex[120];
};
widget* allWeigetAry[120];
int onlyWidget[120];
widget* createWidget(wtype type, int indx, int hgt, int wd, int sp, int bd) {
  widget* nw = (widget*)malloc(sizeof(widget));
  nw->type = type;
  nw->myIndex = indx;
  nw->height = hgt;
  nw->width = wd;
  nw->spacing = sp;
  nw->border = bd;
  nw->childHave = 0;
  nw->added = 0;
  for (int i = 0; i < 120; i++) nw->chieldIndex[i] = 0;
  return nw;
}
struct trieNode {
  bool isEnd;
  widget* myWiget;
  trieNode* next[26];
} * root;
trieNode* createNode() {
  trieNode* nw = (trieNode*)malloc(sizeof(trieNode));
  nw->isEnd = false;
  nw->myWiget = NULL;
  for (int i = 0; i < 26; i++) {
    nw->next[i] = NULL;
  }
  return nw;
}
void insertInTrie(char str[], widget* wgt) {
  trieNode* temp = root;
  for (int i = 0; str[i]; i++) {
    int num = str[i] - 'a';
    if (temp->next[num] == NULL) {
      temp->next[num] = createNode();
    }
    temp = temp->next[num];
  }
  temp->isEnd = true;
  temp->myWiget = wgt;
}
widget* searchInTrie(char str[]) {
  trieNode* temp = root;
  for (int i = 0; str[i]; i++) {
    int num = str[i] - 'a';
    if (temp->next[num] == NULL) return NULL;
    temp = temp->next[num];
  }
  if (temp->isEnd == false) return NULL;
  return temp->myWiget;
}
void searchAndPack(char name1[], char name2[]) {
  widget* name1Widget = searchInTrie(name1);
  widget* name2Widget = searchInTrie(name2);
  int index = name2Widget->myIndex;
  name1Widget->chieldIndex[index] = name1Widget->chieldIndex[index] + 1;
  name1Widget->childHave = 1;
}
void setBorder(char str[], int border) {
  widget* box = searchInTrie(str);
  box->border = border;
}
void setSpacing(char str[], int space) {
  widget* box = searchInTrie(str);
  box->spacing = space;
}
void getValue(int check) {
  widget* temp = allWeigetAry[check];
  for (int p = 0; p < avail; p++) {
    while (temp->chieldIndex[p] > 0) {
      if (onlyWidget[p] == 1) {
        if (temp->type == HBOX) {
          if (temp->added == 0)
            temp->width = allWeigetAry[p]->width;
          else
            temp->width = temp->width + allWeigetAry[p]->width + temp->spacing;
          if (temp->height < allWeigetAry[p]->height)
            temp->height = allWeigetAry[p]->height;
        } else if (temp->type == VBOX) {
          if (temp->added == 0)
            temp->height = allWeigetAry[p]->height;
          else
            temp->height =
                temp->height + allWeigetAry[p]->height + temp->spacing;
          if (temp->width < allWeigetAry[p]->width)
            temp->width = allWeigetAry[p]->width;
        }
        temp->added = temp->added + 1;
      } else if (onlyWidget[p] == 0) {
        getValue(p);
        onlyWidget[p] = 1;
        if (temp->type == HBOX) {
          if (temp->added == 0)
            temp->width = allWeigetAry[p]->width;
          else
            temp->width = temp->width + allWeigetAry[p]->width + temp->spacing;
          if (temp->height < allWeigetAry[p]->height)
            temp->height = allWeigetAry[p]->height;
        } else if (temp->type == VBOX) {
          if (temp->added == 0)
            temp->height = allWeigetAry[p]->height;
          else
            temp->height =
                temp->height + allWeigetAry[p]->height + temp->spacing;
          if (temp->width < allWeigetAry[p]->width)
            temp->width = allWeigetAry[p]->width;
        }
        temp->added = temp->added + 1;
      }
      temp->chieldIndex[p] = temp->chieldIndex[p] - 1;
    }
  }
  if (temp->childHave == 1) {
    temp->width = temp->width + (2 * temp->border);
    temp->height = temp->height + (2 * temp->border);
  }
}
void printValues(char str[], int length, trieNode* temp) {
  if (temp->isEnd == true) {
    str[length] = '\0';
    cout << str << " " << temp->myWiget->width << " " << temp->myWiget->height
         << endl;
  }
  for (int w = 0; w < 26; w++) {
    if (temp->next[w] != NULL) {
      str[length] = w + 'a';
      printValues(str, length + 1, temp->next[w]);
    }
  }
}
void insertWidget(char input[]) {
  int x = 0;
  char str[11] = "";
  wtype type;
  int height = 0, width = 0, number = 0;
  int startIndex, i;
  if (input[0] == 'H') {
    startIndex = 5;
    type = HBOX;
  } else if (input[0] == 'V') {
    startIndex = 5;
    type = VBOX;
  } else {
    startIndex = 7;
    type = NORMAL;
  }
  for (i = startIndex; input[i]; i++) {
    if (input[i] == '(') {
      break;
    }
    str[x] = input[i];
    x++;
  }
  str[x] = '\0';
  if (input[0] == 'W') {
    for (int j = i + 1; input[j]; j++) {
      if (input[j] == ',') {
        width = number;
        number = 0;
      } else if (input[j] == ')') {
        height = number;
        number = 0;
      } else {
        number = number * 10 + (input[j] - '0');
      }
    }
    onlyWidget[avail] = 1;
  }
  widget* wgt = createWidget(type, avail, height, width, 0, 0);
  insertInTrie(str, wgt);
  allWeigetAry[avail] = wgt;
  avail++;
}
int main() {
  root = createNode();
  avail = 0;
  int test;
  cin >> test;
  getchar();
  for (int cc = 0; cc < 120; cc++) onlyWidget[cc] = 0;
  char input[40] = "";
  for (int dd = 0; dd < test; dd++) {
    gets(input);
    if (input[0] == 'H' || input[0] == 'V' || input[0] == 'W') {
      insertWidget(input);
    } else {
      int x = 0;
      int i = 0;
      char name1[25] = "";
      char name2[25] = "";
      for (i = 0; input[i]; i++) {
        if (input[i] == '.') {
          name1[i] = '\0';
          break;
        } else
          name1[x] = input[i];
        x++;
      }
      if (input[i + 1] == 'p') {
        x = 0;
        for (int j = i + 6; input[j]; j++) {
          if (input[j] == ')') {
            name2[j] = '\0';
            break;
          } else
            name2[x] = input[j];
          x++;
        }
        searchAndPack(name1, name2);
      }
      if (input[i + 5] == 'b') {
        int number = 0;
        int j = i + 12;
        for (int m = j; input[m]; m++) {
          if (input[m] == ')') {
            setBorder(name1, number);
            break;
          }
          number = number * 10 + (input[m] - '0');
        }
      }
      if (input[i + 5] == 's') {
        int number = 0;
        int j = i + 13;
        for (int m = j; input[m]; m++) {
          if (input[m] == ')') {
            setSpacing(name1, number);
            break;
          }
          number = number * 10 + (input[m] - '0');
        }
      }
    }
  }
  for (int w = 0; w < avail; w++) {
    if (onlyWidget[w] == 0) {
      getValue(w);
      onlyWidget[w] = 1;
    }
  }
  char result[25];
  int length = 0;
  printValues(result, 0, root);
  return 0;
}
