#!/bin/python3 import sys n = int(input().strip()) genes = input().strip().split(' ') health = list(map(int, input().strip().split(' '))) s = int(input().strip()) for a0 in range(s): first,last,d = input().strip().split(' ') first,last,d = [int(first),int(last),str(d)] min = 0 max = 0 jcnt = 0 for j in range(first,last+1): tem = genes[j] tcnt= d.count(tem) d = d.replace(tem,'',tcnt) jcnt = jcnt + tcnt *health[j] if jcnt >max : max = jcnt if(min >jcnt): min = jcnt print(min,max)