1 import java.io.*;
  2 import java.util.Scanner;
  3 
  4 class serials {
  5 
  6   public static void main( String[] args ) throws Exception {
  7     String inFile = args.length > 0 ? args[0] : "serials.in";
  8     Scanner in = new Scanner( new File( inFile ) );
  9     String s = in.nextLine();
 10     while ( !s.equals( "END" ) ) {
 11       System.out.println( s );
 12       list serials = new list();
 13       int a = in.nextInt();
 14       while ( a != 0 ) {
 15         int b = in.nextInt();
 16         char c = in.next().charAt(0);
 17         int t = in.nextInt();
 18         serials.insert( a, b, c, t );
 19         a = in.nextInt();
 20       }
 21       serials.display();
 22       in.nextLine();
 23       s = in.nextLine();
 24     }
 25   }
 26 
 27 }
 28 
 29 class list {
 30 
 31   node head;
 32 
 33   list() {
 34     head = new node( 0, 0, '0', 0, null );
 35   }
 36 
 37   void insert( int x, int y, char c, int t ) {
 38 
 39     // find the insert point
 40     node p = head;
 41     node q = head.next;
 42     while ( q != null && q.a <= x ) {
 43       p = q;
 44       q = q.next;
 45     }
 46 
 47     // insert the new node
 48     q = new node( p.a, p.b, p.c, p.t, p.next );
 49     if ( x == q.a ) {
 50       p.b = y;
 51       p.c = c;
 52       p.t = t;
 53       if ( y < q.b ) {
 54         p.next = new node( y+1, q.b, q.c, q.t, p.next );
 55       }
 56     }
 57     else if ( x <= q.b ) {
 58       p.b = x-1;
 59       p.next = new node( x, y, c, t, p.next );
 60       p = p.next;
 61       if ( y < q.b ) {
 62         p.next = new node( y+1, q.b, q.c, q.t, p.next );
 63       }
 64     }
 65     else {
 66       p.next = new node( x, y, c, t, p.next );
 67       p = p.next;
 68     }
 69 
 70     // cleanup any successor nodes overlapped by the new node
 71     while ( p.next != null && y >= p.next.a ) {
 72       if ( y >= p.next.b ) {
 73         p.next = p.next.next;
 74       }
 75       else {
 76         p.next.a = y + 1;
 77       }
 78     }
 79 
 80     // merge adjacent serial number ranges with identical codes
 81     p = head;
 82     while ( p.next != null ) {
 83       if ( p.b + 1 == p.next.a && p.c == p.next.c && p.t == p.next.t ) {
 84         p.b = p.next.b;
 85         p.next = p.next.next;
 86       }
 87       else {
 88         p = p.next;
 89       }
 90     }
 91 
 92   }
 93 
 94   void display() {
 95     node p = head.next;
 96     while ( p != null ) {
 97       System.out.format( "%d %d %c %d%n", p.a, p.b, p.c, p.t );
 98       p = p.next;
 99     }
100   }
101 
102 }
103 
104 class node {
105 
106   int a;  // beginning serial number
107   int b;  // ending serial number
108   char c; // status code
109   int t;  // transfer code;
110   node next;
111 
112   node( int aIn, int bIn, char cIn, int tIn, node nextIn ) {
113     a = aIn;
114     b = bIn;
115     c = cIn;
116     t = tIn;
117     next = nextIn;
118   }
119 
120 }