import java.util.*;

public class DictionarySearch {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Validate first input
        if (!sc.hasNextInt()) {
            System.out.println("Invalid input");
            return;
        }

        int n = sc.nextInt();
        sc.nextLine(); // consume newline

        // If n is negative
        if (n < 0) {
            System.out.println("-1");
            System.out.println("-1");
            return;
        }

        String[] words = new String[n];
        for (int i = 0; i < n; i++) {
            if (!sc.hasNextLine()) {
                System.out.println("Invalid input");
                return;
            }
            words[i] = sc.nextLine().trim();
        }

        // Read prefix
        if (!sc.hasNext()) {
            System.out.println("Invalid input");
            return;
        }
        String prefix = sc.next().trim();

        // Read suffix
        if (!sc.hasNext()) {
            System.out.println("Invalid input");
            return;
        }
        String suffix = sc.next().trim();

        // 1. Find last word matching prefix + suffix
        int matchIndex = -1;
        for (int i = 0; i < n; i++) {
            String w = words[i];
            if (w.startsWith(prefix) && w.endsWith(suffix)) {
                matchIndex = i; // keep updating → last match
            }
        }

        // 2. Find longest word index (first occurrence in tie)
        int longestIndex = -1;
        int maxLen = -1;
        for (int i = 0; i < n; i++) {
            String w = words[i];
            if (w.length() > maxLen) { // strictly greater ensures first occurrence
                maxLen = w.length();
                longestIndex = i;
            }
        }

        System.out.println(matchIndex == -1 ? "-1" : matchIndex);
        System.out.println(longestIndex == -1 ? "-1" : longestIndex);
    }
}
