import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { int weakestGene = 0; int strongestGene = 0; Scanner in = new Scanner(System.in); int n = in.nextInt(); String[] genes = new String[n]; for(int genes_i=0; genes_i < n; genes_i++){ genes[genes_i] = in.next(); } int[] health = new int[n]; for(int health_i=0; health_i < n; health_i++){ health[health_i] = in.nextInt(); } int s = in.nextInt(); int[] numbers = new int[s]; int index = 0; for(int a0 = 0; a0 < s; a0++){ int first = in.nextInt(); int last = in.nextInt(); String d = in.next(); // your code goes here for(int i = first; i <= last; i++) { char[] firstWord = d.toCharArray(); char[] second = genes[i].toCharArray(); for(int y = 0; y < d.length()-second.length; y++) { for(int z = 0; z < second.length; z++) { boolean match = true; for(int u = y; u < y+second.length; u++) { if(firstWord[u] != second[z]) { match = false; } } if(match) { numbers[index] += health[i]; } } } } index++; } int least = numbers[0]; int most = numbers[0]; for(int i = 0; i < numbers.length; i++) { if(numbers[i] > most) { most = numbers[i]; } if(numbers[i] < least) { least = numbers[i]; } } System.out.print(least+" "+most); } }