#!/bin/python3 import sys n = int(input().strip()) g = input().strip().split(' ') h = list(map(int, input().strip().split(' '))) s = int(input().strip()) tot = [0]*s for a0 in range(s): first,last,d = input().strip().split(' ') first,last,d = [int(first),int(last),str(d)] # print ("before",tot) for i in range(first,last+1) : ln = len(g[i]) j = 0 while j < len(d) : if ln == 1 : cnt = d.count(g[i]) tot[a0] = tot[a0] + (cnt*h[i]) j = j+1 break else : f = d.find(g[i],j,len(d)) if f != -1: tot[a0] = tot[a0] + h[i] # print ("into ",tot,cnt,g[i],h[i],j) j = j + len(g[i]) else : j = j+1 # print ("after",tot,cnt,g[i],h[i]) # print (tot) tot.sort(); print (tot[0],tot[s-1]) # your code goes here