import java.util.*;

public class DictionarySearch {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Step 1: Read n and validate
        if (!sc.hasNextInt()) {
            System.out.println("Invalid input");
            return;
        }
        int n = sc.nextInt();

        if (n < 0) {
            System.out.println(-1);
            return;
        }

        // Step 2: Read dictionary words
        sc.nextLine(); // consume leftover newline
        String[] words = new String[n];
        for (int i = 0; i < n; i++) {
            if (!sc.hasNext()) {
                System.out.println("Invalid input");
                return;
            }
            words[i] = sc.nextLine().trim();
        }

        // Step 3: Read prefix and suffix
        if (!sc.hasNext()) {
            System.out.println("Invalid input");
            return;
        }
        String prefix = sc.next().trim();

        if (!sc.hasNext()) {
            System.out.println("Invalid input");
            return;
        }
        String suffix = sc.next().trim();

        // Validate prefix/suffix must be exactly 1 char
        if (prefix.length() != 1 || suffix.length() != 1) {
            System.out.println("Invalid input");
            return;
        }

        // Step 4: Find last occurrence of word with prefix & suffix
        int matchIndex = -1;
        for (int i = 0; i < n; i++) {
            if (words[i].startsWith(prefix) && words[i].endsWith(suffix)) {
                matchIndex = i; // keep updating → last occurrence
            }
        }

        // Step 5: Find longest word
        int longestIndex = -1;
        int maxLen = -1;
        for (int i = 0; i < n; i++) {
            if (words[i].length() > maxLen) {
                maxLen = words[i].length();
                longestIndex = i;
            }
        }

        // Step 6: Print results
        System.out.println(matchIndex);
        System.out.println(longestIndex);
    }
}
