#include <iostream>
using namespace std;
#include <stdlib.h>
#include "String.h"
#include "Person4.h"

struct List {
  Person *data;
  List *next;
};

/* -- read on input record and return success/error -- */

bool getrecord(String& nbuf, String& fbuf, int& y, int& m, int& d)
{
  char sep;
  return (cin >> nbuf >> fbuf >> y >> sep >> m >> sep >> d);
}

int main() {
  String nbuf;
  String fbuf;
  Date birth;
  List* first = 0;
  List* curr = 0;
  List* last = 0;
  List* add = 0;
  Person* p, *old;

  /* -- read persons from stdin -- */
  while ( getrecord(nbuf, fbuf, birth.year, birth.month, birth.day) ) {
    /* -- construct new person record -- */
    add = new List;
    add->data = p = new Person(nbuf, fbuf, birth);
    add->next = 0;

    /* -- add to list (and sort them already at insertion time) -- */
    if ( first == 0 ) {
      /* -- first record -- */
      first = add;
    } else {
      /* -- there are already records -- */
      curr = first;
      last = 0;
      while ( curr ) {
        old = curr->data;
        if (*p <= *old) {
          break;
        }
        last = curr;
        curr = curr->next;
      }

      /* -- insert at right place -- */
      add->next = curr;
      if ( last) {
        last->next = add;
      } else {
        first = add;
      }
    }
  }

  /* -- print out sorted list and delete list -- */
  curr = first;
  while ( curr ) {
    p = curr->data;
    cout << p->lastName() << ' ' << p->firstName() << ' ' <<
            p->birthday().year << '.' << p->birthday().month << '.' <<
            p->birthday().day << endl;
    last = curr;
    curr = curr->next;
    delete p;
    delete last;
  }
  return 0;
}