// Arup Guha
// 9/10/04
// Solution to Alphabet problem for 9/11/04 Practice

import java.io.*;

public class alphabet {

  public static void main(String[] args) throws IOException {

    BufferedReader fin=new BufferedReader(new FileReader("alphabet.in"));

    // Read in the alphabet order.
    String perm = fin.readLine();

    String[] words = new String[50];
    int index = 0, length;

    // Read in all the words.
    while (fin.ready()) {
      words[index] = fin.readLine();
      index++;
    }
    length = index; // Store the total number of words.

    sort(words, perm, length);
    print(words, length);

  }

  public static void sort(String[] words, String perm, int length) {

    int i,j;

    // Do an insertion sort of words based on perm.
    for (i=1; i<length; i++) {
      j = i;

      // Insert the current element.
      while (j>0 && comesBefore(words[j], words[j-1],perm)) {
        String temp = words[j-1];
        words[j-1] = words[j];
        words[j] = temp;
        j--;
      }
    }
  }

  // Returns true if and only if a comes Before b alphabetically
  // according to perm.
  public static boolean comesBefore(String a, String b, String perm) {

    int i = 0;

    // Skip over equal characters.
    while (i < a.length() && i < b.length() && a.charAt(i) == b.charAt(i))
      i++;

    // Check the case that a word is "done"
    if (i == a.length())
      return true;
    if (i == b.length())
      return false;

    // Otherwise, compare the first non-matching characters...
    return (find(a.charAt(i),perm) < find(b.charAt(i),perm));

  }

  // Returns the place in the adjusted alphabet perm that c is in.
  public static int find(char c, String perm) {

    // Return the array index of perm that c is located.
    for (int i=0; i<26; i++)
      if (perm.charAt(i) == c)
        return i;

    return 26; // default for not found
  }

  public static void print(String[] words, int length) {
    for (int i=0; i<length; i++)
      System.out.println(words[i]);
  }

}
