import java.io.*;
import java.util.*;

class GraphReader {
  class Node {
    String url;
    String title;
    int[][] links;
  }
  Node[] nodes;

  /**
   * Construct an instance from a file.
   * @param filename: The name of file from which a graph is read.
   */
  GraphReader( String filename ) throws Exception {
    BufferedReader br = new BufferedReader( new FileReader( filename ) );
    int size = Integer.parseInt( br.readLine( ) );
    nodes = new Node[ size ];

    for ( int i = 0; i < size; i++ ) {
      nodes[ i ] = new Node( );
      setNodeInfo( nodes[ i ], br.readLine( ) );
    }
    for ( int i = 0; i < size; i++ ) {
      setLinkInfo( nodes[ i ], br.readLine( ) );
    }
  }

  /**
   * Return the number of nodes in the graph
   */
  int size( ) {
    return nodes.length;
  }

  /**
   * Return the URL of the index-th node.
   * @param index: Specify the index of the node.
   */
  String getURL( int index ) {
    return nodes[ index ].url;
  }
  
  /**
   * Return the title of the index-th node.
   * @param index: Specify the index of the node.
   */
  String getTitle( int index ) {
    return nodes[ index ].title;
  }

  /**
   * Return an array that has the indeces of nodes linked from the nodes.
   * @param index: Specify the index of the node.
   */
  int[] getLinks( int index ) {
    return nodes[ index ].links[ 0 ];
  }
    
  /**
   * Return an array that has the numbers of linkes from the node to each node
   * in the result of getLinks( );
   * @param index: Specify the index of the node.
   */
  int[] getLinksConut( int index ) {
    return nodes[ index ].links[ 1 ];
  }

  private void setNodeInfo( Node node, String str ) {
    StringTokenizer st = new StringTokenizer( str, "|" );
    st.nextToken( ); // "Node"
    st.nextToken( ); // "index"
    node.url = st.nextToken( );
    node.title = st.nextToken( );
    while( st.hasMoreTokens( ) ) {
      node.title = node.title + "|" + st.nextToken( );
    }
  }

  private void setLinkInfo( Node node, String str ) {
    StringTokenizer st = new StringTokenizer( str, "|" );
    st.nextToken( ); // "Link"
    st.nextToken( ); // "index"
    int size = Integer.parseInt( st.nextToken( ) );
    node.links = new int[ 2 ][ size ];
    for ( int i = 0; i < size; i++ ) {
      StringTokenizer st2 = new StringTokenizer( st.nextToken( ) );
      node.links[ 0 ][ i ] = Integer.parseInt( st2.nextToken( ) );
      node.links[ 1 ][ i ] = Integer.parseInt( st2.nextToken( ) );
    }
  }

  /**
   * A sample and test code.
   */
  public static void main( String[] args ) {
    try {
      GraphReader graphReader = new GraphReader( args[ 0 ] );

      int size = graphReader.size( );
      System.out.println( "The number of nodes = " + size );

      System.out.println( "The URL of the first node is " +
			  graphReader.getURL( 0 ) );
      System.out.println( "The title of the first node is " +
			  graphReader.getTitle( 0 ) );

      int[] links = graphReader.getLinks( 0 );
      int[] counts = graphReader.getLinksConut( 0 );

      System.out.println( "The links from the first nodes are as follows. " );
      for ( int i = 0; i < links.length; i++ ) {
	System.out.println( "To node " + links[ i ] + ", " +
			    "the number of links " + counts[ i ] );
      }
    }
    catch ( Exception e ) {
      e.printStackTrace( );
    }
  }
}
