import java.util.*;

public class DictionaryWordFinder {
    
    // Finds the last occurrence of a word matching prefix and suffix
    public static int findPrefixSuffixMatch(String[] dictionary, char prefix, char suffix) {
        int lastMatchIndex = -1;
        for (int i = 0; i < dictionary.length; i++) {
            String word = dictionary[i];
            if (word != null && word.length() > 0) {
                if (word.charAt(0) == prefix && word.charAt(word.length() - 1) == suffix) {
                    lastMatchIndex = i;
                }
            }
        }
        return lastMatchIndex;
    }
    
    // Finds the index of the longest word
    public static int findLongestWord(String[] dictionary) {
        if (dictionary.length == 0) return -1;
        int maxLength = 0;
        int longestWordIndex = -1;
        for (int i = 0; i < dictionary.length; i++) {
            String word = dictionary[i];
            if (word != null && word.length() > maxLength) {
                maxLength = word.length();
                longestWordIndex = i;
            }
        }
        return longestWordIndex;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Read number of words
        int n = Integer.parseInt(scanner.nextLine().trim());
        
        // Validate n
        if (n < 0) {
            System.out.println("-1");
            System.out.println("-1");
            return;
        }
        if (n == 0) {
            System.out.println("-1");
            System.out.println("-1");
            return;
        }
        
        // Read dictionary
        String[] dictionary = new String[n];
        for (int i = 0; i < n; i++) {
            dictionary[i] = scanner.nextLine().trim();
        }
        
        // ⚠️ No try/catch here → if prefix/suffix missing, program will throw NoSuchElementException
        char prefix = scanner.nextLine().trim().charAt(0);
        char suffix = scanner.nextLine().trim().charAt(0);
        
        // Find results
        int prefixSuffixMatchIndex = findPrefixSuffixMatch(dictionary, prefix, suffix);
        int longestWordIndex = findLongestWord(dictionary);
        
        // Print results
        System.out.println(prefixSuffixMatchIndex);
        System.out.println(longestWordIndex);
    }
}
