/* Problem 5--References
   This was a bit tedious, but you just plow through the strings and
   reorganize the data. */

import java.io.*;

class LLS { //A node in a linked list of string
 public String s;
 public LLS next;
 public LLS (String S, LLS N) {s=S; next = N;}
}

class LL { //A linked list of string

 public int ct=0;
 public LLS head=null;

 public void add (String s) { //adds a string to the head
  head = new LLS (s,head);
  ct++;
 }

 public String[] spew () { //returns an array of strings in the list

  String[] SA = new String[ct];
  LLS P = head;
  for (int i=ct-1; i >= 0; i--) {
   SA[i] = P.s;
   P = P.next;
  }
  return SA;
 }
}

public class prob5 {

 public static BufferedReader in;
 public static PrintStream out;
 public static boolean start = true;
 //P is the list of paragraph lines, R the list of ref lines
 public static LL P = new LL(), R = new LL(), D = P;
 public static int[] Table = new int[100], RTable = new int[100];
 public static int rc = 1;

 public static void main (String[] args) throws Exception {

  in = new BufferedReader (new FileReader ("prob5.in"));
  out = new PrintStream (new FileOutputStream ("prob5.out"));
  String line; //Read in the data
  while ((line=in.readLine())!=null) ReadIn (line);
  String[] PStr = P.spew (), RStr = R.spew ();
  for (int i=0; i < PStr.length; i++) Process (PStr[i]);
  for (int i=0; i < PStr.length; i++) PrintLine (PStr[i]);
  for (int i=0; i < 100; i++) //Print the paragraphs
   if (Table[i] != 0) RTable[Table[i]] = i;
  for (int i=1; i < 100 && RTable[i] > 0; i++)
   PrintRef (i,RTable[i],RStr); //Print the references
 }

/* ReadIn places a line of text into the appropriate list */
 public static void ReadIn (String line) {

  if (line.trim().equals ("")) start = true;
  if (start && line.length() > 0 && line.charAt(0)=='[') {
   D = R; //Start of a reference
   start = false;
  } else if (start && line.trim().length() > 0) {
   D = P; //Start of a paragraph
   start = false;
  }
  D.add (line); //Add to current list
 }

/* Takes a line and records the references in order */
 public static void Process (String line) {

  for (int i=0; i < line.length(); i++)
   if (line.charAt (i) == '[') {
    int ct = 0;
    for (i++;line.charAt (i) != ']'; i++)
     ct = 10*ct+(line.charAt(i)-'0');
    if (Table[ct]==0) Table[ct] = rc++;
   }
 }

/* Prints a line of paragraph text, substituting reference numbers */
 public static void PrintLine (String line) {

  for (int i=0; i < line.length(); i++)
   if (line.charAt (i) == '[') {
    int ct = 0;
    for (i++;line.charAt (i) != ']'; i++)
     ct = 10*ct+(line.charAt(i)-'0');
    out.print ("["+Table[ct]+"]");
   } else out.print (line.charAt (i));
  out.println ();
 }

/* Prints a reference paragraph converting r to i. */
 public static void PrintRef (int i, int r, String[] RStr) {

  boolean OK = true;
  for (int ii=0; ii < RStr.length; ii++) {
   if (OK && RStr[ii].trim().length() > 0 & RStr[ii].charAt(0) == '[') {
    OK = false; //Search for the proper reference
    int ct = 0;
    int j;
    for (j=1;RStr[ii].charAt (j) != ']'; j++)
     ct = 10*ct+(RStr[ii].charAt(j)-'0');
    if (ct==r) { //Print out whole paragraph
     out.println ("["+i+"]"+RStr[ii].substring (j+1));
     for (ii++;RStr[ii].trim().length()> 0;ii++) out.println (RStr[ii]);
     for (;ii < RStr.length && RStr[ii].trim().length()==0; ii++)
      out.println (RStr[ii]);
     return;
    }
   } else if (OK && RStr[ii].trim().length() > 0) OK = false;
   else if (!OK && RStr[ii].trim().length()==0) OK = true;
  }
 }
}

