#!/bin/env python3 import sys n = int(input().strip()) genes = input().strip().split(' ') health = list(map(int, input().strip().split(' '))) gh = {} i = 0 while i < n: g = genes[i] if g not in gh: gh[g] = [] gh[g].append(health[i]) i+= 1 genes = set(genes) s = int(input().strip()) hmin = hmax = -1 for a0 in range(s): first,last,d = input().strip().split(' ') first,last,d = [int(first),int(last),str(d)] # your code goes here h = 0 i = first while i < last: substr = d[i:] for g in genes: if substr.find(g) == 0: h += sum(gh[g]) i += 1 if h < hmin or hmin < 0: hmin = h if h > hmax or hmax < 0: hmax = h print(hmin, hmax)