import java.util.*;

public class DictionarySearch {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Read first input (number of words)
        String firstInput = sc.nextLine().trim();
        int n;

        // Validate if numeric
        try {
            n = Integer.parseInt(firstInput);
        } catch (NumberFormatException e) {
            System.out.println("Invalid input.");
            return;
        }

        // If n is negative → -1, -1
        if (n < 0) {
            System.out.println(-1);
            System.out.println(-1);
            return;
        }

        // Read words
        String[] words = new String[n];
        for (int i = 0; i < n; i++) {
            words[i] = sc.nextLine().trim();
        }

        // Read prefix and suffix
        if (!sc.hasNextLine()) {
            System.out.println(-1);
            System.out.println(-1);
            return;
        }
        String prefix = sc.nextLine().trim();
        if (!sc.hasNextLine()) {
            System.out.println(-1);
            System.out.println(-1);
            return;
        }
        String suffix = sc.nextLine().trim();

        // ------------------------------
        // 1) Find first word that matches prefix & suffix
        // ------------------------------
        int prefixSuffixIndex = -1;
        for (int i = 0; i < n; i++) {
            String word = words[i];
            if (word.startsWith(prefix) && word.endsWith(suffix)) {
                prefixSuffixIndex = i;
                break;
            }
        }

        // ------------------------------
        // 2) Find longest word index
        // ------------------------------
        int longestIndex = -1;
        int maxLen = -1;
        for (int i = 0; i < n; i++) {
            if (words[i].length() > maxLen) {
                maxLen = words[i].length();
                longestIndex = i;
            }
        }

        // ------------------------------
        // Output results
        // ------------------------------
        System.out.println(prefixSuffixIndex);
        System.out.println(longestIndex);
    }
}
