import java.io.File;
import java.util.Map;
import java.util.TreeMap;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

class Day21
{
    public static void printMap( Map<String,List<Integer>> m )
    {
        for( Map.Entry<String,List<Integer>> e : m.entrySet( ) )
        {
            System.out.print( e.getKey( ) + " occurs on lines" );
            for( Integer x : e.getValue( ) )
                System.out.print( " " + x );
            System.out.println( );
        }
    }

    public static void main( String [ ] args )
    {
        File f = new File( "day21.txt" );
        try
        {
            Scanner sc = new Scanner( f );
            Map<String,List<Integer>> words = new TreeMap<String,List<Integer>>( );
            int lineNum = 0;

            while( sc.hasNextLine( ) )
            {
                String thisLine = sc.nextLine( );
                lineNum++;

                Scanner lineScan = new Scanner( thisLine );
                while( lineScan.hasNext( ) )
                {
                    String thisWord = lineScan.next( );

                    List<Integer> theLines = words.get( thisWord );

                    if( theLines == null )
                    {
                        theLines = new ArrayList<Integer>( );
                        words.put( thisWord, theLines );
                        theLines.add( lineNum );
                    }
                    else
                    {
                        theLines.add( lineNum );
                    }
                }
            }

            printMap( words );
        }
        catch( IOException e )
        {
            System.out.println( "Error opening data file" );
        }
    }

}

