#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define for if (0) ; else for
#define MAKEPIC 1
#define MAXPIC 20

void Assert(bool b, const char *s)
{
  if (!b)
  {
    fprintf(stderr, "Incorrect test: %s\n", s);
    exit(1);
  }
}

int max(int a, int b)
{
  return (a > b) ? a : b;
}

/**
  * curc = current char
  * curw = current word (EOF if eof, "" if empty line)
  * curl = strlen(curw)
  * m = surrounded count (only intersecting current line)
  * (vx, vy) = current position
  * (picy, picl, picr) = surrounded positions (picy is bottom)
  * lh = line height
  * (lastx, lasty) = floating insertion position
  * maxy = bottom of all surrounded
  * mpf = MetaPOST log file
  **/

char curc, curw[65536];
int curl, w, h, c, m, vx, vy, lh, maxy, cfrag, lastx, lasty;
int picy[1000], picl[1000], picr[1000];
FILE *mpf;

void nextC()
{
  static int cnt = 0;
  if (curc != EOF)
  {
    curc = getc(stdin);
    Assert(cnt++ <= 1000, "document too big");
  }
}

void nextW()
{
  int crC = 0;
  while (curc == ' ' || curc == '\n')
  {
    crC += (curc == '\n');
    nextC();
  }
  if (crC > 1)
  {
    curw[0] = 0;
    return;
  }
  if (curc == EOF)
  {
    curw[0] = EOF;
    curw[1] = 0;
    return;
  }
  curl = 0;
  if (curc == '(')
  {
    while (curc != ')')
    {
      curw[curl++] = curc;
      nextC();
      Assert(curc != EOF, "image description never ends");
    }
    nextC();     
    curw[curl] = 0;
    return;
  }
  while (curc != ' ' && curc != '\n' && curc != EOF)
  {
    curw[curl++] = curc;
    nextC();
  }
  curw[curl] = 0;
}

void init()
{
  curc = 0;
  nextC();
  nextW();
  Assert(sscanf(curw, "%d", &w) == 1 && w >= 1 && w <= 1000, "cannot read w");
  nextW();
  Assert(sscanf(curw, "%d", &h) == 1 && h >= 1 && h <= 50, "cannot read h");
  nextW();
  Assert(sscanf(curw, "%d", &c) == 1 && c >= 1 && c <= w, "cannot read c");
  nextW();

  if (MAKEPIC)
  {
    mpf = fopen("picture.mp", "wt");
    fprintf(mpf, "%%% Problem: layout\n");
    fprintf(mpf, "%%% Machine-generated illustration\n");
    fprintf(mpf, "def makePicture = \n");
    fprintf(mpf, "setParams(%d, %d, %d);\n\n", w, h, c);
  }

  vx = 0;
  vy = 0;
  maxy = 0;
  m = 0;
  cfrag = 0;
  lh = h;
  lastx = 0;
  lasty = 0;
}

void par() /// start new paragraph
{
  if (!vx && !vy)
    return;
  vx = 0;
  vy = max(maxy, vy + lh);
  lh = h;
  m = 0;  
  cfrag = 0;
  lastx = vx;
  lasty = vy;
}

void newLine()
{
  vx = 0;
  vy += lh;
  lh = h;
  cfrag = 0;

  int m0 = m;
  m = 0;
  for (int i=0;i<m0;i++)
    if (picy[i] > vy)
    {
      picy[m] = picy[i];
      picl[m] = picl[i];
      picr[m] = picr[i];
      ++m;
    }
}

int fragStart(int f)
{
  return f ? picr[f - 1] : 0;
}

int fragEnd(int f)
{  
  return (f == m) ? w : picl[f];
}

void insertBox(int cw, int &cx, int &cy, bool space)
{
  space &= (vx > fragStart(cfrag));
  cx = vx + c * space;
  if (cx + cw <= fragEnd(cfrag))
  {
    cy = vy;
    vx = cx + cw;
    return;
  }
  ++cfrag;
  for (;;)
  {
    if (cfrag > m)
      newLine();
    int fS = fragStart(cfrag), fE = fragEnd(cfrag);
    if (fS + cw <= fE)
    {
      cx = fS;
      cy = vy;
      vx = fS + cw;
      return;
    }
    ++cfrag;
  }  
}

