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

class Words
{
    public static void printNicely( Map<String,List<Integer>> m )
    {
        for( Map.Entry<String,List<Integer>> e : m.entrySet( ) )
        {
            System.out.print( String.format( "%20s ", e.getKey( ) ) );

            List<Integer> theLines = e.getValue( );
            Iterator<Integer> itr = theLines.iterator( );

            System.out.print( itr.next( ) );
            while( itr.hasNext( ) )
                System.out.print( ", " + itr.next( ) );
            
            System.out.println( );
        }


    }


    public static void listFile( String fileName ) throws IOException
    {
        File f = new File( fileName );
        Scanner scan = new Scanner( f );
        Map<String,List<Integer>> words = new TreeMap<String,List<Integer>>( );

        String oneLine;
        int lineNum = 0;
        while( scan.hasNextLine( ) )
        {
            oneLine = scan.nextLine( );
            lineNum++;

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

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

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

        }

        printNicely( words );
    }


    public static void main( String [ ] args ) 
    {
        try
        {
            listFile( "data.txt" );
        }
        catch( IOException e )
        {
            System.out.println( "I/O Error on data file" );
        }
    }
}

