#!/bin/python3 import sys import re n = int(input().strip()) genes = input().strip().split(' ') health = list(map(int, input().strip().split(' '))) s = int(input().strip()) total_health =[]; for a0 in range(s): first,last,d = input().strip().split(' ') first,last,d = [int(first),int(last),str(d)] # your code goes here total_health.append(0); for i in range(first,last+1): pat = r'^.*'+genes[i]+'.*$'; result = re.findall(pat,d); total_health[a0] += len(result)*health[i]; print (total_health) total_health.sort(); print (total_health) print (total_health[0]); print (total_health[s-1]);