void embedBox(int cw, int ch, int &cx, int &cy)
{
  insertBox(cw, cx, cy, true);
  lastx = vx;
  lasty = vy;
  if (lh < ch)
    lh = ch;
}

void surroundBox(int cw, int ch, int &cx, int &cy)
{
  insertBox(cw, cx, cy, false);
  lastx = vx;
  lasty = vy;
  if (maxy < ch + cy)
    maxy = ch + cy;
  int ip; /// insertion place
  for (ip=0;ip<m;ip++)
    if (picl[ip] > cx)
      break;
  if (ip < m)
  {
    memmove(picy + ip + 1, picy + ip, (m - ip) * sizeof(int));
    memmove(picl + ip + 1, picl + ip, (m - ip) * sizeof(int));
    memmove(picr + ip + 1, picr + ip, (m - ip) * sizeof(int));
  }
  picy[ip] = ch + cy;  
  picl[ip] = cx;
  picr[ip] = cx + cw;
  ++m;
}

void outputPicture(int cx, int cy)
{
  printf("%d %d\n", cx, cy);
}

void insertWord()
{
  int cw = curl * c;
  Assert(cw <= w, "word too long");
  int cx, cy;
  embedBox(cw, h, cx, cy);
  if (MAKEPIC)
    fprintf(mpf, "drawWord(%d, %d, %d, %d);\n", cx, cy, cw, h);
}

void insertPicture()
{
  static int pictureCount = 0;
  Assert(pictureCount++ <= MAXPIC, "too many pictures"); 
  int cw, ch, cx, cy;
  char *cc = strstr(curw, "width=");
  Assert(sscanf(cc + 6, "%d", &cw) && cw >= 1 && cw <= w, "illegal picture width");
  cc = strstr(curw, "height=");
  Assert(sscanf(cc + 7, "%d", &ch) && ch >= 1 && ch <= 1000, "illegal picture height");

  if (strstr(curw, "layout=floating"))
  {
    int dx, dy;
    cc = strstr(curw, "dx=");
    Assert(sscanf(cc + 3, "%d", &dx) && dx >= -1000 && dx <= 1000, "illegal dx");
    cc = strstr(curw, "dy=");
    Assert(sscanf(cc + 3, "%d", &dy) && dy >= -1000 && dy <= 1000, "illegal dy");
    int cx = lastx + dx, cy = lasty + dy;
    if (cx < 0)
      cx = 0;
    if (cx + cw > w)
      cx = w - cw;
    outputPicture(cx, cy);
    lastx = cx + cw;
    lasty = cy;
    if (MAKEPIC)
      fprintf(mpf, "drawFloating(%d, %d, %d, %d);\n", cx, cy, cw, ch);
    return;
  }

  if (strstr(curw, "layout=embedded")) /// embedded
  {
    embedBox(cw, ch, cx, cy);
    outputPicture(cx, cy);
    if (MAKEPIC)
      fprintf(mpf, "drawEmbedded(%d, %d, %d, %d);\n", cx, cy, cw, ch);
    return;
  }

  if (strstr(curw, "layout=surrounded")) /// surrounded
  {
    surroundBox(cw, ch, cx, cy);
    outputPicture(cx, cy);
    if (MAKEPIC)
      fprintf(mpf, "drawSurrounded(%d, %d, %d, %d);\n", cx, cy, cw, ch);
    return;
  }

  Assert(0, "illegal layout");
}

int main()
{
  freopen("layout.in", "rt", stdin);
  freopen("layout.out", "wt", stdout);

  init();

  while (curw[0] != EOF)
  {
    if (!curw[0])
    {
      par();
      nextW();
      continue;
    }
    if (curw[0] == '(')
      insertPicture();
    else
      insertWord();
    nextW();
  }

  if (MAKEPIC)
  {
    fprintf(mpf, "enddef;\n");
    fclose(mpf);
  }

  return 0;
}