#!/bin/python import sys def check_contain_gene(gene, d, j): if(d[j:j+len(gene)] == gene): return True else: return False n = int(raw_input().strip()) genes = raw_input().strip().split(' ') health = map(int, raw_input().strip().split(' ')) s = int(raw_input().strip()) max_bene = 0 min_bene = 999999 for a0 in xrange(s): first,last,d = raw_input().strip().split(' ') first,last,d = [int(first),int(last),str(d)] # your code goes here benifit = 0 for i in range(first, last+1): for j in range(len(d)): if check_contain_gene(genes[i],d,j): benifit += health[i] #print "the benifit for", a0 , "is:", benifit if benifit > max_bene: max_bene = benifit #print "The maximum benefit is:", max_bene if benifit < min_bene: min_bene = benifit print min_bene, max_bene