#!/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()) healthiest = -1 unhealthiest = 100000000000000 for a0 in xrange(s): first,last,d = raw_input().strip().split(' ') first,last,d = [int(first),int(last),str(d)] s = 0 # your code goes here for i in range(first, last+1): if d.find(genes[i])>=0: s+=health[i] if s > healthiest: healthiest = s if s < unhealthiest: unhealthiest = s print unhealthiest, healthiest