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) { 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 maxHealth = 0, minHealth = Integer.MAX_VALUE; for(int a0 = 0; a0 < s; a0++){ int first = in.nextInt(); int last = in.nextInt(); String d = in.next(); // your code goes here int totalHealth = 0; for (int i = first; i <= last; i++) { String gene = genes[i]; // System.out.println(gene); totalHealth += health[i] * countOccurrance(d, gene); } if (totalHealth > maxHealth) { maxHealth = totalHealth; } if (totalHealth < minHealth) { minHealth = totalHealth; } } System.out.println(minHealth + " " + maxHealth); } private static int countOccurrance(String dna, String gene) { int dnaLen = dna.length(); int geneLen = gene.length(); int occ = 0; for (int i = 0; i < dnaLen; i++) { boolean match = true; for (int j = 0; j < geneLen; j++) { if ((i + j < dnaLen) && dna.charAt(i + j) != gene.charAt(j)) { match = false; break; } } if (match) { occ++; // System.out.println(i + " " + gene); } } return occ; } }