#!/bin/python import sys n = int(raw_input().strip()) genes = raw_input().strip().split(' ') health = map(int, raw_input().strip().split(' ')) s = int(raw_input().strip()) countList = [] 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 count = 0 for char in d: if char in genes[first:last+1]: count = count + health[genes.index(char)] for i in range(len(d)-1): if d[i:i+1] in genes[first:last+1]: count = count + health[genes.index(d[i:i+1])] countList.append(count) countList = sorted(countList) print "{0} {1}".format(countList[0],countList[-1])